summaryrefslogtreecommitdiffstats
path: root/urunlevel/runlevel/pidofproc.c
blob: 6d03ae8aad453e0c1155f7bb9cadd1e3fb39ee16 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Mini pidofproc implementation for busybox.
 *
 * Copyright (C) 2005 by David Seikel won_fang@yahoo.com.au
 *
 * 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 <getopt.h>
#include <string.h>
#include <unistd.h>

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


int checkpid(char *pid)
{
    int found = 0;
    char proc[132];

    sprintf(proc, "/proc/%s", pid);
    if (stat(proc, &path_stat) == 0)
    {
	char *state = NULL;

	sprintf(proc, "/proc/%s/stat", pid);
	state = quick_read(proc);
	if (state != NULL)
	{
	    /* Read and interpret the third space seperated field (State - DRSW = OK - ZX = DEAD_PID - T = STOPPED) */
	    if (state[0] != '\0')
	    {
		while (*state != ' ')  state++;
		while (*state == ' ')  state++;
		while (*state != ' ')  state++
// Should compare the name while scanning this.
		    ;
		while (*state == ' ')  state++;
		switch (*state)
		{
		    case 'D' :
		    case 'R' :
		    case 'S' :
		    case 'W' :
		    case 'T' :  /* Until proven otherwise, I'm assuming this means OK. */
			found = 1;
		}
	    }
	}
	else  /* Paranoid fallbacks. */
	{
	    sprintf(proc, "/proc/%s/exe", pid);
	    if (stat(proc, &path_stat) == 0)
	    {
// Should check that it is a link to our executable.
		found = 1;
	    }
	    else
	    {
		sprintf(proc, "/proc/%s/cmdline", pid);
		if (stat(proc, &path_stat) == 0)
		{
// Should compare the name.
		    found = 1;
		}
	    }
	}
	
    }

    return found;
}


int pidofproc(char *pidfile, char *pathname, char **pids)
{
    int status = INIT_D_STATUS_UNKNOWN;
    char *our_pidfile = pidfile;
    char *our_pids = NULL;

    if (our_pidfile == NULL)
	bb_xasprintf(&our_pidfile, "/var/run/%s.pid", bb_get_last_path_component(pathname));

    our_pids = quick_read(our_pidfile);

    if (our_pids != NULL)
    {
	int i;
	char *pid;
	char *strtok_temp;

	status = INIT_D_STATUS_DEAD_PID;
	if (pids != NULL)
	{
	    (*pids) = (char *) xmalloc(sizeof (char) * strlen(our_pids) + 1);
	    (*pids)[0] = '\0';
	}

	pid = strtok_r(our_pids, " \n\r", &strtok_temp);
	for (i = 0; pid != NULL; i++)
	{
	    if (checkpid(pid))
	    {
		if (pids != NULL)
		{
		    strcat(*pids, pid);
		    strcat(*pids, " ");
		}
		status = INIT_D_STATUS_OK;
	    }

	    pid = strtok_r(NULL, " \n\r", &strtok_temp);
	}

	free(our_pids);
    }
    else
	status = INIT_D_STATUS_NOT_RUNNING;

    if (pidfile == NULL)
	free(our_pidfile);

    return status;
}


/*
pidofproc [-p pidfile] pathname
    This function returns one or more pid(s) for a particular
    daemon using the algorithm given above. Only pids of running processes
    should be returned.

    Multiple pid(s) shall be separated by a single space in the pidfile
    and in the output of pidofproc.

    Compliant applications may use the basename instead of the pathname.
    pidofproc should return the LSB defined exit status
    codes for "status". It shall return 0 if the program is
    running and not 0 otherwise.
*/

static const struct option long_options[] = 
{
	{ "pidfile",	1,	NULL,	'p' },
	{ 0,		0,	0,	0 }
};

int pidofproc_main(int argc, char **argv)
{
    int result = EXIT_FAILURE;
    char *pidfile = NULL;
    char *pids = NULL;
    int opt;

    while ((opt = getopt_long (argc, argv, "p:", long_options, NULL)) > 0)
    {
	switch (opt) 
	{
	    case 'p':
		pidfile = optarg;
		break;

	    default:
		bb_show_usage();
	}
    }

    /* We require exactly one argument: the path name */
    if (optind != (argc - 1)) 
	bb_show_usage();

    result = pidofproc(pidfile, argv[optind], &pids);
    if (pids != NULL)
	bb_printf("%s\n", pids);

    return result;
}