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