/[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 2928 Revision 2986
77 uid_t uid, pid_t pid) 77 uid_t uid, pid_t pid)
78{ 78{
79 DIR *procdir; 79 DIR *procdir;
80 struct dirent *entry; 80 struct dirent *entry;
81 int npids = 0; 81 int npids = 0;
82 int foundany = false;
83 pid_t p; 82 pid_t p;
84 pid_t *pids = NULL; 83 pid_t *pids = NULL;
84 pid_t *tmp = NULL;
85 char buffer[PATH_MAX]; 85 char buffer[PATH_MAX];
86 struct stat sb; 86 struct stat sb;
87 pid_t runscript_pid = 0; 87 pid_t runscript_pid = 0;
88 char *pp; 88 char *pp;
89 89
90 if ((procdir = opendir ("/proc")) == NULL) 90 if ((procdir = opendir ("/proc")) == NULL)
91 eerrorx ("opendir `/proc': %s", strerror (errno)); 91 return (NULL);
92 92
93 /* 93 /*
94 We never match RC_RUNSCRIPT_PID if present so we avoid the below 94 We never match RC_RUNSCRIPT_PID if present so we avoid the below
95 scenario 95 scenario
96 96
107 } 107 }
108 108
109 while ((entry = readdir (procdir)) != NULL) { 109 while ((entry = readdir (procdir)) != NULL) {
110 if (sscanf (entry->d_name, "%d", &p) != 1) 110 if (sscanf (entry->d_name, "%d", &p) != 1)
111 continue; 111 continue;
112 foundany = true;
113 112
114 if (runscript_pid != 0 && runscript_pid == p) 113 if (runscript_pid != 0 && runscript_pid == p)
115 continue; 114 continue;
116 115
117 if (pid != 0 && pid != p) 116 if (pid != 0 && pid != p)
127 continue; 126 continue;
128 127
129 if (exec && ! cmd && ! pid_is_exec (p, exec)) 128 if (exec && ! cmd && ! pid_is_exec (p, exec))
130 continue; 129 continue;
131 130
132 pids = realloc (pids, sizeof (pid_t) * (npids + 2)); 131 tmp = realloc (pids, sizeof (pid_t) * (npids + 2));
133 if (! pids) 132 if (! tmp) {
134 eerrorx ("memory exhausted"); 133 free (pids);
134 closedir (procdir);
135 errno = ENOMEM;
136 return (NULL);
137 }
138 pids = tmp;
135 139
136 pids[npids] = p; 140 pids[npids] = p;
137 pids[npids + 1] = 0; 141 pids[npids + 1] = 0;
138 npids++; 142 npids++;
139 } 143 }
140 closedir (procdir); 144 closedir (procdir);
141
142 if (! foundany)
143 eerrorx ("nothing in /proc");
144 145
145 return (pids); 146 return (pids);
146} 147}
147librc_hidden_def(rc_find_pids) 148librc_hidden_def(rc_find_pids)
148 149
175 int i; 176 int i;
176 int processes = 0; 177 int processes = 0;
177 int argc = 0; 178 int argc = 0;
178 char **argv; 179 char **argv;
179 pid_t *pids = NULL; 180 pid_t *pids = NULL;
181 pid_t *tmp;
180 int npids = 0; 182 int npids = 0;
181 183
182 if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) 184 if ((kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf)) == NULL) {
183 eerrorx ("kvm_open: %s", errbuf); 185 fprintf (stderr, "kvm_open: %s", errbuf);
186 return (NULL);
187 }
184 188
185#if defined(__DragonFly__) || defined( __FreeBSD__) 189#if defined(__DragonFly__) || defined( __FreeBSD__)
186 kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes); 190 kp = kvm_getprocs (kd, KERN_PROC_PROC, 0, &processes);
187#else 191#else
188 kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), 192 kp = kvm_getproc2 (kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
208 212
209 if (strcmp (*argv, exec) != 0) 213 if (strcmp (*argv, exec) != 0)
210 continue; 214 continue;
211 } 215 }
212 216
213 pids = realloc (pids, sizeof (pid_t) * (npids + 2)); 217 tmp = realloc (pids, sizeof (pid_t) * (npids + 2));
214 if (! pids) 218 if (! tmp) {
215 eerrorx ("memory exhausted"); 219 free (pids);
220 kvm_close (kd);
221 errno = ENOMEM;
222 return (NULL);
223 }
224 pids = tmp;
216 225
217 pids[npids] = p; 226 pids[npids] = p;
218 pids[npids + 1] = 0; 227 pids[npids + 1] = 0;
219 npids++; 228 npids++;
220 } 229 }
221 kvm_close(kd); 230 kvm_close (kd);
222 231
223 return (pids); 232 return (pids);
224} 233}
225librc_hidden_def(rc_find_pids) 234librc_hidden_def(rc_find_pids)
226 235
236 char *ffile = rc_strcatpaths (path, file, (char *) NULL); 245 char *ffile = rc_strcatpaths (path, file, (char *) NULL);
237 FILE *fp; 246 FILE *fp;
238 int lc = 0; 247 int lc = 0;
239 int m = 0; 248 int m = 0;
240 249
241 if (! rc_exists (ffile)) {
242 free (ffile);
243 return (false);
244 }
245
246 if ((fp = fopen (ffile, "r")) == NULL) { 250 if ((fp = fopen (ffile, "r")) == NULL) {
247 eerror ("fopen `%s': %s", ffile, strerror (errno));
248 free (ffile); 251 free (ffile);
249 return (false); 252 return (false);
250 } 253 }
251 254
252 if (! mname) 255 if (! mname)
278 free (ffile); 281 free (ffile);
279 282
280 return (m == 111 ? true : false); 283 return (m == 111 ? true : false);
281} 284}
282 285
283void rc_set_service_daemon (const char *service, const char *exec, 286void rc_service_daemon_set (const char *service, const char *exec,
284 const char *name, const char *pidfile, 287 const char *name, const char *pidfile,
285 bool started) 288 bool started)
286{ 289{
287 char *svc = rc_xstrdup (service); 290 char *svc = rc_xstrdup (service);
288 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc), 291 char *dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename (svc),
348 /* Now store our daemon info */ 351 /* Now store our daemon info */
349 if (started) { 352 if (started) {
350 char buffer[10]; 353 char buffer[10];
351 FILE *fp; 354 FILE *fp;
352 355
353 if (! rc_is_dir (dirpath)) 356 if (mkdir (dirpath, 0755) == 0 || errno == EEXIST) {
354 if (mkdir (dirpath, 0755) != 0)
355 eerror ("mkdir `%s': %s", dirpath, strerror (errno));
356
357 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1); 357 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
358 file = rc_strcatpaths (dirpath, buffer, (char *) NULL); 358 file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
359 if ((fp = fopen (file, "w")) == NULL) 359 if ((fp = fopen (file, "w")))
360 eerror ("fopen `%s': %s", file, strerror (errno));
361 else {
362 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile); 360 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
363 fclose (fp); 361 fclose (fp);
364 }
365 free (file); 362 free (file);
363 }
366 } 364 }
367 365
368 free (mexec); 366 free (mexec);
369 free (mname); 367 free (mname);
370 free (mpidfile); 368 free (mpidfile);
371 free (dirpath); 369 free (dirpath);
372} 370}
373librc_hidden_def(rc_set_service_daemon) 371librc_hidden_def(rc_service_daemon_set)
374 372
375bool rc_service_started_daemon (const char *service, const char *exec, 373bool rc_service_started_daemon (const char *service, const char *exec,
376 int indx) 374 int indx)
377{ 375{
378 char *dirpath; 376 char *dirpath;
456 files = rc_ls_dir (dirpath, 0); 454 files = rc_ls_dir (dirpath, 0);
457 STRLIST_FOREACH (files, file, i) { 455 STRLIST_FOREACH (files, file, i) {
458 path = rc_strcatpaths (dirpath, file, (char *) NULL); 456 path = rc_strcatpaths (dirpath, file, (char *) NULL);
459 fp = fopen (path, "r"); 457 fp = fopen (path, "r");
460 free (path); 458 free (path);
461 if (! fp) { 459 if (! fp)
462 eerror ("fopen `%s': %s", file, strerror (errno)); 460 break;
463 continue;
464 }
465 461
466 while ((fgets (buffer, RC_LINEBUFFER, fp))) { 462 while ((fgets (buffer, RC_LINEBUFFER, fp))) {
467 int lb = strlen (buffer) - 1; 463 int lb = strlen (buffer) - 1;
468 if (buffer[lb] == '\n') 464 if (buffer[lb] == '\n')
469 buffer[lb] = 0; 465 buffer[lb] = 0;
497 retval = true; 493 retval = true;
498 break; 494 break;
499 } 495 }
500 496
501 if ((fp = fopen (pidfile, "r")) == NULL) { 497 if ((fp = fopen (pidfile, "r")) == NULL) {
502 eerror ("fopen `%s': %s", pidfile, strerror (errno));
503 retval = true; 498 retval = true;
504 break; 499 break;
505 } 500 }
506 501
507 if (fscanf (fp, "%d", &pid) != 1) { 502 if (fscanf (fp, "%d", &pid) != 1) {
508 eerror ("no pid found in `%s'", pidfile);
509 fclose (fp); 503 fclose (fp);
510 retval = true; 504 retval = true;
511 break; 505 break;
512 } 506 }
513 507

Legend:
Removed from v.2928  
changed lines
  Added in v.2986

  ViewVC Help
Powered by ViewVC 1.1.20