Now that s-s-d accepts >1 argument for stopping a daemon on match we need to store all arguments used incase a service started the same daemon more than once with different paramters.

This commit is contained in:
Roy Marples 2008-02-23 00:26:11 +00:00
parent 00aa221b98
commit 8b4b958678
3 changed files with 75 additions and 74 deletions

View File

@ -293,46 +293,62 @@ librc_hidden_def(rc_find_pids)
# error "Platform not supported!" # error "Platform not supported!"
#endif #endif
static bool _match_daemon (const char *path, const char *file, static bool _match_daemon (const char *path, const char *file, char **match)
const char *mexec, const char *mname,
const char *mpidfile)
{ {
char *line; char *line;
char *ffile = rc_strcatpaths (path, file, (char *) NULL); char *ffile = rc_strcatpaths (path, file, (char *) NULL);
FILE *fp; FILE *fp;
int lc = 0;
int m = 0;
if ((fp = fopen (ffile, "r")) == NULL) { fp = fopen (ffile, "r");
free (ffile); free (ffile);
return (false);
}
if (! mname) if (! fp)
m += 10; return (false);
if (! mpidfile)
m += 100;
while ((line = rc_getline (fp))) { while ((line = rc_getline (fp))) {
if (strcmp (line, mexec) == 0) rc_strlist_delete (&match, line);
m += 1; if (! match || !*match)
else if (mname && strcmp (line, mname) == 0)
m += 10;
else if (mpidfile && strcmp (line, mpidfile) == 0)
m += 100;
free (line);
if (m == 111)
break;
lc++;
if (lc > 5)
break; break;
} }
fclose (fp); fclose (fp);
free (ffile); if (match && *match)
return (false);
return (true);
}
return (m == 111 ? true : false); static char **_match_list (const char* const* argv,
const char *name, const char *pidfile)
{
char **match = NULL;
int i = 0;
size_t l;
char *m;
while (argv && argv[i]) {
l = strlen (*argv) + strlen ("argv_=") + 16;
m = xmalloc (sizeof (char) * l);
snprintf (m, l, "argv_0=%s", argv[i++]);
rc_strlist_add (&match, m);
free (m);
}
if (name) {
l = strlen (name) + 6;
m = xmalloc (sizeof (char) * l);
snprintf (m, l, "name=%s", name);
rc_strlist_add (&match, m);
free (m);
}
if (pidfile) {
l = strlen (pidfile) + 9;
m = xmalloc (sizeof (char) * l);
snprintf (m, l, "pidfile=%s", pidfile);
rc_strlist_add (&match, m);
free (m);
}
return (match);
} }
bool rc_service_daemon_set (const char *service, const char *const *argv, bool rc_service_daemon_set (const char *service, const char *const *argv,
@ -341,15 +357,13 @@ bool rc_service_daemon_set (const char *service, const char *const *argv,
{ {
char *dirpath; char *dirpath;
char *file = NULL; char *file = NULL;
size_t l;
char *mexec;
char *mname;
char *mpidfile;
int nfiles = 0; int nfiles = 0;
char *oldfile = NULL; char *oldfile = NULL;
bool retval = false; bool retval = false;
DIR *dp; DIR *dp;
struct dirent *d; struct dirent *d;
char **match = NULL;
int i = 0;
if (! (argv && *argv) && ! name && ! pidfile) { if (! (argv && *argv) && ! name && ! pidfile) {
errno = EINVAL; errno = EINVAL;
@ -359,26 +373,7 @@ bool rc_service_daemon_set (const char *service, const char *const *argv,
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", dirpath = rc_strcatpaths (RC_SVCDIR, "daemons",
basename_c (service), (char *) NULL); basename_c (service), (char *) NULL);
if (argv && *argv) { match = _match_list (argv, name, pidfile);
l = strlen (*argv) + 6;
mexec = xmalloc (sizeof (char) * l);
snprintf (mexec, l, "exec=%s", *argv);
} else
mexec = xstrdup ("exec=");
if (name) {
l = strlen (name) + 6;
mname = xmalloc (sizeof (char) * l);
snprintf (mname, l, "name=%s", name);
} else
mname = xstrdup ("name=");
if (pidfile) {
l = strlen (pidfile) + 9;
mpidfile = xmalloc (sizeof (char) * l);
snprintf (mpidfile, l, "pidfile=%s", pidfile);
} else
mpidfile = xstrdup ("pidfile=");
/* Regardless, erase any existing daemon info */ /* Regardless, erase any existing daemon info */
if ((dp = opendir (dirpath))) { if ((dp = opendir (dirpath))) {
@ -389,9 +384,7 @@ bool rc_service_daemon_set (const char *service, const char *const *argv,
nfiles++; nfiles++;
if (! oldfile) { if (! oldfile) {
if (_match_daemon (dirpath, d->d_name, if (_match_daemon (dirpath, d->d_name, match)) {
mexec, mname, mpidfile))
{
unlink (file); unlink (file);
oldfile = file; oldfile = file;
nfiles--; nfiles--;
@ -415,7 +408,17 @@ bool rc_service_daemon_set (const char *service, const char *const *argv,
snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1); snprintf (buffer, sizeof (buffer), "%03d", nfiles + 1);
file = rc_strcatpaths (dirpath, buffer, (char *) NULL); file = rc_strcatpaths (dirpath, buffer, (char *) NULL);
if ((fp = fopen (file, "w"))) { if ((fp = fopen (file, "w"))) {
fprintf (fp, "%s\n%s\n%s\n", mexec, mname, mpidfile); while (argv && argv[i]) {
fprintf (fp, "argv_%d=%s\n", i, argv[i]);
i++;
}
fprintf (fp, "name=");
if (name)
fprintf (fp, "%s", name);
fprintf (fp, "\npidfile=");
if (pidfile)
fprintf (fp, "%s", pidfile);
fprintf (fp, "\n");
fclose (fp); fclose (fp);
retval = true; retval = true;
} }
@ -424,48 +427,44 @@ bool rc_service_daemon_set (const char *service, const char *const *argv,
} else } else
retval = true; retval = true;
free (mexec); rc_strlist_free (match);
free (mname);
free (mpidfile);
free (dirpath); free (dirpath);
return (retval); return (retval);
} }
librc_hidden_def(rc_service_daemon_set) librc_hidden_def(rc_service_daemon_set)
bool rc_service_started_daemon (const char *service, const char *exec, bool rc_service_started_daemon (const char *service, const char *const *argv,
int indx) int indx)
{ {
char *dirpath; char *dirpath;
char *file; char *file;
size_t l; size_t l;
char *mexec; char **match;
bool retval = false; bool retval = false;
DIR *dp; DIR *dp;
struct dirent *d; struct dirent *d;
if (! service || ! exec) if (! service || ! (argv && *argv))
return (false); return (false);
dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename_c (service), dirpath = rc_strcatpaths (RC_SVCDIR, "daemons", basename_c (service),
(char *) NULL); (char *) NULL);
l = strlen (exec) + 6; match = _match_list (argv, NULL, NULL);
mexec = xmalloc (sizeof (char) * l);
snprintf (mexec, l, "exec=%s", exec);
if (indx > 0) { if (indx > 0) {
l = sizeof (char) * 10; l = sizeof (char) * 10;
file = xmalloc (l); file = xmalloc (l);
snprintf (file, l, "%03d", indx); snprintf (file, l, "%03d", indx);
retval = _match_daemon (dirpath, file, mexec, NULL, NULL); retval = _match_daemon (dirpath, file, match);
free (file); free (file);
} else { } else {
if ((dp = opendir (dirpath))) { if ((dp = opendir (dirpath))) {
while ((d = readdir (dp))) { while ((d = readdir (dp))) {
if (d->d_name[0] == '.') if (d->d_name[0] == '.')
continue; continue;
retval = _match_daemon (dirpath, d->d_name, mexec, NULL, NULL); retval = _match_daemon (dirpath, d->d_name, match);
if (retval) if (retval)
break; break;
} }
@ -474,7 +473,7 @@ bool rc_service_started_daemon (const char *service, const char *exec,
} }
free (dirpath); free (dirpath);
free (mexec); rc_strlist_free (match);
return (retval); return (retval);
} }
librc_hidden_def(rc_service_started_daemon) librc_hidden_def(rc_service_started_daemon)
@ -530,7 +529,7 @@ bool rc_service_daemons_crashed (const char *service)
continue; continue;
} }
if (strcmp (token, "argv") == 0) { if (strncmp (token, "argv_", 5) == 0) {
rc_strlist_add (&argv, p); rc_strlist_add (&argv, p);
} else if (strcmp (token, "exec") == 0) { } else if (strcmp (token, "exec") == 0) {
if (exec) if (exec)

View File

@ -190,7 +190,7 @@ pid_t rc_service_stop (const char *service);
* @param exec to check * @param exec to check
* @param indx of the daemon (optional - 1st daemon, 2nd daemon, etc) * @param indx of the daemon (optional - 1st daemon, 2nd daemon, etc)
* @return true if started by this service, otherwise false */ * @return true if started by this service, otherwise false */
bool rc_service_started_daemon (const char *service, const char *exec, bool rc_service_started_daemon (const char *service, const char *const *argv,
int indx); int indx);
/*! Return a saved value for a service /*! Return a saved value for a service

View File

@ -225,20 +225,22 @@ static int do_service (int argc, char **argv)
ok = (rc_service_state (service) & RC_SERVICE_WASINACTIVE); ok = (rc_service_state (service) & RC_SERVICE_WASINACTIVE);
else if (strcmp (applet, "service_started_daemon") == 0) { else if (strcmp (applet, "service_started_daemon") == 0) {
int idx = 0; int idx = 0;
char *d = argv[1]; char *d[] = { argv[1], NULL };
service = getenv ("SVCNAME"); service = getenv ("SVCNAME");
if (argc > 3) { if (argc > 3) {
service = argv[1]; service = argv[1];
d = argv[2]; d[0] = argv[2];
sscanf (argv[3], "%d", &idx); sscanf (argv[3], "%d", &idx);
} else if (argc == 3) { } else if (argc == 3) {
if (sscanf (argv[2], "%d", &idx) != 1) { if (sscanf (argv[2], "%d", &idx) != 1) {
service = argv[1]; service = argv[1];
d = argv[2]; *d = argv[2];
} }
} }
exit (rc_service_started_daemon (service, d, idx) ? 0 : 1); ok = rc_service_started_daemon (service,
(const char * const *)d, idx);
} else } else
eerrorx ("%s: unknown applet", applet); eerrorx ("%s: unknown applet", applet);