summaryrefslogtreecommitdiffstats
path: root/urunlevel/runlevel/start_daemon.c
blob: 6bd0b2f160de005dc43819edf7e38bdb00470624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * Mini start_daemon implementation for busybox.
 *
 * Copyright (C) 2005 by David Seikel won_fang@yahoo.com.au
 *
 * Clean room implementation of LSB init.d specs.
 * I didn't steal any of this, honest B-).
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 */

#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>

#include "busybox.h"
#include "lib_init_d.h"


int start_daemon(int force, int nice_level, char *pidfile, char *pathname,
				 char *args)
{
	int status = pidofproc(pidfile, pathname, NULL);

	if ((status != INIT_D_STATUS_OK) || (force)) {
		char *pids = NULL;
		char *strtok_temp;

		doit(DAEMON, "start-stop-daemon -Sbmp %s -x %s -- %s", pidfile,
			 pathname, args);
// Should poll /proc/pid/... instead
		sleep(1);
		status = pidofproc(pidfile, pathname, &pids);
		if ((status == INIT_D_STATUS_OK) && (nice_level)) {
			int i;
			char *pid;

			pid = strtok_r(pids, " ", &strtok_temp);
			for (i = 0; pid != NULL; i++) {
				int ps = atoi(pid);
				int oldp;

				errno = 0;
				oldp = getpriority(PRIO_PROCESS, ps);
				if (errno == 0)
					setpriority(PRIO_PROCESS, ps, oldp + nice_level);
				pid = strtok_r(NULL, " ", &strtok_temp);
			}
		}
	}

	return status;
}


/*
start_daemon [-f] [-n nicelevel] [-p pidfile] pathname [args]
    This runs the specified program as a daemon.
    start_daemon shall check if the program is already running
    using the algorithm given above. If so, it shall not
    start another copy of the daemon unless the -f
    option is given. The -n option specifies a nice
    level. See nice(1).

    start_daemon should return the LSB defined exit status codes. It
    shall return 0 if the program has been successfully started or
    is running and not 0 otherwise.
*/


	/* getopt not used, because I don't think it can handle [args] */
int start_daemon_main(int argc, char **argv)
{
	int result = EXIT_FAILURE;
	int force = 0;
	int nice_level = 0;
	char *pidfile = NULL;
	char *pathname = NULL;
	char *args = NULL;
	int i;

	for (i = 1; i < argc; i++) {
		char *p = argv[i];

		if (*p == '-') {
			while (*(++p)) {
				switch (*p) {
				case 'f':
					force = 1;
					break;

				case 'n':
					if ((++i) < argc)
						nice_level = atoi(argv[i]);
					else
						bb_show_usage();
					break;

				case 'p':
					if ((++i) < argc)
						pidfile = argv[i];
					else
						bb_show_usage();
					break;

				default:
					bb_show_usage();
				}
			}
		} else if (pathname == NULL) {
			pathname = p;
			break;
		} else
			bb_show_usage();
	}

	if (pathname == NULL)
		bb_show_usage();

	if (i < argc)
		args = argv_cat(argc - i, &argv[i]);

//bb_printf("ARGS - |%s|%s|%d|%d|%s\n", pidfile, pathname, force, nice_level, args);
	result = start_daemon(force, nice_level, pidfile, pathname, args);

	if (args != NULL)
		free(args);

	return result;
}