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