25 #include <libubox/list.h>
26 #include <libubox/blobmsg_json.h>
30 struct list_head
list;
43 static struct ubus_context *
ctx;
44 static struct blob_buf
b;
63 regmatch_t matches[2];
67 fp = fopen(file,
"r");
69 fprintf(stderr,
"failed to open %s\n", file);
72 len = fread(buffer, 1,
sizeof(buffer) - 1, fp);
76 fprintf(stderr,
"failed to read from %s\n", file);
81 i = calloc(1,
sizeof(
struct initd));
83 fprintf(stderr,
"failed to alloc initd struct\n");
88 i->
name = strndup(buffer + matches[1].rm_so, (
size_t)matches[1].rm_eo - matches[1].rm_so);
89 if (!regexec(&
pat_exec, buffer, 2, matches, 0))
90 i->
exec = strndup(buffer + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
91 if (!regexec(&
pat_desc, buffer, 2, matches, 0))
92 i->
desc = strndup(buffer + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
93 if (!regexec(&
pat_tpl, buffer, 2, matches, 0))
94 i->
tpl = strndup(buffer + matches[1].rm_so, matches[1].rm_eo - matches[1].rm_so);
95 if (!regexec(&
pat_start, buffer, 2, matches, 0))
96 i->
start += atoi(buffer + matches[1].rm_so);
97 if (!regexec(&
pat_stop, buffer, 2, matches, 0))
98 i->
stop += atoi(buffer + matches[1].rm_so);
101 list_add(&i->
list, &initds);
110 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
113 regcomp(&
pat_provides,
"# Provides:[ \t]*([a-zA-Z0-9]+)", REG_EXTENDED);
114 regcomp(&
pat_require,
"# Required-Start:[ \t]*([a-zA-Z0-9 ]+)", REG_EXTENDED);
115 regcomp(&
pat_start,
"# Default-Start:[ \t]*([0-9])", REG_EXTENDED);
116 regcomp(&
pat_stop,
"# Default-Stop:[ \t]*([0-9])", REG_EXTENDED);
117 regcomp(&
pat_desc,
"# Description:[ \t]*([a-zA-Z0-9 ]+)", REG_EXTENDED);
118 regcomp(&
pat_exec,
"# X-Exec:[ \t]*([a-zA-Z0-9/ ]+)", REG_EXTENDED);
119 regcomp(&
pat_tpl,
"# X-Template:[ \t]*([a-zA-Z0-9/.]+)", REG_EXTENDED);
121 if (glob(
"/etc/rc.d/P*", gl_flags, NULL, &gl) >= 0) {
123 for (j = 0; j < gl.gl_pathc; j++)
140 void *instances, *instance, *command;
142 list_for_each_entry(i, &initds,
list) {
145 blob_buf_init(&
b, 0);
146 blobmsg_add_string(&
b,
"name", i->
name);
147 instances = blobmsg_open_table(&
b,
"instances");
148 instance = blobmsg_open_table(&
b,
"instance");
149 command = blobmsg_open_array(&
b,
"command");
150 t = strtok(i->
exec,
" ");
152 blobmsg_add_string(&
b, NULL, t);
153 t = strtok(NULL,
" ");
155 blobmsg_close_array(&
b, command);
156 blobmsg_close_table(&
b, instance);
157 blobmsg_close_table(&
b, instances);
158 ubus_invoke(
ctx,
service,
"add",
b.head, NULL, 0, 1000);
164 int main(
int argc,
char **argv)
170 if (list_empty(&initds))
173 ctx = ubus_connect(NULL);
175 fprintf(stderr,
"Failed to connect to ubus\n");
179 ret = ubus_lookup_id(
ctx,
"service", &
service);
181 fprintf(stderr,
"Failed to find service object: %s\n", ubus_strerror(ret));
static void initd_init(void)
static regex_t pat_require
int main(int argc, char **argv)
static int initd_parse(const char *file)
static struct ubus_context * ctx
static regex_t pat_provides
static void initd_free(struct initd *i)
static int init_services(void)