2
0

rcS.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * runqueue-example.c
  3. *
  4. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <libubox/uloop.h>
  19. #include <libubox/runqueue.h>
  20. #include <inttypes.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <glob.h>
  28. #include <libubox/ustream.h>
  29. #include "procd.h"
  30. #include "rcS.h"
  31. static struct runqueue q, r;
  32. struct initd {
  33. struct ustream_fd fd;
  34. struct runqueue_process proc;
  35. struct timespec ts_start;
  36. char *file;
  37. char *param;
  38. };
  39. static void pipe_cb(struct ustream *s, int bytes)
  40. {
  41. struct initd *initd = container_of(s, struct initd, fd.stream);
  42. char *newline, *str;
  43. int len;
  44. do {
  45. str = ustream_get_read_buf(s, NULL);
  46. if (!str)
  47. break;
  48. newline = strchr(str, '\n');
  49. if (!newline)
  50. break;
  51. *newline = 0;
  52. len = newline + 1 - str;
  53. ULOG_NOTE("%s: %s", initd->file, str);
  54. #ifdef SHOW_BOOT_ON_CONSOLE
  55. fprintf(stderr, "%s: %s\n", initd->file, str);
  56. #endif
  57. ustream_consume(s, len);
  58. } while (1);
  59. }
  60. static void q_initd_run(struct runqueue *q, struct runqueue_task *t)
  61. {
  62. struct initd *s = container_of(t, struct initd, proc.task);
  63. int pipefd[2];
  64. pid_t pid;
  65. clock_gettime(CLOCK_MONOTONIC_RAW, &s->ts_start);
  66. DEBUG(2, "start %s %s \n", s->file, s->param);
  67. if (pipe(pipefd) == -1) {
  68. ERROR("Failed to create pipe: %m\n");
  69. return;
  70. }
  71. pid = fork();
  72. if (pid < 0)
  73. return;
  74. if (pid) {
  75. close(pipefd[1]);
  76. fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
  77. s->fd.stream.string_data = true,
  78. s->fd.stream.notify_read = pipe_cb,
  79. runqueue_process_add(q, &s->proc, pid);
  80. ustream_fd_init(&s->fd, pipefd[0]);
  81. return;
  82. }
  83. close(pipefd[0]);
  84. int devnull = open("/dev/null", O_RDONLY);
  85. dup2(devnull, STDIN_FILENO);
  86. dup2(pipefd[1], STDOUT_FILENO);
  87. dup2(pipefd[1], STDERR_FILENO);
  88. if (devnull > STDERR_FILENO)
  89. close(devnull);
  90. execlp(s->file, s->file, s->param, NULL);
  91. exit(1);
  92. }
  93. static void q_initd_complete(struct runqueue *q, struct runqueue_task *p)
  94. {
  95. struct initd *s = container_of(p, struct initd, proc.task);
  96. struct timespec ts_stop, ts_res;
  97. clock_gettime(CLOCK_MONOTONIC_RAW, &ts_stop);
  98. ts_res.tv_sec = ts_stop.tv_sec - s->ts_start.tv_sec;
  99. ts_res.tv_nsec = ts_stop.tv_nsec - s->ts_start.tv_nsec;
  100. if (ts_res.tv_nsec < 0) {
  101. --ts_res.tv_sec;
  102. ts_res.tv_nsec += 1000000000;
  103. }
  104. DEBUG(2, "stop %s %s - took %" PRId64 ".%09" PRId64 "s\n", s->file, s->param, (int64_t)ts_res.tv_sec, (int64_t)ts_res.tv_nsec);
  105. ustream_free(&s->fd.stream);
  106. close(s->fd.fd.fd);
  107. free(s);
  108. }
  109. static void add_initd(struct runqueue *q, char *file, char *param)
  110. {
  111. static const struct runqueue_task_type initd_type = {
  112. .run = q_initd_run,
  113. .cancel = runqueue_process_cancel_cb,
  114. .kill = runqueue_process_kill_cb,
  115. };
  116. struct initd *s;
  117. char *p, *f;
  118. s = calloc_a(sizeof(*s), &f, strlen(file) + 1, &p, strlen(param) + 1);
  119. if (!s) {
  120. ERROR("Out of memory in %s.\n", file);
  121. return;
  122. }
  123. s->proc.task.type = &initd_type;
  124. s->proc.task.complete = q_initd_complete;
  125. if (!strcmp(param, "stop") || !strcmp(param, "shutdown")) {
  126. s->proc.task.run_timeout = 15000;
  127. s->proc.task.cancel_timeout = 10000;
  128. }
  129. s->param = p;
  130. s->file = f;
  131. strcpy(s->param, param);
  132. strcpy(s->file, file);
  133. runqueue_task_add(q, &s->proc.task, false);
  134. }
  135. static int _rc(struct runqueue *q, char *path, const char *file, char *pattern, char *param)
  136. {
  137. char *dir = alloca(2 + strlen(path) + strlen(file) + strlen(pattern));
  138. glob_t gl;
  139. int j;
  140. if (!dir) {
  141. ERROR("Out of memory in %s.\n", file);
  142. return -1;
  143. }
  144. DEBUG(2, "running %s/%s%s %s\n", path, file, pattern, param);
  145. sprintf(dir, "%s/%s%s", path, file, pattern);
  146. if (glob(dir, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
  147. DEBUG(2, "glob failed on %s\n", dir);
  148. return -1;
  149. }
  150. for (j = 0; j < gl.gl_pathc; j++)
  151. add_initd(q, gl.gl_pathv[j], param);
  152. globfree(&gl);
  153. return 0;
  154. }
  155. int rcS(char *pattern, char *param, void (*q_empty)(struct runqueue *))
  156. {
  157. runqueue_init(&q);
  158. q.empty_cb = q_empty;
  159. q.max_running_tasks = 1;
  160. return _rc(&q, "/etc/rc.d", pattern, "*", param);
  161. }
  162. int rc(const char *file, char *param)
  163. {
  164. return _rc(&r, "/etc/init.d", file, "", param);
  165. }
  166. static void r_empty(struct runqueue *q)
  167. {
  168. }
  169. static void __attribute__((constructor)) rc_init() {
  170. runqueue_init(&r);
  171. r.empty_cb = r_empty;
  172. r.max_running_tasks = 8;
  173. }