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