0001-Replace-nonstandard-on_exit-function-by-atexit.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. From e57ad4c71cce734de7f8aa75e84fce97bc148c2b Mon Sep 17 00:00:00 2001
  2. From: Maksim Kiselev <bigunclemax@gmail.com>
  3. Date: Mon, 15 May 2023 14:46:56 +0300
  4. Subject: [PATCH] Replace nonstandard on_exit() function by atexit()
  5. on_exit() is not portable and not available on the C libraries musl
  6. and uClibc.
  7. So let's replace it with standard atexit() function.
  8. Upstream: https://github.com/intel/ledmon/pull/139
  9. Signed-off-by: Maksim Kiselev <bigunclemax@gmail.com>
  10. ---
  11. src/ledctl.c | 12 ++++-------
  12. src/ledmon.c | 59 +++++++++++++++++++++++++++-------------------------
  13. 2 files changed, 35 insertions(+), 36 deletions(-)
  14. diff --git a/src/ledctl.c b/src/ledctl.c
  15. index 7a89a24..10fd57a 100644
  16. --- a/src/ledctl.c
  17. +++ b/src/ledctl.c
  18. @@ -214,15 +214,11 @@ static void ibpi_state_fini(struct ibpi_state *p)
  19. *
  20. * This is internal function of ledctl utility. The function cleans up a memory
  21. * allocated for the application and closes all opened handles. This function is
  22. - * design to be registered as on_exit() handler function.
  23. - *
  24. - * @param[in] status exit status of the ledctl application.
  25. - * @param[in] ignored function ignores this argument.
  26. + * design to be registered as atexit() handler function.
  27. *
  28. * @return The function does not return a value.
  29. */
  30. -static void _ledctl_fini(int status __attribute__ ((unused)),
  31. - void *ignore __attribute__ ((unused)))
  32. +static void _ledctl_fini(void)
  33. {
  34. sysfs_reset();
  35. list_erase(&ibpi_list);
  36. @@ -948,7 +944,7 @@ static char *ledctl_strstatus(ledctl_status_code_t s)
  37. * @brief Application's entry point.
  38. *
  39. * This is the entry point of ledctl utility. This function does all the work.
  40. - * It allocates and initializes all used structures. Registers on_exit()
  41. + * It allocates and initializes all used structures. Registers atexit()
  42. * handlers.
  43. * Then the function parses command line options and commands given and scans
  44. * sysfs tree for controllers, block devices and RAID devices. If no error is
  45. @@ -983,7 +979,7 @@ int main(int argc, char *argv[])
  46. status = _init_ledctl_conf();
  47. if (status != LEDCTL_STATUS_SUCCESS)
  48. return status;
  49. - if (on_exit(_ledctl_fini, progname))
  50. + if (atexit(_ledctl_fini))
  51. exit(LEDCTL_STATUS_ONEXIT_ERROR);
  52. slot_request_init(&slot_req);
  53. status = _cmdline_parse(argc, argv, &slot_req);
  54. diff --git a/src/ledmon.c b/src/ledmon.c
  55. index 6f52fd6..1329295 100644
  56. --- a/src/ledmon.c
  57. +++ b/src/ledmon.c
  58. @@ -57,6 +57,19 @@
  59. #include "utils.h"
  60. #include "vmdssd.h"
  61. +/**
  62. + * This macro is the alternative way to get exit status
  63. + * in atexit() callback function
  64. + */
  65. +#define EXIT(x) ((exit)(exit_status = (x)))
  66. +
  67. +static int exit_status;
  68. +
  69. +/**
  70. + * Flag to print exit status
  71. + */
  72. +static int ignore;
  73. +
  74. /**
  75. * @brief List of active block devices.
  76. *
  77. @@ -151,20 +164,16 @@ static int possible_params_size = ARRAY_SIZE(possible_params);
  78. *
  79. * This is internal function of monitor service. It is used to finalize daemon
  80. * process i.e. free allocated memory, unlock and remove pidfile and close log
  81. - * file and syslog. The function is registered as on_exit() handler.
  82. - *
  83. - * @param[in] status The function ignores this parameter.
  84. - * @param[in] program_name The name of the binary file. This argument
  85. - * is passed via on_exit() function.
  86. + * file and syslog. The function is registered as atexit() handler.
  87. *
  88. * @return The function does not return a value.
  89. */
  90. -static void _ledmon_fini(int __attribute__ ((unused)) status, void *program_name)
  91. +static void _ledmon_fini(void)
  92. {
  93. sysfs_reset();
  94. list_erase(&ledmon_block_list);
  95. log_close();
  96. - pidfile_remove(program_name);
  97. + pidfile_remove(progname);
  98. }
  99. typedef enum {
  100. @@ -207,30 +216,25 @@ static char *ledmon_strstatus(ledmon_status_code_t s)
  101. *
  102. * This is internal function of monitor service. It is used to report an exit
  103. * status of the monitor service. The message is logged in to syslog and to log
  104. - * file. The function is registered as on_exit() hander.
  105. - *
  106. - * @param[in] status Status given in the last call to exit()
  107. - * function.
  108. - * @param[in] arg Argument passed to on_exit().
  109. + * file. The function is registered as atexit() handler.
  110. *
  111. * @return The function does not return a value.
  112. */
  113. -static void _ledmon_status(int status, void *arg)
  114. +static void _ledmon_status(void)
  115. {
  116. int log_level;
  117. char message[4096];
  118. - int ignore = *((int *)arg);
  119. if (ignore)
  120. return;
  121. - if (status == LEDMON_STATUS_SUCCESS)
  122. + if (exit_status == LEDMON_STATUS_SUCCESS)
  123. log_level = LOG_LEVEL_INFO;
  124. else
  125. log_level = LOG_LEVEL_ERROR;
  126. snprintf(message, sizeof(message), "exit status is %s.",
  127. - ledmon_strstatus(status));
  128. + ledmon_strstatus(exit_status));
  129. if (get_log_fd() >= 0)
  130. _log(log_level, message);
  131. @@ -364,10 +368,10 @@ static ledmon_status_code_t _cmdline_parse_non_daemonise(int argc, char *argv[])
  132. break;
  133. case 'h':
  134. _ledmon_help();
  135. - exit(EXIT_SUCCESS);
  136. + EXIT(EXIT_SUCCESS);
  137. case 'v':
  138. _ledmon_version();
  139. - exit(EXIT_SUCCESS);
  140. + EXIT(EXIT_SUCCESS);
  141. case ':':
  142. case '?':
  143. return LEDMON_STATUS_CMDLINE_ERROR;
  144. @@ -890,14 +894,13 @@ static void _close_parent_fds(void)
  145. int main(int argc, char *argv[])
  146. {
  147. ledmon_status_code_t status = LEDMON_STATUS_SUCCESS;
  148. - static int ignore;
  149. setup_options(&longopt, &shortopt, possible_params,
  150. possible_params_size);
  151. set_invocation_name(argv[0]);
  152. openlog(progname, LOG_PID | LOG_PERROR, LOG_DAEMON);
  153. - if (on_exit(_ledmon_status, &ignore))
  154. + if (atexit(_ledmon_status))
  155. return LEDMON_STATUS_ONEXIT_ERROR;
  156. if (_cmdline_parse_non_daemonise(argc, argv) != LEDMON_STATUS_SUCCESS)
  157. @@ -935,18 +938,18 @@ int main(int argc, char *argv[])
  158. if (pid < 0) {
  159. log_debug("main(): fork() failed (errno=%d).", errno);
  160. - exit(EXIT_FAILURE);
  161. + EXIT(EXIT_FAILURE);
  162. }
  163. if (pid > 0) {
  164. ignore = 1; /* parent: don't print exit status */
  165. - exit(EXIT_SUCCESS);
  166. + EXIT(EXIT_SUCCESS);
  167. }
  168. pid_t sid = setsid();
  169. if (sid < 0) {
  170. log_debug("main(): setsid() failed (errno=%d).", errno);
  171. - exit(EXIT_FAILURE);
  172. + EXIT(EXIT_FAILURE);
  173. }
  174. _close_parent_fds();
  175. @@ -960,16 +963,16 @@ int main(int argc, char *argv[])
  176. if (chdir("/") < 0) {
  177. log_debug("main(): chdir() failed (errno=%d).", errno);
  178. - exit(EXIT_FAILURE);
  179. + EXIT(EXIT_FAILURE);
  180. }
  181. if (pidfile_create(progname)) {
  182. log_debug("main(): pidfile_creat() failed.");
  183. - exit(EXIT_FAILURE);
  184. + EXIT(EXIT_FAILURE);
  185. }
  186. _ledmon_setup_signals();
  187. - if (on_exit(_ledmon_fini, progname))
  188. - exit(LEDMON_STATUS_ONEXIT_ERROR);
  189. + if (atexit(_ledmon_fini))
  190. + EXIT(LEDMON_STATUS_ONEXIT_ERROR);
  191. list_init(&ledmon_block_list, (item_free_t)block_device_fini);
  192. sysfs_init();
  193. log_info("monitor service has been started...");
  194. @@ -987,5 +990,5 @@ int main(int argc, char *argv[])
  195. }
  196. ledmon_remove_shared_conf();
  197. stop_udev_monitor();
  198. - exit(EXIT_SUCCESS);
  199. + EXIT(EXIT_SUCCESS);
  200. }
  201. --
  202. 2.39.2