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