rcS.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 <stdlib.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <glob.h>
  27. #include <libubox/ustream.h>
  28. #include "procd.h"
  29. #include "rcS.h"
  30. static struct runqueue q, r;
  31. struct initd {
  32. struct ustream_fd fd;
  33. struct runqueue_process proc;
  34. struct timespec ts_start;
  35. char *file;
  36. char *param;
  37. };
  38. static void pipe_cb(struct ustream *s, int bytes)
  39. {
  40. struct initd *initd = container_of(s, struct initd, fd.stream);
  41. char *newline, *str;
  42. int len;
  43. do {
  44. str = ustream_get_read_buf(s, NULL);
  45. if (!str)
  46. break;
  47. newline = strchr(str, '\n');
  48. if (!newline)
  49. break;
  50. *newline = 0;
  51. len = newline + 1 - str;
  52. ULOG_NOTE("%s: %s", initd->file, str);
  53. #ifdef SHOW_BOOT_ON_CONSOLE
  54. fprintf(stderr, "%s: %s\n", initd->file, str);
  55. #endif
  56. ustream_consume(s, len);
  57. } while (1);
  58. }
  59. static void q_initd_run(struct runqueue *q, struct runqueue_task *t)
  60. {
  61. struct initd *s = container_of(t, struct initd, proc.task);
  62. int pipefd[2];
  63. pid_t pid;
  64. clock_gettime(CLOCK_MONOTONIC_RAW, &s->ts_start);
  65. DEBUG(2, "start %s %s \n", s->file, s->param);
  66. if (pipe(pipefd) == -1) {
  67. ERROR("Failed to create pipe: %m\n");
  68. return;
  69. }
  70. pid = fork();
  71. if (pid < 0)
  72. return;
  73. if (pid) {
  74. close(pipefd[1]);
  75. fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
  76. s->fd.stream.string_data = true,
  77. s->fd.stream.notify_read = pipe_cb,
  78. runqueue_process_add(q, &s->proc, pid);
  79. ustream_fd_init(&s->fd, pipefd[0]);
  80. return;
  81. }
  82. close(pipefd[0]);
  83. int devnull = open("/dev/null", O_RDONLY);
  84. dup2(devnull, STDIN_FILENO);
  85. dup2(pipefd[1], STDOUT_FILENO);
  86. dup2(pipefd[1], STDERR_FILENO);
  87. if (devnull > STDERR_FILENO)
  88. close(devnull);
  89. execlp(s->file, s->file, s->param, NULL);
  90. exit(1);
  91. }
  92. static void q_initd_complete(struct runqueue *q, struct runqueue_task *p)
  93. {
  94. struct initd *s = container_of(p, struct initd, proc.task);
  95. struct timespec ts_stop, ts_res;
  96. clock_gettime(CLOCK_MONOTONIC_RAW, &ts_stop);
  97. ts_res.tv_sec = ts_stop.tv_sec - s->ts_start.tv_sec;
  98. ts_res.tv_nsec = ts_stop.tv_nsec - s->ts_start.tv_nsec;
  99. if (ts_res.tv_nsec < 0) {
  100. --ts_res.tv_sec;
  101. ts_res.tv_nsec += 1000000000;
  102. }
  103. DEBUG(2, "stop %s %s - took %lu.%09lus\n", s->file, s->param, ts_res.tv_sec, ts_res.tv_nsec);
  104. ustream_free(&s->fd.stream);
  105. close(s->fd.fd.fd);
  106. free(s);
  107. }
  108. static void add_initd(struct runqueue *q, char *file, char *param)
  109. {
  110. static const struct runqueue_task_type initd_type = {
  111. .run = q_initd_run,
  112. .cancel = runqueue_process_cancel_cb,
  113. .kill = runqueue_process_kill_cb,
  114. };
  115. struct initd *s;
  116. char *p, *f;
  117. s = calloc_a(sizeof(*s), &f, strlen(file) + 1, &p, strlen(param) + 1);
  118. if (!s) {
  119. ERROR("Out of memory in %s.\n", file);
  120. return;
  121. }
  122. s->proc.task.type = &initd_type;
  123. s->proc.task.complete = q_initd_complete;
  124. if (!strcmp(param, "stop") || !strcmp(param, "shutdown")) {
  125. s->proc.task.run_timeout = 15000;
  126. s->proc.task.cancel_timeout = 10000;
  127. }
  128. s->param = p;
  129. s->file = f;
  130. strcpy(s->param, param);
  131. strcpy(s->file, file);
  132. runqueue_task_add(q, &s->proc.task, false);
  133. }
  134. static int _rc(struct runqueue *q, char *path, const char *file, char *pattern, char *param)
  135. {
  136. char *dir = alloca(2 + strlen(path) + strlen(file) + strlen(pattern));
  137. glob_t gl;
  138. int j;
  139. if (!dir) {
  140. ERROR("Out of memory in %s.\n", file);
  141. return -1;
  142. }
  143. DEBUG(2, "running %s/%s%s %s\n", path, file, pattern, param);
  144. sprintf(dir, "%s/%s%s", path, file, pattern);
  145. if (glob(dir, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
  146. DEBUG(2, "glob failed on %s\n", dir);
  147. return -1;
  148. }
  149. for (j = 0; j < gl.gl_pathc; j++)
  150. add_initd(q, gl.gl_pathv[j], param);
  151. globfree(&gl);
  152. return 0;
  153. }
  154. int rcS(char *pattern, char *param, void (*q_empty)(struct runqueue *))
  155. {
  156. runqueue_init(&q);
  157. q.empty_cb = q_empty;
  158. q.max_running_tasks = 1;
  159. return _rc(&q, "/etc/rc.d", pattern, "*", param);
  160. }
  161. int rc(const char *file, char *param)
  162. {
  163. return _rc(&r, "/etc/init.d", file, "", param);
  164. }
  165. static void r_empty(struct runqueue *q)
  166. {
  167. }
  168. static void __attribute__((constructor)) rc_init() {
  169. runqueue_init(&r);
  170. r.empty_cb = r_empty;
  171. r.max_running_tasks = 8;
  172. }