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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2547 Revision 2549
6 */ 6 */
7 7
8#include <sys/types.h> 8#include <sys/types.h>
9#include <sys/stat.h> 9#include <sys/stat.h>
10 10
11#if defined(__DragonFly__) || defined(__FreeBSD__) || \
11#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__) 12 defined(__NetBSD__) || defined (__OpenBSD__)
12#include <sys/param.h> 13#include <sys/param.h>
13#include <sys/user.h> 14#include <sys/user.h>
14#include <sys/sysctl.h> 15#include <sys/sysctl.h>
15#include <kvm.h> 16#include <kvm.h>
16#include <limits.h> 17#include <limits.h>
176 eerrorx ("nothing in /proc"); 177 eerrorx ("nothing in /proc");
177 178
178 return (pids); 179 return (pids);
179} 180}
180 181
182#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
181#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 183 defined(__NetBSD__) || defined(__OpenBSD__)
182 184
183# if defined(__FreeBSD__) 185# if defined(__DragonFly__) || defined(__FreeBSD__)
186# ifndef KERN_PROC_PROC
187# define KERN_PROC_PROC KERN_PROC_ALL
188# endif
184# define _KINFO_PROC kinfo_proc 189# define _KINFO_PROC kinfo_proc
185# define _KVM_GETPROCS kvm_getprocs
186# define _KVM_GETARGV kvm_getargv 190# define _KVM_GETARGV kvm_getargv
187# define _GET_KINFO_UID(kp) (kp.ki_ruid) 191# define _GET_KINFO_UID(kp) (kp.ki_ruid)
188# define _GET_KINFO_COMM(kp) (kp.ki_comm) 192# define _GET_KINFO_COMM(kp) (kp.ki_comm)
189# define _GET_KINFO_PID(kp) (kp.ki_pid) 193# define _GET_KINFO_PID(kp) (kp.ki_pid)
190# else 194# else
191# define _KINFO_PROC kinfo_proc2 195# define _KINFO_PROC kinfo_proc2
192# define _KVM_GETPROCS kvm_getprocs2
193# define _KVM_GETARGV kvm_getargv2 196# define _KVM_GETARGV kvm_getargv2
194# define _GET_KINFO_UID(kp) (kp.p_ruid) 197# define _GET_KINFO_UID(kp) (kp.p_ruid)
195# define _GET_KINFO_COMM(kp) (kp.p_comm) 198# define _GET_KINFO_COMM(kp) (kp.p_comm)
196# define _GET_KINFO_PID(kp) (kp.p_pid) 199# define _GET_KINFO_PID(kp) (kp.p_pid)
197# endif 200# endif
210 int npids = 0; 213 int npids = 0;
211 214
212 if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) 215 if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL)
213 eerrorx ("kvm_open: %s", errbuf); 216 eerrorx ("kvm_open: %s", errbuf);
214 217
218#if defined(__DragonFly__) || defined( __FreeBSD__)
215 kp = _KVM_GETPROCS (kd, KERN_PROC_PROC, 0, &processes); 219 kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes);
220#else
221 kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
222 &processes);
223#endif
216 for (i = 0; i < processes; i++) 224 for (i = 0; i < processes; i++)
217 { 225 {
218 pid_t p = _GET_KINFO_PID (kp[i]); 226 pid_t p = _GET_KINFO_PID (kp[i]);
219 if (pid != 0 && pid != p) 227 if (pid != 0 && pid != p)
220 continue; 228 continue;
258static bool _match_daemon (const char *path, const char *file, 266static bool _match_daemon (const char *path, const char *file,
259 const char *mexec, const char *mname, 267 const char *mexec, const char *mname,
260 const char *mpidfile) 268 const char *mpidfile)
261{ 269{
262 char buffer[RC_LINEBUFFER]; 270 char buffer[RC_LINEBUFFER];
263 char *ffile = rc_strcatpaths (path, file, NULL); 271 char *ffile = rc_strcatpaths (path, file, (char *) NULL);
264 FILE *fp; 272 FILE *fp;
265 int lc = 0; 273 int lc = 0;
266 int m = 0; 274 int m = 0;
267 275
268 if (! rc_exists (ffile)) 276 if (! rc_exists (ffile))
312 320
313void rc_set_service_daemon (const char *service, const char *exec, 321void rc_set_service_daemon (const char *service, const char *exec,
314 const char *name, const char *pidfile, 322 const char *name, const char *pidfile,
315 bool started) 323 bool started)
316{ 324{
317 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), NULL); 325 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
326 (char *) NULL);
318 char **files = NULL; 327 char **files = NULL;
319 char *file; 328 char *file;
320 char *ffile = NULL; 329 char *ffile = NULL;
321 int i; 330 int i;
322 char *mexec; 331 char *mexec;
359 { 368 {
360 char *oldfile = NULL; 369 char *oldfile = NULL;
361 files = rc_ls_dir (NULL, dirpath, 0); 370 files = rc_ls_dir (NULL, dirpath, 0);
362 STRLIST_FOREACH (files, file, i) 371 STRLIST_FOREACH (files, file, i)
363 { 372 {
364 ffile = rc_strcatpaths (dirpath, file, NULL); 373 ffile = rc_strcatpaths (dirpath, file, (char *) NULL);
365 nfiles++; 374 nfiles++;
366 375
367 if (! oldfile) 376 if (! oldfile)
368 { 377 {
369 if (_match_daemon (dirpath, file, mexec, mname, mpidfile)) 378 if (_match_daemon (dirpath, file, mexec, mname, mpidfile))
394 if (! rc_is_dir (dirpath)) 403 if (! rc_is_dir (dirpath))
395 if (mkdir (dirpath, 0755) != 0) 404 if (mkdir (dirpath, 0755) != 0)
396 eerror ("mkdir `%s': %s", dirpath, strerror (errno)); 405 eerror ("mkdir `%s': %s", dirpath, strerror (errno));
397 406
398 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1); 407 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
399 file = rc_strcatpaths (dirpath, buffer, NULL); 408 file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
400 if ((fp = fopen (file, "w")) == NULL) 409 if ((fp = fopen (file, "w")) == NULL)
401 eerror ("fopen `%s': %s", file, strerror (errno)); 410 eerror ("fopen `%s': %s", file, strerror (errno));
402 else 411 else
403 { 412 {
404 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile); 413 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
423 bool retval = false; 432 bool retval = false;
424 433
425 if (! service || ! exec) 434 if (! service || ! exec)
426 return (false); 435 return (false);
427 436
428 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), NULL); 437 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
438 (char *) NULL);
429 if (! rc_is_dir (dirpath)) 439 if (! rc_is_dir (dirpath))
430 { 440 {
431 free (dirpath); 441 free (dirpath);
432 return (false); 442 return (false);
433 } 443 }
478 bool retval = false; 488 bool retval = false;
479 489
480 if (! service) 490 if (! service)
481 return (false); 491 return (false);
482 492
483 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service), NULL); 493 dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (service),
494 (char *) NULL);
484 if (! rc_is_dir (dirpath)) 495 if (! rc_is_dir (dirpath))
485 { 496 {
486 free (dirpath); 497 free (dirpath);
487 return (false); 498 return (false);
488 } 499 }
489 500
490 memset (buffer, 0, sizeof (buffer)); 501 memset (buffer, 0, sizeof (buffer));
491 files = rc_ls_dir (NULL, dirpath, 0); 502 files = rc_ls_dir (NULL, dirpath, 0);
492 STRLIST_FOREACH (files, file, i) 503 STRLIST_FOREACH (files, file, i)
493 { 504 {
494 path = rc_strcatpaths (dirpath, file, NULL); 505 path = rc_strcatpaths (dirpath, file, (char *) NULL);
495 fp = fopen (path, "r"); 506 fp = fopen (path, "r");
496 free (path); 507 free (path);
497 if (! fp) 508 if (! fp)
498 { 509 {
499 eerror ("fopen `%s': %s", file, strerror (errno)); 510 eerror ("fopen `%s': %s", file, strerror (errno));

Legend:
Removed from v.2547  
changed lines
  Added in v.2549

  ViewVC Help
Powered by ViewVC 1.1.20