From ee9ade8aa608c58184fa9cb7bd17cd4854b17c52 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 13 Dec 2020 15:01:33 -0600 Subject: [PATCH] Update: always ensure standard file descriptors are closed on program exit. Explicitly close file descritpros on exit. This is especially important now that the program could be a child process and the file descriptor could be open between a parent process. --- level_3/byte_dump/c/main.c | 9 ++++++++- level_3/control/c/control.h | 1 + level_3/control/c/main.c | 9 ++++++++- level_3/controller/c/main.c | 9 ++++++++- level_3/fake/c/main.c | 5 +++++ level_3/firewall/c/firewall.h | 1 + level_3/fss_basic_list_read/c/fss_basic_list_read.h | 1 + level_3/fss_basic_list_read/c/main.c | 9 ++++++++- level_3/fss_basic_list_write/c/fss_basic_list_write.h | 1 + level_3/fss_basic_list_write/c/main.c | 9 ++++++++- level_3/fss_basic_read/c/fss_basic_read.h | 1 + level_3/fss_basic_read/c/main.c | 9 ++++++++- level_3/fss_basic_write/c/fss_basic_write.h | 1 + level_3/fss_basic_write/c/main.c | 9 ++++++++- level_3/fss_embedded_list_read/c/fss_embedded_list_read.h | 1 + level_3/fss_embedded_list_read/c/main.c | 9 ++++++++- level_3/fss_embedded_list_write/c/fss_embedded_list_write.h | 1 + level_3/fss_embedded_list_write/c/main.c | 9 ++++++++- level_3/fss_extended_list_read/c/fss_extended_list_read.h | 1 + level_3/fss_extended_list_read/c/main.c | 9 ++++++++- level_3/fss_extended_list_write/c/fss_extended_list_write.h | 1 + level_3/fss_extended_list_write/c/main.c | 9 ++++++++- level_3/fss_extended_read/c/fss_extended_read.h | 1 + level_3/fss_extended_read/c/main.c | 9 ++++++++- level_3/fss_extended_write/c/fss_extended_write.h | 1 + level_3/fss_extended_write/c/main.c | 9 ++++++++- level_3/fss_status_code/c/fss_status_code.h | 1 + level_3/fss_status_code/c/main.c | 5 +++++ level_3/iki_read/c/iki_read.h | 1 + level_3/iki_read/c/main.c | 9 ++++++++- level_3/iki_write/c/iki_write.h | 1 + level_3/iki_write/c/main.c | 9 ++++++++- level_3/init/c/init.h | 1 + level_3/init/c/main.c | 8 ++++++-- level_3/status_code/c/main.c | 5 +++++ level_3/status_code/c/status_code.h | 1 + 36 files changed, 158 insertions(+), 17 deletions(-) diff --git a/level_3/byte_dump/c/main.c b/level_3/byte_dump/c/main.c index 9a955d2..1714422 100644 --- a/level_3/byte_dump/c/main.c +++ b/level_3/byte_dump/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(byte_dump_main(arguments, &data))) { + const f_status_t status = byte_dump_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/control/c/control.h b/level_3/control/c/control.h index 0be02a6..0311111 100644 --- a/level_3/control/c/control.h +++ b/level_3/control/c/control.h @@ -13,6 +13,7 @@ #ifndef _control_h // libc includes +#include // fll-0 includes #include diff --git a/level_3/control/c/main.c b/level_3/control/c/main.c index 2759341..b0da32a 100644 --- a/level_3/control/c/main.c +++ b/level_3/control/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(control_main(arguments, &data))) { + const f_status_t status = control_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/controller/c/main.c b/level_3/controller/c/main.c index 653a1e4..d9cde60 100644 --- a/level_3/controller/c/main.c +++ b/level_3/controller/c/main.c @@ -10,7 +10,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.pid = getpid(); - if (F_status_is_error(controller_main(arguments, &data))) { + const f_status_t status = controller_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fake/c/main.c b/level_3/fake/c/main.c index 5d612b0..bf9a47e 100644 --- a/level_3/fake/c/main.c +++ b/level_3/fake/c/main.c @@ -45,6 +45,11 @@ int main(const unsigned long argc, const f_string_t *argv) { f_signal_close(&data.signal); + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/firewall/c/firewall.h b/level_3/firewall/c/firewall.h index 40797fb..51b5a13 100644 --- a/level_3/firewall/c/firewall.h +++ b/level_3/firewall/c/firewall.h @@ -15,6 +15,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_basic_list_read/c/fss_basic_list_read.h b/level_3/fss_basic_list_read/c/fss_basic_list_read.h index 2925fa5..7ce5951 100644 --- a/level_3/fss_basic_list_read/c/fss_basic_list_read.h +++ b/level_3/fss_basic_list_read/c/fss_basic_list_read.h @@ -18,6 +18,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_basic_list_read/c/main.c b/level_3/fss_basic_list_read/c/main.c index 8bc7bb7..c7119c8 100644 --- a/level_3/fss_basic_list_read/c/main.c +++ b/level_3/fss_basic_list_read/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_basic_list_read_main(arguments, &data))) { + const f_status_t status = fss_basic_list_read_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_basic_list_write/c/fss_basic_list_write.h b/level_3/fss_basic_list_write/c/fss_basic_list_write.h index c3cf3d2..091c452 100644 --- a/level_3/fss_basic_list_write/c/fss_basic_list_write.h +++ b/level_3/fss_basic_list_write/c/fss_basic_list_write.h @@ -13,6 +13,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_basic_list_write/c/main.c b/level_3/fss_basic_list_write/c/main.c index d7b0ee0..8821ae6 100644 --- a/level_3/fss_basic_list_write/c/main.c +++ b/level_3/fss_basic_list_write/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_basic_list_write_main(arguments, &data))) { + const f_status_t status = fss_basic_list_write_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_basic_read/c/fss_basic_read.h b/level_3/fss_basic_read/c/fss_basic_read.h index 8613c14..c473b13 100644 --- a/level_3/fss_basic_read/c/fss_basic_read.h +++ b/level_3/fss_basic_read/c/fss_basic_read.h @@ -18,6 +18,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_basic_read/c/main.c b/level_3/fss_basic_read/c/main.c index 8c53a8c..525f9a8 100644 --- a/level_3/fss_basic_read/c/main.c +++ b/level_3/fss_basic_read/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_basic_read_main(arguments, &data))) { + const f_status_t status = fss_basic_read_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_basic_write/c/fss_basic_write.h b/level_3/fss_basic_write/c/fss_basic_write.h index 137a743..086d6ff 100644 --- a/level_3/fss_basic_write/c/fss_basic_write.h +++ b/level_3/fss_basic_write/c/fss_basic_write.h @@ -13,6 +13,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_basic_write/c/main.c b/level_3/fss_basic_write/c/main.c index 8a52fc7..3e3b56b 100644 --- a/level_3/fss_basic_write/c/main.c +++ b/level_3/fss_basic_write/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_basic_write_main(arguments, &data))) { + const f_status_t status = fss_basic_write_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_embedded_list_read/c/fss_embedded_list_read.h b/level_3/fss_embedded_list_read/c/fss_embedded_list_read.h index 255c83d..09a09da 100644 --- a/level_3/fss_embedded_list_read/c/fss_embedded_list_read.h +++ b/level_3/fss_embedded_list_read/c/fss_embedded_list_read.h @@ -18,6 +18,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_embedded_list_read/c/main.c b/level_3/fss_embedded_list_read/c/main.c index 904d21f..7cd347c 100644 --- a/level_3/fss_embedded_list_read/c/main.c +++ b/level_3/fss_embedded_list_read/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_embedded_list_read_main(arguments, &data))) { + const f_status_t status = fss_embedded_list_read_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_embedded_list_write/c/fss_embedded_list_write.h b/level_3/fss_embedded_list_write/c/fss_embedded_list_write.h index c3df671..9e57388 100644 --- a/level_3/fss_embedded_list_write/c/fss_embedded_list_write.h +++ b/level_3/fss_embedded_list_write/c/fss_embedded_list_write.h @@ -13,6 +13,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_embedded_list_write/c/main.c b/level_3/fss_embedded_list_write/c/main.c index 7db7189..d194dfb 100644 --- a/level_3/fss_embedded_list_write/c/main.c +++ b/level_3/fss_embedded_list_write/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_embedded_list_write_main(arguments, &data))) { + const f_status_t status = fss_embedded_list_write_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_extended_list_read/c/fss_extended_list_read.h b/level_3/fss_extended_list_read/c/fss_extended_list_read.h index 71a8771..62f9d89 100644 --- a/level_3/fss_extended_list_read/c/fss_extended_list_read.h +++ b/level_3/fss_extended_list_read/c/fss_extended_list_read.h @@ -18,6 +18,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_extended_list_read/c/main.c b/level_3/fss_extended_list_read/c/main.c index a53a249..c1d0383 100644 --- a/level_3/fss_extended_list_read/c/main.c +++ b/level_3/fss_extended_list_read/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_extended_list_read_main(arguments, &data))) { + const f_status_t status = fss_extended_list_read_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_extended_list_write/c/fss_extended_list_write.h b/level_3/fss_extended_list_write/c/fss_extended_list_write.h index 791f225..fc4151b 100644 --- a/level_3/fss_extended_list_write/c/fss_extended_list_write.h +++ b/level_3/fss_extended_list_write/c/fss_extended_list_write.h @@ -13,6 +13,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_extended_list_write/c/main.c b/level_3/fss_extended_list_write/c/main.c index 2360d4d..d543498 100644 --- a/level_3/fss_extended_list_write/c/main.c +++ b/level_3/fss_extended_list_write/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_extended_list_write_main(arguments, &data))) { + const f_status_t status = fss_extended_list_write_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_extended_read/c/fss_extended_read.h b/level_3/fss_extended_read/c/fss_extended_read.h index ab106be..6126350 100644 --- a/level_3/fss_extended_read/c/fss_extended_read.h +++ b/level_3/fss_extended_read/c/fss_extended_read.h @@ -18,6 +18,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_extended_read/c/main.c b/level_3/fss_extended_read/c/main.c index d1a3e10..d9c3ea9 100644 --- a/level_3/fss_extended_read/c/main.c +++ b/level_3/fss_extended_read/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_extended_read_main(arguments, &data))) { + const f_status_t status = fss_extended_read_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_extended_write/c/fss_extended_write.h b/level_3/fss_extended_write/c/fss_extended_write.h index 2f674fc..9b94f5d 100644 --- a/level_3/fss_extended_write/c/fss_extended_write.h +++ b/level_3/fss_extended_write/c/fss_extended_write.h @@ -13,6 +13,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_extended_write/c/main.c b/level_3/fss_extended_write/c/main.c index 5f78046..4eee1d6 100644 --- a/level_3/fss_extended_write/c/main.c +++ b/level_3/fss_extended_write/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(fss_extended_write_main(arguments, &data))) { + const f_status_t status = fss_extended_write_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/fss_status_code/c/fss_status_code.h b/level_3/fss_status_code/c/fss_status_code.h index c3dccd5..20af567 100644 --- a/level_3/fss_status_code/c/fss_status_code.h +++ b/level_3/fss_status_code/c/fss_status_code.h @@ -13,6 +13,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/fss_status_code/c/main.c b/level_3/fss_status_code/c/main.c index 765f96f..a668c81 100644 --- a/level_3/fss_status_code/c/main.c +++ b/level_3/fss_status_code/c/main.c @@ -10,6 +10,11 @@ int main(const unsigned long argc, const f_string_t *argv) { const f_status_t status = fss_status_code_main(arguments, &data); + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + if (F_status_is_error(status) || status == F_false) { return 1; } diff --git a/level_3/iki_read/c/iki_read.h b/level_3/iki_read/c/iki_read.h index bdde2b8..23c1a9e 100644 --- a/level_3/iki_read/c/iki_read.h +++ b/level_3/iki_read/c/iki_read.h @@ -19,6 +19,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/iki_read/c/main.c b/level_3/iki_read/c/main.c index d0c4093..0a7f6b4 100644 --- a/level_3/iki_read/c/main.c +++ b/level_3/iki_read/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(iki_read_main(arguments, &data))) { + const f_status_t status = iki_read_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/iki_write/c/iki_write.h b/level_3/iki_write/c/iki_write.h index 9d371fb..1bddce4 100644 --- a/level_3/iki_write/c/iki_write.h +++ b/level_3/iki_write/c/iki_write.h @@ -19,6 +19,7 @@ #include #include #include +#include // fll-0 includes #include diff --git a/level_3/iki_write/c/main.c b/level_3/iki_write/c/main.c index f86f945..08a68b7 100644 --- a/level_3/iki_write/c/main.c +++ b/level_3/iki_write/c/main.c @@ -8,7 +8,14 @@ int main(const unsigned long argc, const f_string_t *argv) { data.process_pipe = F_true; } - if (F_status_is_error(iki_write_main(arguments, &data))) { + const f_status_t status = iki_write_main(arguments, &data); + + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/init/c/init.h b/level_3/init/c/init.h index f28bf2d..84979cb 100644 --- a/level_3/init/c/init.h +++ b/level_3/init/c/init.h @@ -20,6 +20,7 @@ // libc includes #include +#include // fll-0 includes #include diff --git a/level_3/init/c/main.c b/level_3/init/c/main.c index e6fe030..cd1282b 100644 --- a/level_3/init/c/main.c +++ b/level_3/init/c/main.c @@ -3,7 +3,6 @@ int main(const unsigned long argc, const f_string_t *argv) { const f_console_arguments_t arguments = { argc, argv }; init_data_t data = init_data_initialize; - f_status_t status = F_none; f_signal_set_empty(&data.signal.set); f_signal_set_add(F_signal_abort, &data.signal.set); @@ -25,10 +24,15 @@ int main(const unsigned long argc, const f_string_t *argv) { // restore umask. umask(data.umask); - status = init_main(arguments, &data); + const f_status_t status = init_main(arguments, &data); f_signal_close(&data.signal); + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + if (F_status_is_error(status)) { return 1; } diff --git a/level_3/status_code/c/main.c b/level_3/status_code/c/main.c index 19775d1..1126a88 100644 --- a/level_3/status_code/c/main.c +++ b/level_3/status_code/c/main.c @@ -10,6 +10,11 @@ int main(const unsigned long argc, const f_string_t *argv) { const f_status_t status = status_code_main(arguments, &data); + // close all open file descriptors. + close(f_type_descriptor_output); + close(f_type_descriptor_input); + close(f_type_descriptor_error); + if (F_status_is_error(status) || status == F_false) { return 1; } diff --git a/level_3/status_code/c/status_code.h b/level_3/status_code/c/status_code.h index 7d14654..ab12225 100644 --- a/level_3/status_code/c/status_code.h +++ b/level_3/status_code/c/status_code.h @@ -13,6 +13,7 @@ #include #include #include +#include // fll-0 includes #include -- 1.8.3.1