aboutsummaryrefslogtreecommitdiff
path: root/src/librc
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2008-03-17 09:59:52 +0000
committerRoy Marples <roy@marples.name>2008-03-17 09:59:52 +0000
commit50a7697bf207e4919ce893bfc1604fd3a9d807de (patch)
tree1a9c9debaa4d5d862f42c8015826d35b0ce26d7c /src/librc
parent40930d7d0a613aaf6fa124a5963afcae0c30ce7a (diff)
rc_find_pids now returns RC_PIDLIST instead of a NULL terminated array.
Diffstat (limited to 'src/librc')
-rw-r--r--src/librc/librc-daemon.c71
-rw-r--r--src/librc/rc.h9
2 files changed, 44 insertions, 36 deletions
diff --git a/src/librc/librc-daemon.c b/src/librc/librc-daemon.c
index 9ae4ac26..9e58c6a9 100644
--- a/src/librc/librc-daemon.c
+++ b/src/librc/librc-daemon.c
@@ -107,19 +107,18 @@ static bool pid_is_exec(pid_t pid, const char *const *argv)
return true;
}
-pid_t *rc_find_pids(const char *const *argv, const char *cmd,
- uid_t uid, pid_t pid)
+RC_PIDLIST *rc_find_pids(const char *const *argv, const char *cmd,
+ uid_t uid, pid_t pid)
{
DIR *procdir;
struct dirent *entry;
- int npids = 0;
pid_t p;
- pid_t *pids = NULL;
- pid_t *tmp = NULL;
char buffer[PATH_MAX];
struct stat sb;
pid_t runscript_pid = 0;
char *pp;
+ RC_PIDLIST *pids = NULL;
+ RC_PID *pi;
if ((procdir = opendir("/proc")) == NULL)
return NULL;
@@ -159,21 +158,17 @@ pid_t *rc_find_pids(const char *const *argv, const char *cmd,
if (cmd && ! pid_is_cmd(p, cmd))
continue;
- if (argv && ! cmd && ! pid_is_exec(p, (const char *const *)argv))
+ if (argv && ! cmd && !
+ pid_is_exec(p, (const char *const *)argv))
continue;
- tmp = realloc(pids, sizeof (pid_t) * (npids + 2));
- if (! tmp) {
- free(pids);
- closedir(procdir);
- errno = ENOMEM;
- return NULL;
+ if (! pids) {
+ pids = xmalloc(sizeof(*pids));
+ LIST_INIT(pids);
}
- pids = tmp;
-
- pids[npids] = p;
- pids[npids + 1] = 0;
- npids++;
+ pi = xmalloc(sizeof(*pi));
+ pi->pid = p;
+ LIST_INSERT_HEAD(pids, pi, entries);
}
closedir(procdir);
@@ -205,8 +200,8 @@ librc_hidden_def(rc_find_pids)
# define _KVM_FLAGS O_RDONLY
# endif
-pid_t *rc_find_pids(const char *const *argv, const char *cmd,
- uid_t uid, pid_t pid)
+RC_PIDLIST *rc_find_pids(const char *const *argv, const char *cmd,
+ uid_t uid, pid_t pid)
{
static kvm_t *kd = NULL;
char errbuf[_POSIX2_LINE_MAX];
@@ -215,8 +210,8 @@ pid_t *rc_find_pids(const char *const *argv, const char *cmd,
int processes = 0;
int pargc = 0;
char **pargv;
- pid_t *pids = NULL;
- pid_t *tmp;
+ RC_PIDLIST *pids = NULL;
+ RC_PID *pi;
pid_t p;
const char *const *arg;
int npids = 0;
@@ -272,18 +267,13 @@ pid_t *rc_find_pids(const char *const *argv, const char *cmd,
continue;
}
- tmp = realloc(pids, sizeof(pid_t) * (npids + 2));
- if (! tmp) {
- free(pids);
- kvm_close(kd);
- errno = ENOMEM;
- return NULL;
+ if (! pids) {
+ pids = xmalloc(sizeof(*pids));
+ LIST_INIT(pids);
}
- pids = tmp;
-
- pids[npids] = p;
- pids[npids + 1] = 0;
- npids++;
+ pi = xmalloc(sizeof(*pi));
+ pi->pid = p;
+ LIST_INSERT_HEAD(pids, pi, entries);
}
kvm_close(kd);
@@ -498,7 +488,9 @@ bool rc_service_daemons_crashed(const char *service)
char *name = NULL;
char *pidfile = NULL;
pid_t pid = 0;
- pid_t *pids = NULL;
+ RC_PIDLIST *pids;
+ RC_PID *p1;
+ RC_PID *p2;
char *p;
char *token;
bool retval = false;
@@ -604,9 +596,18 @@ bool rc_service_daemons_crashed(const char *service)
argv[i] = '\0';
}
- if ((pids = rc_find_pids((const char *const *)argv, name, 0, pid)) == NULL)
+ if ((pids = rc_find_pids((const char *const *)argv,
+ name, 0, pid)) == NULL)
+ {
+ p1 = LIST_FIRST(pids);
+ while (p1) {
+ p2 = LIST_NEXT(p1, entries);
+ free(p1);
+ p1 = p2;
+ }
+ free(pids);
retval = true;
- free(pids);
+ }
free(argv);
argv = NULL;
rc_stringlist_free(list);
diff --git a/src/librc/rc.h b/src/librc/rc.h
index 061959e7..bf40e31c 100644
--- a/src/librc/rc.h
+++ b/src/librc/rc.h
@@ -451,6 +451,13 @@ void rc_stringlist_free(RC_STRINGLIST *);
* @return pointer to the new path */
char *rc_strcatpaths(const char *, const char *, ...) SENTINEL;
+typedef struct rc_pid
+{
+ pid_t pid;
+ LIST_ENTRY(rc_pid) entries;
+} RC_PID;
+typedef LIST_HEAD(rc_pidlist, rc_pid) RC_PIDLIST;
+
/*! Find processes based on criteria.
* All of these are optional.
* pid overrides anything else.
@@ -460,6 +467,6 @@ char *rc_strcatpaths(const char *, const char *, ...) SENTINEL;
* @param uid to check for
* @param pid to check for
* @return NULL terminated list of pids */
-pid_t *rc_find_pids(const char *const *, const char *, uid_t, pid_t);
+RC_PIDLIST *rc_find_pids(const char *const *, const char *, uid_t, pid_t);
#endif