4 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
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.
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.
19 #include <libubox/uloop.h>
20 #include <libubox/runqueue.h>
25 #include <sys/types.h>
29 #include <libubox/ustream.h>
34 static struct runqueue q
, r
;
38 struct runqueue_process proc
;
43 static void pipe_cb(struct ustream
*s
, int bytes
)
49 str
= ustream_get_read_buf(s
, NULL
);
52 newline
= strchr(str
, '\n');
56 len
= newline
+ 1 - str
;
58 #ifdef SHOW_BOOT_ON_CONSOLE
59 fprintf(stderr
, "%s\n", str
);
61 ustream_consume(s
, len
);
65 static void q_initd_run(struct runqueue
*q
, struct runqueue_task
*t
)
67 struct initd
*s
= container_of(t
, struct initd
, proc
.task
);
71 DEBUG(2, "start %s %s \n", s
->file
, s
->param
);
72 if (pipe(pipefd
) == -1) {
73 ERROR("Failed to create pipe\n");
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]);
90 dup2(pipefd
[1], STDOUT_FILENO
);
91 dup2(pipefd
[1], STDERR_FILENO
);
93 execlp(s
->file
, s
->file
, s
->param
, NULL
);
97 static void q_initd_complete(struct runqueue
*q
, struct runqueue_task
*p
)
99 struct initd
*s
= container_of(p
, struct initd
, proc
.task
);
101 DEBUG(2, "stop %s %s \n", s
->file
, s
->param
);
102 ustream_free(&s
->fd
.stream
);
107 static void add_initd(struct runqueue
*q
, char *file
, char *param
)
109 static const struct runqueue_task_type initd_type
= {
111 .cancel
= runqueue_process_cancel_cb
,
112 .kill
= runqueue_process_kill_cb
,
117 s
= calloc_a(sizeof(*s
), &f
, strlen(file
) + 1, &p
, strlen(param
) + 1);
118 s
->proc
.task
.type
= &initd_type
;
119 s
->proc
.task
.complete
= q_initd_complete
;
120 if (!strcmp(param
, "stop") || !strcmp(param
, "shutdown"))
121 s
->proc
.task
.run_timeout
= 15000;
124 strcpy(s
->param
, param
);
125 strcpy(s
->file
, file
);
126 runqueue_task_add(q
, &s
->proc
.task
, false);
129 static int _rc(struct runqueue
*q
, char *path
, const char *file
, char *pattern
, char *param
)
131 char *dir
= alloca(2 + strlen(path
) + strlen(file
) + strlen(pattern
));
135 DEBUG(2, "running %s/%s%s %s\n", path
, file
, pattern
, param
);
136 sprintf(dir
, "%s/%s%s", path
, file
, pattern
);
137 if (glob(dir
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
)) {
138 DEBUG(2, "glob failed on %s\n", dir
);
142 for (j
= 0; j
< gl
.gl_pathc
; j
++)
143 add_initd(q
, gl
.gl_pathv
[j
], param
);
150 int rcS(char *pattern
, char *param
, void (*q_empty
)(struct runqueue
*))
153 q
.empty_cb
= q_empty
;
154 q
.max_running_tasks
= 1;
156 return _rc(&q
, "/etc/rc.d", pattern
, "*", param
);
159 int rc(const char *file
, char *param
)
161 return _rc(&r
, "/etc/init.d", file
, "", param
);
164 static void r_empty(struct runqueue
*q
)
169 static void __attribute__((constructor
)) rc_init() {
171 r
.empty_cb
= r_empty
;
172 r
.max_running_tasks
= 8;