rcS.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <glob.h>
  26. #include <libubox/ustream.h>
  27. #include "procd.h"
  28. #include "rcS.h"
  29. static struct runqueue q, r;
  30. struct initd {
  31. struct ustream_fd fd;
  32. struct runqueue_process proc;
  33. char *file;
  34. char *param;
  35. };
  36. static void pipe_cb(struct ustream *s, int bytes)
  37. {
  38. struct ustream_buf *buf = s->r.head;
  39. char *newline, *str;
  40. int len;
  41. do {
  42. str = ustream_get_read_buf(s, NULL);
  43. if (!str)
  44. break;
  45. newline = strchr(buf->data, '\n');
  46. if (!newline)
  47. break;
  48. *newline = 0;
  49. len = newline + 1 - str;
  50. log_printf(buf->data);
  51. ustream_consume(s, len);
  52. } while (1);
  53. }
  54. static void q_initd_run(struct runqueue *q, struct runqueue_task *t)
  55. {
  56. struct initd *s = container_of(t, struct initd, proc.task);
  57. int pipefd[2];
  58. pid_t pid;
  59. DEBUG(1, "start %s %s \n", s->file, s->param);
  60. if (pipe(pipefd) == -1) {
  61. ERROR("Failed to create pipe\n");
  62. return;
  63. }
  64. pid = fork();
  65. if (pid < 0)
  66. return;
  67. if (pid) {
  68. close(pipefd[1]);
  69. s->fd.stream.string_data = true,
  70. s->fd.stream.notify_read = pipe_cb,
  71. runqueue_process_add(q, &s->proc, pid);
  72. ustream_fd_init(&s->fd, pipefd[0]);
  73. return;
  74. }
  75. close(pipefd[0]);
  76. dup2(pipefd[1], STDOUT_FILENO);
  77. dup2(pipefd[1], STDERR_FILENO);
  78. execlp(s->file, s->file, s->param, NULL);
  79. exit(1);
  80. }
  81. static void q_initd_complete(struct runqueue *q, struct runqueue_task *p)
  82. {
  83. struct initd *s = container_of(p, struct initd, proc.task);
  84. DEBUG(1, "stop %s %s \n", s->file, s->param);
  85. ustream_free(&s->fd.stream);
  86. close(s->fd.fd.fd);
  87. free(s);
  88. }
  89. static void add_initd(struct runqueue *q, char *file, char *param)
  90. {
  91. static const struct runqueue_task_type initd_type = {
  92. .run = q_initd_run,
  93. .cancel = runqueue_process_cancel_cb,
  94. .kill = runqueue_process_kill_cb,
  95. };
  96. struct initd *s;
  97. s = calloc(1, sizeof(*s));
  98. s->proc.task.type = &initd_type;
  99. s->proc.task.complete = q_initd_complete;
  100. s->param = param;
  101. s->file = file;
  102. runqueue_task_add(q, &s->proc.task, false);
  103. }
  104. static int _rc(struct runqueue *q, char *path, const char *file, char *pattern, char *param)
  105. {
  106. char dir[64];
  107. glob_t gl;
  108. int j;
  109. DEBUG(1, "running %s/%s%s %s\n", path, file, pattern, param);
  110. snprintf(dir, sizeof(dir), "%s/%s%s", path, file, pattern);
  111. if (glob(dir, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) {
  112. printf("glob failed on %s\n", dir);
  113. return -1;
  114. }
  115. for (j = 0; j < gl.gl_pathc; j++)
  116. add_initd(q, gl.gl_pathv[j], param);
  117. return 0;
  118. }
  119. int rcS(char *pattern, char *param, void (*q_empty)(struct runqueue *))
  120. {
  121. runqueue_init(&q);
  122. q.empty_cb = q_empty;
  123. q.max_running_tasks = 1;
  124. return _rc(&q, "/etc/rc.d", pattern, "*", param);
  125. }
  126. int rc(const char *file, char *param)
  127. {
  128. return _rc(&r, "/etc/init.d", file, "", param);
  129. }
  130. static void r_empty(struct runqueue *q)
  131. {
  132. }
  133. static void __attribute__((constructor)) measure_init() {
  134. runqueue_init(&r);
  135. r.empty_cb = r_empty;
  136. r.max_running_tasks = 1;
  137. }