/[baselayout]/trunk/src/librc-daemon.c
Gentoo

Contents of /trunk/src/librc-daemon.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2957 - (show annotations) (download) (as text)
Sat Sep 29 16:42:08 2007 UTC (7 years ago) by uberlord
File MIME type: text/x-csrc
File size: 10910 byte(s)
librc no longer uses libeinfo. deptree function names are now all under rc_deptree_
1 /*
2 librc-daemon
3 Finds PID for given daemon criteria
4 Copyright 2007 Gentoo Foundation
5 Released under the GPLv2
6 */
7
8 #include "librc.h"
9
10 #if defined(__linux__)
11 static bool pid_is_cmd (pid_t pid, const char *cmd)
12 {
13 char buffer[32];
14 FILE *fp;
15 int c;
16
17 snprintf(buffer, sizeof (buffer), "/proc/%d/stat", pid);
18 if ((fp = fopen (buffer, "r")) == NULL)
19 return (false);
20
21 while ((c = getc (fp)) != EOF && c != '(')
22 ;
23
24 if (c != '(') {
25 fclose(fp);
26 return (false);
27 }
28
29 while ((c = getc (fp)) != EOF && c == *cmd)
30 cmd++;
31
32 fclose (fp);
33
34 return ((c == ')' && *cmd == '\0') ? true : false);
35 }
36
37 static bool pid_is_exec (pid_t pid, const char *exec)
38 {
39 char cmdline[32];
40 char buffer[PATH_MAX];
41 char *p;
42 int fd = -1;
43 int r;
44
45 snprintf (cmdline, sizeof (cmdline), "/proc/%u/exe", pid);
46 memset (buffer, 0, sizeof (buffer));
47 if (readlink (cmdline, buffer, sizeof (buffer)) != -1) {
48 if (strcmp (exec, buffer) == 0)
49 return (true);
50
51 /* We should cater for deleted binaries too */
52 if (strlen (buffer) > 10) {
53 p = buffer + (strlen (buffer) - 10);
54 if (strcmp (p, " (deleted)") == 0) {
55 *p = 0;
56 if (strcmp (buffer, exec) == 0)
57 return (true);
58 }
59 }
60 }
61
62 snprintf (cmdline, sizeof (cmdline), "/proc/%u/cmdline", pid);
63 if ((fd = open (cmdline, O_RDONLY)) < 0)
64 return (false);
65
66 r = read(fd, buffer, sizeof (buffer));
67 close (fd);
68
69 if (r == -1)
70 return 0;
71
72 buffer[r] = 0;
73 return (strcmp (exec, buffer) == 0 ? true : false);
74 }
75
76 pid_t *rc_find_pids (const char *exec, const char *cmd,
77 uid_t uid, pid_t pid)
78 {
79 DIR *procdir;
80 struct dirent *entry;
81 int npids = 0;
82 pid_t p;
83 pid_t *pids = NULL;
84 pid_t *tmp = NULL;
85 char buffer[PATH_MAX];
86 struct stat sb;
87 pid_t runscript_pid = 0;
88 char *pp;
89
90 if ((procdir = opendir ("/proc")) == NULL)
91 return (NULL);
92
93 /*
94 We never match RC_RUNSCRIPT_PID if present so we avoid the below
95 scenario
96
97 /etc/init.d/ntpd stop does
98 start-stop-daemon --stop --name ntpd
99 catching /etc/init.d/ntpd stop
100
101 nasty
102 */
103
104 if ((pp = getenv ("RC_RUNSCRIPT_PID"))) {
105 if (sscanf (pp, "%d", &runscript_pid) != 1)
106 runscript_pid = 0;
107 }
108
109 while ((entry = readdir (procdir)) != NULL) {
110 if (sscanf (entry->d_name, "%d", &p) != 1)
111 continue;
112
113 if (runscript_pid != 0 && runscript_pid == p)
114 continue;
115
116 if (pid != 0 && pid != p)
117 continue;
118
119 if (uid) {
120 snprintf (buffer, sizeof (buffer), "/proc/%d", p);
121 if (stat (buffer, &sb) != 0 || sb.st_uid != uid)
122 continue;
123 }
124
125 if (cmd && ! pid_is_cmd (p, cmd))
126 continue;
127
128 if (exec && ! cmd && ! pid_is_exec (p, exec))
129 continue;
130
131 tmp = realloc (pids, sizeof (pid_t) * (npids + 2));
132 if (! tmp) {
133 free (pids);
134 closedir (procdir);
135 errno = ENOMEM;
136 return (NULL);
137 }
138 pids = tmp;
139
140 pids[npids] = p;
141 pids[npids + 1] = 0;
142 npids++;
143 }
144 closedir (procdir);
145
146 return (pids);
147 }
148 librc_hidden_def(rc_find_pids)
149
150 #elif defined(__DragonFly__) || defined(__FreeBSD__) || \
151 defined(__NetBSD__) || defined(__OpenBSD__)
152
153 # if defined(__DragonFly__) || defined(__FreeBSD__)
154 # ifndef KERN_PROC_PROC
155 # define KERN_PROC_PROC KERN_PROC_ALL
156 # endif
157 # define _KINFO_PROC kinfo_proc
158 # define _KVM_GETARGV kvm_getargv
159 # define _GET_KINFO_UID(kp) (kp.ki_ruid)
160 # define _GET_KINFO_COMM(kp) (kp.ki_comm)
161 # define _GET_KINFO_PID(kp) (kp.ki_pid)
162 # else
163 # define _KINFO_PROC kinfo_proc2
164 # define _KVM_GETARGV kvm_getargv2
165 # define _GET_KINFO_UID(kp) (kp.p_ruid)
166 # define _GET_KINFO_COMM(kp) (kp.p_comm)
167 # define _GET_KINFO_PID(kp) (kp.p_pid)
168 # endif
169
170 pid_t *rc_find_pids (const char *exec, const char *cmd,
171 uid_t uid, pid_t pid)
172 {
173 static kvm_t *kd = NULL;
174 char errbuf[_POSIX2_LINE_MAX];
175 struct _KINFO_PROC *kp;
176 int i;
177 int processes = 0;
178 int argc = 0;
179 char **argv;
180 pid_t *pids = NULL;
181 pid_t *tmp;
182 int npids = 0;
183
184 if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL)
185 eerrorx ("kvm_open: %s", errbuf);
186
187 #if defined(__DragonFly__) || defined( __FreeBSD__)
188 kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes);
189 #else
190 kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
191 &processes);
192 #endif
193 for (i = 0; i < processes; i++) {
194 pid_t p = _GET_KINFO_PID (kp[i]);
195 if (pid != 0 && pid != p)
196 continue;
197
198 if (uid != 0 && uid != _GET_KINFO_UID (kp[i]))
199 continue;
200
201 if (cmd) {
202 if (! _GET_KINFO_COMM (kp[i]) ||
203 strcmp (cmd, _GET_KINFO_COMM (kp[i])) != 0)
204 continue;
205 }
206
207 if (exec && ! cmd) {
208 if ((argv = _KVM_GETARGV (kd, &kp[i], argc)) == NULL || ! *argv)
209 continue;
210
211 if (strcmp (*argv, exec) != 0)
212 continue;
213 }
214
215 tmp = realloc (pids, sizeof (pid_t) * (npids + 2));
216 if (! tmp) {
217 free (pids);
218 kvm_close (kd);
219 errno = ENOMEM;
220 return (NULL);
221 }
222 pids = tmp;
223
224 pids[npids] = p;
225 pids[npids + 1] = 0;
226 npids++;
227 }
228 kvm_close (kd);
229
230 return (pids);
231 }
232 librc_hidden_def(rc_find_pids)
233
234 #else
235 # error "Platform not supported!"
236 #endif
237
238 static bool _match_daemon (const char *path, const char *file,
239 const char *mexec, const char *mname,
240 const char *mpidfile)
241 {
242 char buffer[RC_LINEBUFFER];
243 char *ffile = rc_strcatpaths (path, file, (char *) NULL);
244 FILE *fp;
245 int lc = 0;
246 int m = 0;
247
248 if ((fp = fopen (ffile, "r")) == NULL) {
249 free (ffile);
250 return (false);
251 }
252
253 if (! mname)
254 m += 10;
255 if (! mpidfile)
256 m += 100;
257
258 memset (buffer, 0, sizeof (buffer));
259 while ((fgets (buffer, RC_LINEBUFFER, fp))) {
260 int lb = strlen (buffer) - 1;
261 if (buffer[lb] == '\n')
262 buffer[lb] = 0;
263
264 if (strcmp (buffer, mexec) == 0)
265 m += 1;
266 else if (mname && strcmp (buffer, mname) == 0)
267 m += 10;
268 else if (mpidfile && strcmp (buffer, mpidfile) == 0)
269 m += 100;
270
271 if (m == 111)
272 break;
273
274 lc++;
275 if (lc > 5)
276 break;
277 }
278 fclose (fp);
279 free (ffile);
280
281 return (m == 111 ? true : false);
282 }
283
284 void rc_set_service_daemon (const char *service, const char *exec,
285 const char *name, const char *pidfile,
286 bool started)
287 {
288 char *svc = rc_xstrdup (service);
289 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
290 (char *) NULL);
291 char **files = NULL;
292 char *file;
293 char *ffile = NULL;
294 int i;
295 char *mexec;
296 char *mname;
297 char *mpidfile;
298 int nfiles = 0;
299
300 free (svc);
301 if (! exec && ! name && ! pidfile)
302 return;
303
304 if (exec) {
305 i = strlen (exec) + 6;
306 mexec = rc_xmalloc (sizeof (char *) * i);
307 snprintf (mexec, i, "exec=%s", exec);
308 } else
309 mexec = rc_xstrdup ("exec=");
310
311 if (name) {
312 i = strlen (name) + 6;
313 mname = rc_xmalloc (sizeof (char *) * i);
314 snprintf (mname, i, "name=%s", name);
315 } else
316 mname = rc_xstrdup ("name=");
317
318 if (pidfile) {
319 i = strlen (pidfile) + 9;
320 mpidfile = rc_xmalloc (sizeof (char *) * i);
321 snprintf (mpidfile, i, "pidfile=%s", pidfile);
322 } else
323 mpidfile = rc_xstrdup ("pidfile=");
324
325 /* Regardless, erase any existing daemon info */
326 if (rc_is_dir (dirpath)) {
327 char *oldfile = NULL;
328 files = rc_ls_dir (dirpath, 0);
329 STRLIST_FOREACH (files, file, i) {
330 ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
331 nfiles++;
332
333 if (! oldfile) {
334 if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) {
335 unlink (ffile);
336 oldfile = ffile;
337 nfiles--;
338 }
339 } else {
340 rename (ffile, oldfile);
341 free (oldfile);
342 oldfile = ffile;
343 }
344 }
345 free (ffile);
346 rc_strlist_free (files);
347 }
348
349 /* Now store our daemon info */
350 if (started) {
351 char buffer[10];
352 FILE *fp;
353
354 if (mkdir (dirpath, 0755) == 0 || errno == EEXIST) {
355 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
356 file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
357 if ((fp = fopen (file, "w")))
358 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
359 fclose (fp);
360 free (file);
361 }
362 }
363
364 free (mexec);
365 free (mname);
366 free (mpidfile);
367 free (dirpath);
368 }
369 librc_hidden_def(rc_set_service_daemon)
370
371 bool rc_service_started_daemon (const char *service, const char *exec,
372 int indx)
373 {
374 char *dirpath;
375 char *file;
376 int i;
377 char *mexec;
378 bool retval = false;
379 char *svc;
380
381 if (! service || ! exec)
382 return (false);
383
384 svc = rc_xstrdup (service);
385 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
386 (char *) NULL);
387 free (svc);
388
389 if (! rc_is_dir (dirpath)) {
390 free (dirpath);
391 return (false);
392 }
393
394 i = strlen (exec) + 6;
395 mexec = rc_xmalloc (sizeof (char *) * i);
396 snprintf (mexec, i, "exec=%s", exec);
397
398 if (indx > 0) {
399 int len = sizeof (char *) * 10;
400 file = rc_xmalloc (len);
401 snprintf (file, len, "%03d", indx);
402 retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
403 free (file);
404 } else {
405 char **files = rc_ls_dir (dirpath, 0);
406 STRLIST_FOREACH (files, file, i) {
407 retval = _match_daemon (dirpath, file, mexec, NULL, NULL);
408 if (retval)
409 break;
410 }
411 rc_strlist_free (files);
412 }
413
414 free (mexec);
415 return (retval);
416 }
417 librc_hidden_def(rc_service_started_daemon)
418
419 bool rc_service_daemons_crashed (const char *service)
420 {
421 char *dirpath;
422 char **files;
423 char *file;
424 char *path;
425 int i;
426 FILE *fp;
427 char buffer[RC_LINEBUFFER];
428 char *exec = NULL;
429 char *name = NULL;
430 char *pidfile = NULL;
431 pid_t pid = 0;
432 pid_t *pids = NULL;
433 char *p;
434 char *token;
435 bool retval = false;
436 char *svc;
437
438 if (! service)
439 return (false);
440
441 svc = rc_xstrdup (service);
442 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
443 (char *) NULL);
444 free (svc);
445
446 if (! rc_is_dir (dirpath)) {
447 free (dirpath);
448 return (false);
449 }
450
451 memset (buffer, 0, sizeof (buffer));
452 files = rc_ls_dir (dirpath, 0);
453 STRLIST_FOREACH (files, file, i) {
454 path = rc_strcatpaths (dirpath, file, (char *) NULL);
455 fp = fopen (path, "r");
456 free (path);
457 if (! fp)
458 break;
459
460 while ((fgets (buffer, RC_LINEBUFFER, fp))) {
461 int lb = strlen (buffer) - 1;
462 if (buffer[lb] == '\n')
463 buffer[lb] = 0;
464
465 p = buffer;
466 if ((token = strsep (&p, "=")) == NULL || ! p)
467 continue;
468
469 if (strlen (p) == 0)
470 continue;
471
472 if (strcmp (token, "exec") == 0) {
473 if (exec)
474 free (exec);
475 exec = rc_xstrdup (p);
476 } else if (strcmp (token, "name") == 0) {
477 if (name)
478 free (name);
479 name = rc_xstrdup (p);
480 } else if (strcmp (token, "pidfile") == 0) {
481 if (pidfile)
482 free (pidfile);
483 pidfile = rc_xstrdup (p);
484 }
485 }
486 fclose (fp);
487
488 pid = 0;
489 if (pidfile) {
490 if (! rc_exists (pidfile)) {
491 retval = true;
492 break;
493 }
494
495 if ((fp = fopen (pidfile, "r")) == NULL) {
496 retval = true;
497 break;
498 }
499
500 if (fscanf (fp, "%d", &pid) != 1) {
501 fclose (fp);
502 retval = true;
503 break;
504 }
505
506 fclose (fp);
507 free (pidfile);
508 pidfile = NULL;
509
510 /* We have the pid, so no need to match on name */
511 free (exec);
512 exec = NULL;
513 free (name);
514 name = NULL;
515 }
516
517 if ((pids = rc_find_pids (exec, name, 0, pid)) == NULL) {
518 retval = true;
519 break;
520 }
521 free (pids);
522
523 free (exec);
524 exec = NULL;
525 free (name);
526 name = NULL;
527 }
528
529 free (exec);
530 free (name);
531 free (dirpath);
532 rc_strlist_free (files);
533
534 return (retval);
535 }
536 librc_hidden_def(rc_service_daemons_crashed)

  ViewVC Help
Powered by ViewVC 1.1.20