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