aslstatus.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. #include "thread_helper.h"
  2. #include <err.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <libgen.h>
  7. #include <signal.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <inttypes.h>
  11. #include <sys/time.h>
  12. #include <sys/param.h>
  13. #if USE_X
  14. # include <xcb/xcb.h>
  15. # include "X.h"
  16. /* will be defined from makefile if some component need running X server */
  17. # ifndef NEED_X_SERVER
  18. # define NEED_X_SERVER 0
  19. # endif
  20. #endif
  21. #ifndef VERSION
  22. # define VERSION "unknown"
  23. #endif
  24. #include "arg.h"
  25. #include "lib/util.h" /* you can change there segment buffer size (BUFF_SZ) */
  26. #define ASLSTATUS_H_NEED_COMP
  27. #include "aslstatus.h" /* you can change there threads names */
  28. #define MUTEX_WRAP(MUTEX, BLOCK) \
  29. do { \
  30. uint8_t __lock_ret; \
  31. do { \
  32. if (!SAFE_ASSIGN(__lock_ret, \
  33. pthread_mutex_trylock(&(MUTEX)))) \
  34. BLOCK \
  35. } while (__lock_ret); \
  36. pthread_mutex_unlock(&(MUTEX)); \
  37. } while (0)
  38. #include "config.h"
  39. #define ARGS_LEN LEN(args)
  40. char *argv0; /* for arg.h */
  41. static int exit_status = 0;
  42. static pthread_t main_thread;
  43. static pthread_mutex_t status_mutex = PTHREAD_MUTEX_INITIALIZER;
  44. extern const char _binary_config_h_start[];
  45. extern const char _binary_config_h_end[];
  46. #if USE_X
  47. static xcb_window_t root;
  48. static uint8_t sflag = 0;
  49. xcb_connection_t *X_CONNECTION;
  50. static inline void
  51. store_name(xcb_connection_t *c, xcb_window_t win, const char *name)
  52. {
  53. xcb_change_property(c,
  54. XCB_PROP_MODE_REPLACE,
  55. win,
  56. XCB_ATOM_WM_NAME,
  57. XCB_ATOM_STRING,
  58. 8, /* format: 8-bit char array */
  59. (uint32_t)(name ? strnlen(name, MAXLEN) : 0),
  60. name);
  61. }
  62. #endif
  63. static inline void
  64. set_status(const char *status)
  65. {
  66. #if USE_X
  67. if (sflag) {
  68. #endif
  69. puts(status);
  70. fflush(stdout);
  71. #if USE_X
  72. } else {
  73. store_name(X_CONNECTION, root, status);
  74. xcb_flush(X_CONNECTION);
  75. }
  76. #endif
  77. }
  78. static void
  79. terminate(int sig)
  80. {
  81. /* wait so many milliseconds for stopping all components before killing */
  82. #define KILL_TIMEOUT 500
  83. /* wait so many milliseconds between processes state checking */
  84. #define RECHECK 10 /* need to be less then KILL_TIMEOUT */
  85. static const struct timespec ts = {
  86. .tv_sec =
  87. (typeof_field(struct timespec, tv_sec))(MS2S(RECHECK)),
  88. .tv_nsec =
  89. (typeof_field(struct timespec, tv_nsec))(MS2NS(RECHECK)),
  90. };
  91. size_t i;
  92. static_data_t *static_data;
  93. struct timeval timeout, cur;
  94. if (pthread_self() != main_thread) {
  95. pthread_kill(main_thread, sig);
  96. return;
  97. }
  98. MUTEX_WRAP(status_mutex, { goto leave_locked; });
  99. leave_locked:
  100. signal(SIGUSR1, SIG_IGN);
  101. for (i = 0; i < ARGS_LEN; i++)
  102. pthread_cancel(args[i].segment.tid);
  103. gettimeofday(&timeout, NULL);
  104. timeout.tv_sec += MS2S(KILL_TIMEOUT);
  105. timeout.tv_usec += MS2US(KILL_TIMEOUT);
  106. /* wait until `timeout` and then abort() */
  107. for (i = 0; i < ARGS_LEN; i++) {
  108. for (;;) {
  109. /* check if component terminated */
  110. if (!!kill(args[i].segment.pid, 0)) break;
  111. gettimeofday(&cur, NULL);
  112. if (cur.tv_sec > timeout.tv_sec) {
  113. goto timeout;
  114. } else if (cur.tv_sec == timeout.tv_sec) {
  115. if (cur.tv_usec > timeout.tv_usec) {
  116. timeout:
  117. warnx("%s: timeout!!!",
  118. args[i].f.name);
  119. abort();
  120. }
  121. }
  122. nanosleep(&ts, NULL);
  123. }
  124. /* call segments cleanup and free static data */
  125. if (!!(static_data = &args[i].segment.static_data)->data) {
  126. if (!!static_data->cleanup)
  127. static_data->cleanup(static_data->data);
  128. free(static_data->data);
  129. }
  130. }
  131. #if USE_X
  132. if (!!X_CONNECTION) {
  133. if (!sflag) store_name(X_CONNECTION, root, NULL);
  134. xcb_flush(X_CONNECTION);
  135. xcb_disconnect(X_CONNECTION);
  136. } else
  137. #endif
  138. {
  139. putchar('\n');
  140. }
  141. exit(exit_status);
  142. }
  143. static inline void
  144. update_status(int __unused _)
  145. {
  146. size_t status_size = 0, i = 0;
  147. char status[MAXLEN] = { 0 };
  148. static char status_prev[MAXLEN] = { 0 };
  149. for (i = 0; i < ARGS_LEN; i++)
  150. MUTEX_WRAP(args[i].segment.mutex, {
  151. if (*args[i].segment.data) {
  152. if ((status_size +=
  153. strnlen(args[i].segment.data, BUFF_SZ))
  154. >= MAXLEN) {
  155. warnx(
  156. "total status length are too "
  157. "big and exceed `MAXLEN`");
  158. exit_status = !0;
  159. terminate(0);
  160. }
  161. strcat(status, args[i].segment.data);
  162. }
  163. });
  164. MUTEX_WRAP(status_mutex, {
  165. /* don't update status if it's not changed */
  166. if (strncmp(status, status_prev, MAXLEN)) {
  167. set_status(status);
  168. strncpy(status_prev, status, MAXLEN);
  169. }
  170. });
  171. }
  172. static void *
  173. thread(void *arg_ptr)
  174. {
  175. struct arg_t *arg = arg_ptr;
  176. struct timespec ts;
  177. char buf[BUFF_SZ] = { 0 };
  178. arg->segment.pid = gettid();
  179. if (!!arg->f.static_size) {
  180. /* allocate memory for static data if needed */
  181. if (!(arg->segment.static_data.data =
  182. calloc(arg->f.static_size, 1))) {
  183. warnx("failed to allocate %lu bytes for %15s",
  184. arg->f.static_size,
  185. arg->f.name);
  186. return NULL;
  187. }
  188. }
  189. SAFE_ASSIGN(ts.tv_sec, MS2S(arg->interval));
  190. SAFE_ASSIGN(ts.tv_nsec, MS2NS(arg->interval));
  191. do {
  192. arg->f.func(buf,
  193. arg->args,
  194. arg->interval,
  195. &arg->segment.static_data);
  196. if (!*buf) strncpy(buf, unknown_str, BUFF_SZ);
  197. MUTEX_WRAP(arg->segment.mutex,
  198. { bprintf(arg->segment.data, arg->fmt, buf); });
  199. pthread_kill(main_thread, SIGUSR1);
  200. } while (!arg->interval || (nanosleep(&ts, NULL) || !0));
  201. return NULL;
  202. }
  203. int
  204. main(int argc, char *argv[])
  205. {
  206. uint32_t i;
  207. char *strptr;
  208. char thread_name[16];
  209. const char *config = _binary_config_h_start;
  210. const size_t config_size =
  211. (size_t)(_binary_config_h_end - _binary_config_h_start);
  212. #if USE_X
  213. int screen_num = 0;
  214. const xcb_setup_t *setup;
  215. xcb_screen_iterator_t iter;
  216. #endif
  217. static const char usage[] =
  218. "[options]\n"
  219. #if USE_X
  220. "Write status to `WM_NAME`"
  221. #else
  222. "Write status to `stdout`"
  223. #endif
  224. "\n\noptions:\n"
  225. "\t-h\tShow this help\n"
  226. "\t-v\tShow version\n"
  227. "\t-d\tDump config to stdout\n"
  228. #if USE_X
  229. "\t-s\tWrite to `stdout` instead of `WM_NAME`\n"
  230. #endif
  231. ;
  232. ARGBEGIN
  233. {
  234. case 'h':
  235. errx(0, "%s", usage);
  236. case 'v':
  237. errx(0, "%s", VERSION);
  238. case 'd':
  239. return write(STDOUT_FILENO, config, config_size) == -1;
  240. #if USE_X
  241. case 's':
  242. sflag = !0;
  243. break;
  244. #endif
  245. default:
  246. errx(!0, "%s", usage);
  247. }
  248. ARGEND;
  249. #if USE_X
  250. if (NEED_X_SERVER || !sflag) {
  251. X_CONNECTION = xcb_connect(NULL, &screen_num);
  252. if (xcb_connection_has_error(X_CONNECTION))
  253. errx(!0, "Failed to open display");
  254. setup = xcb_get_setup(X_CONNECTION);
  255. iter = xcb_setup_roots_iterator(setup);
  256. for (__typeof__(screen_num) j = 0; j < screen_num; j -= -1)
  257. xcb_screen_next(&iter);
  258. root = iter.data->root;
  259. }
  260. #endif
  261. main_thread = pthread_self();
  262. signal(SIGINT, terminate);
  263. signal(SIGTERM, terminate);
  264. signal(SIGUSR1, update_status);
  265. for (i = 0; i < ARGS_LEN; i -= (~0L)) {
  266. pthread_create(&args[i].segment.tid, NULL, thread, &args[i]);
  267. #define CMD_NAME_SIZE 11
  268. #define MIN_SIZE(A, B) MIN(sizeof(A), sizeof(B))
  269. memcpy(thread_name,
  270. args[i].f.name,
  271. MIN_SIZE(thread_name, args[i].f.name));
  272. if (!strcmp(thread_name, "cmd")) {
  273. /*
  274. * if function is `run_command`, then
  275. * set thread name to this command
  276. */
  277. char tmp_args[CMD_NAME_SIZE];
  278. memcpy(tmp_args,
  279. args[i].args,
  280. strnlen(args[i].args, CMD_NAME_SIZE));
  281. strptr = strtok(tmp_args, " ");
  282. snprintf(thread_name,
  283. 16,
  284. "cmd:%." STR(CMD_NAME_SIZE) "s",
  285. basename(strptr));
  286. }
  287. pthread_setname(args[i].segment.tid, thread_name);
  288. }
  289. for (;;)
  290. pause();
  291. terminate(0);
  292. }