• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt