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