diff options
Diffstat (limited to 'urunlevel/runlevel/start_daemon.c')
-rw-r--r-- | urunlevel/runlevel/start_daemon.c | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/urunlevel/runlevel/start_daemon.c b/urunlevel/runlevel/start_daemon.c new file mode 100644 index 0000000..ba922fa --- /dev/null +++ b/urunlevel/runlevel/start_daemon.c | |||
@@ -0,0 +1,153 @@ | |||
1 | /* | ||
2 | * Mini start_daemon implementation for busybox. | ||
3 | * | ||
4 | * Copyright (C) 2005 by David Seikel won_fang@yahoo.com.au | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | ||
19 | * 02111-1307 USA | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | #include <errno.h> | ||
24 | #include <getopt.h> | ||
25 | #include <string.h> | ||
26 | #include <sys/time.h> | ||
27 | #include <sys/resource.h> | ||
28 | #include <unistd.h> | ||
29 | |||
30 | #include "busybox.h" | ||
31 | #include "lib_init_d.h" | ||
32 | |||
33 | |||
34 | int start_daemon(int force, int nice_level, char *pidfile, char *pathname, char *args) | ||
35 | { | ||
36 | int status = 0; | ||
37 | status = pidofproc(pidfile, pathname, NULL); | ||
38 | |||
39 | if ((status != INIT_D_STATUS_OK) || (force)) | ||
40 | { | ||
41 | char *pids = NULL; | ||
42 | char *strtok_temp; | ||
43 | |||
44 | doit(DAEMON, "start-stop-daemon -Sbmp %s -x %s -- %s", pidfile, pathname, args); | ||
45 | sleep(1); | ||
46 | status = pidofproc(pidfile, pathname, &pids); | ||
47 | if ((status == INIT_D_STATUS_OK) && (nice_level)) | ||
48 | { | ||
49 | int i; | ||
50 | char *pid; | ||
51 | |||
52 | pid = strtok_r(pids, " ", &strtok_temp); | ||
53 | for (i = 0; pid != NULL; i++) | ||
54 | { | ||
55 | int ps = atoi(pid); | ||
56 | int oldp; | ||
57 | |||
58 | errno = 0; | ||
59 | oldp = getpriority(PRIO_PROCESS, ps); | ||
60 | if (errno == 0) | ||
61 | setpriority(PRIO_PROCESS, ps, oldp + nice_level); | ||
62 | pid = strtok_r(NULL, " ", &strtok_temp); | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | |||
67 | return status; | ||
68 | } | ||
69 | |||
70 | |||
71 | /* | ||
72 | start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args] | ||
73 | This runs the specified program as a daemon. | ||
74 | start_daemon shall check if the program is already running | ||
75 | using the algorithm given above. If so, it shall not | ||
76 | start another copy of the daemon unless the -f | ||
77 | option is given. The -n option specifies a nice | ||
78 | level. See nice(1). | ||
79 | |||
80 | start_daemon should return the LSB defined exit status codes. It | ||
81 | shall return 0 if the program has been successfully started or | ||
82 | is running and not 0 otherwise. | ||
83 | */ | ||
84 | |||
85 | |||
86 | int start_daemon_main(int argc, char **argv) | ||
87 | { | ||
88 | int result = EXIT_FAILURE; | ||
89 | int force = 0; | ||
90 | int nice_level = 0; | ||
91 | char *pidfile = NULL; | ||
92 | char *pathname = NULL; | ||
93 | char *args = NULL; | ||
94 | int i; | ||
95 | |||
96 | for (i = 1; i < argc; i++) | ||
97 | { | ||
98 | char *p = argv[i]; | ||
99 | |||
100 | if (*p == '-') | ||
101 | { | ||
102 | while (*(++p)) | ||
103 | { | ||
104 | switch (*p) | ||
105 | { | ||
106 | case 'f' : | ||
107 | force = 1; | ||
108 | break; | ||
109 | |||
110 | case 'n' : | ||
111 | if ((++i) < argc) | ||
112 | nice_level = atoi(argv[i]); | ||
113 | else | ||
114 | bb_show_usage(); | ||
115 | break; | ||
116 | |||
117 | case 'p' : | ||
118 | if ((++i) < argc) | ||
119 | pidfile = argv[i]; | ||
120 | else | ||
121 | bb_show_usage(); | ||
122 | break; | ||
123 | |||
124 | default: | ||
125 | bb_show_usage(); | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | else if (pathname == NULL) | ||
130 | { | ||
131 | pathname = p; | ||
132 | break; | ||
133 | } | ||
134 | else | ||
135 | bb_show_usage(); | ||
136 | } | ||
137 | |||
138 | if (pathname == NULL) | ||
139 | bb_show_usage(); | ||
140 | |||
141 | if (i < argc) | ||
142 | args = argv_cat(argc - i, &argv[i]); | ||
143 | |||
144 | //bb_printf("ARGS - |%s|%s|%d|%d|%s\n", pidfile, pathname, force, nice_level, args); | ||
145 | result = start_daemon(force, nice_level, pidfile, pathname, args); | ||
146 | |||
147 | if (args != NULL) | ||
148 | free(args); | ||
149 | |||
150 | return result; | ||
151 | } | ||
152 | |||
153 | |||