move get_pid function to a shared file

This commit is contained in:
William Hubbs 2017-08-23 14:16:49 -05:00
parent df28002b72
commit cfbe9c2ede
4 changed files with 30 additions and 54 deletions

View File

@ -71,5 +71,6 @@ bool _rc_can_find_pids(void);
RC_SERVICE lookup_service_state(const char *service); RC_SERVICE lookup_service_state(const char *service);
void from_time_t(char *time_string, time_t tv); void from_time_t(char *time_string, time_t tv);
time_t to_time_t(char *timestring); time_t to_time_t(char *timestring);
pid_t get_pid(const char *applet, const char *pidfile);
#endif #endif

View File

@ -474,3 +474,27 @@ time_t to_time_t(char *timestring)
} }
return result; return result;
} }
pid_t get_pid(const char *applet,const char *pidfile)
{
FILE *fp;
pid_t pid;
if (! pidfile)
return -1;
if ((fp = fopen(pidfile, "r")) == NULL) {
ewarnv("%s: fopen `%s': %s", applet, pidfile, strerror(errno));
return -1;
}
if (fscanf(fp, "%d", &pid) != 1) {
ewarnv("%s: no pid found in `%s'", applet, pidfile);
fclose(fp);
return -1;
}
fclose(fp);
return pid;
}

View File

@ -368,31 +368,6 @@ parse_schedule(const char *string, int timeout)
return; return;
} }
static pid_t
get_pid(const char *pidfile)
{
FILE *fp;
pid_t pid;
if (! pidfile)
return -1;
if ((fp = fopen(pidfile, "r")) == NULL) {
ewarnv("%s: fopen `%s': %s", applet, pidfile, strerror(errno));
return -1;
}
if (fscanf(fp, "%d", &pid) != 1) {
ewarnv("%s: no pid found in `%s'", applet, pidfile);
fclose(fp);
return -1;
}
fclose(fp);
return pid;
}
/* return number of processed killed, -1 on error */ /* return number of processed killed, -1 on error */
static int static int
do_stop(const char *exec, const char *const *argv, do_stop(const char *exec, const char *const *argv,
@ -472,7 +447,7 @@ run_stop_schedule(const char *exec, const char *const *argv,
} }
if (pidfile) { if (pidfile) {
pid = get_pid(pidfile); pid = get_pid(applet, pidfile);
if (pid == -1) if (pid == -1)
return 0; return 0;
} }
@ -1090,7 +1065,7 @@ int main(int argc, char **argv)
} }
if (pidfile) if (pidfile)
pid = get_pid(pidfile); pid = get_pid(applet, pidfile);
else else
pid = 0; pid = 0;
@ -1365,7 +1340,7 @@ int main(int argc, char **argv)
alive = true; alive = true;
} else { } else {
if (pidfile) { if (pidfile) {
pid = get_pid(pidfile); pid = get_pid(applet, pidfile);
if (pid == -1) { if (pid == -1) {
eerrorx("%s: did not " eerrorx("%s: did not "
"create a valid" "create a valid"

View File

@ -147,30 +147,6 @@ static void cleanup(void)
free(changeuser); free(changeuser);
} }
static pid_t get_pid(const char *pidfile)
{
FILE *fp;
pid_t pid;
if (! pidfile)
return -1;
if ((fp = fopen(pidfile, "r")) == NULL) {
ewarnv("%s: fopen `%s': %s", applet, pidfile, strerror(errno));
return -1;
}
if (fscanf(fp, "%d", &pid) != 1) {
ewarnv("%s: no pid found in `%s'", applet, pidfile);
fclose(fp);
return -1;
}
fclose(fp);
return pid;
}
static void child_process(char *exec, char **argv, char *svcname, static void child_process(char *exec, char **argv, char *svcname,
int start_count) int start_count)
{ {
@ -673,7 +649,7 @@ int main(int argc, char **argv)
*exec_file ? exec_file : exec); *exec_file ? exec_file : exec);
if (stop) { if (stop) {
pid = get_pid(pidfile); pid = get_pid(applet, pidfile);
if (pid == -1) if (pid == -1)
i = pid; i = pid;
else else
@ -697,7 +673,7 @@ int main(int argc, char **argv)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
pid = get_pid(pidfile); pid = get_pid(applet, pidfile);
if (pid != -1) if (pid != -1)
if (kill(pid, 0) == 0) if (kill(pid, 0) == 0)
eerrorx("%s: %s is already running", applet, exec); eerrorx("%s: %s is already running", applet, exec);