/[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 2957
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 eerrorx ("kvm_open: %s", errbuf);
184 186
208 210
209 if (strcmp (*argv, exec) != 0) 211 if (strcmp (*argv, exec) != 0)
210 continue; 212 continue;
211 } 213 }
212 214
213 pids = realloc (pids, sizeof (pid_t) * (npids + 2)); 215 tmp = realloc (pids, sizeof (pid_t) * (npids + 2));
214 if (! pids) 216 if (! tmp) {
215 eerrorx ("memory exhausted"); 217 free (pids);
218 kvm_close (kd);
219 errno = ENOMEM;
220 return (NULL);
221 }
222 pids = tmp;
216 223
217 pids[npids] = p; 224 pids[npids] = p;
218 pids[npids + 1] = 0; 225 pids[npids + 1] = 0;
219 npids++; 226 npids++;
220 } 227 }
221 kvm_close(kd); 228 kvm_close (kd);
222 229
223 return (pids); 230 return (pids);
224} 231}
225librc_hidden_def(rc_find_pids) 232librc_hidden_def(rc_find_pids)
226 233
236 char *ffile = rc_strcatpaths (path, file, (char *) NULL); 243 char *ffile = rc_strcatpaths (path, file, (char *) NULL);
237 FILE *fp; 244 FILE *fp;
238 int lc = 0; 245 int lc = 0;
239 int m = 0; 246 int m = 0;
240 247
241 if (! rc_exists (ffile)) {
242 free (ffile);
243 return (false);
244 }
245
246 if ((fp = fopen (ffile, "r")) == NULL) { 248 if ((fp = fopen (ffile, "r")) == NULL) {
247 eerror ("fopen `%s': %s", ffile, strerror (errno));
248 free (ffile); 249 free (ffile);
249 return (false); 250 return (false);
250 } 251 }
251 252
252 if (! mname) 253 if (! mname)
348 /* Now store our daemon info */ 349 /* Now store our daemon info */
349 if (started) { 350 if (started) {
350 char buffer[10]; 351 char buffer[10];
351 FILE *fp; 352 FILE *fp;
352 353
353 if (! rc_is_dir (dirpath)) 354 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); 355 snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
358 file = rc_strcatpaths (dirpath, buffer, (char *) NULL); 356 file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
359 if ((fp = fopen (file, "w")) == NULL) 357 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); 358 fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile);
363 fclose (fp); 359 fclose (fp);
364 }
365 free (file); 360 free (file);
361 }
366 } 362 }
367 363
368 free (mexec); 364 free (mexec);
369 free (mname); 365 free (mname);
370 free (mpidfile); 366 free (mpidfile);
456 files = rc_ls_dir (dirpath, 0); 452 files = rc_ls_dir (dirpath, 0);
457 STRLIST_FOREACH (files, file, i) { 453 STRLIST_FOREACH (files, file, i) {
458 path = rc_strcatpaths (dirpath, file, (char *) NULL); 454 path = rc_strcatpaths (dirpath, file, (char *) NULL);
459 fp = fopen (path, "r"); 455 fp = fopen (path, "r");
460 free (path); 456 free (path);
461 if (! fp) { 457 if (! fp)
462 eerror ("fopen `%s': %s", file, strerror (errno)); 458 break;
463 continue;
464 }
465 459
466 while ((fgets (buffer, RC_LINEBUFFER, fp))) { 460 while ((fgets (buffer, RC_LINEBUFFER, fp))) {
467 int lb = strlen (buffer) - 1; 461 int lb = strlen (buffer) - 1;
468 if (buffer[lb] == '\n') 462 if (buffer[lb] == '\n')
469 buffer[lb] = 0; 463 buffer[lb] = 0;
497 retval = true; 491 retval = true;
498 break; 492 break;
499 } 493 }
500 494
501 if ((fp = fopen (pidfile, "r")) == NULL) { 495 if ((fp = fopen (pidfile, "r")) == NULL) {
502 eerror ("fopen `%s': %s", pidfile, strerror (errno));
503 retval = true; 496 retval = true;
504 break; 497 break;
505 } 498 }
506 499
507 if (fscanf (fp, "%d", &pid) != 1) { 500 if (fscanf (fp, "%d", &pid) != 1) {
508 eerror ("no pid found in `%s'", pidfile);
509 fclose (fp); 501 fclose (fp);
510 retval = true; 502 retval = true;
511 break; 503 break;
512 } 504 }
513 505

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

  ViewVC Help
Powered by ViewVC 1.1.20