mirror of
https://github.com/troglobit/finit.git
synced 2026-10-01 05:22:48 +07:00
Functions pidfile_read() and pidfile_poll() now provied by libite.
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
@@ -74,9 +74,6 @@ void runlevel_set (int pre, int now);
|
||||
int runlevel_get (void);
|
||||
char *runlevel_string (int levels);
|
||||
|
||||
pid_t pidfile_read (char *pidfile);
|
||||
pid_t pidfile_poll (char *cmd, char *path);
|
||||
|
||||
int pid_alive (pid_t pid);
|
||||
char *pid_get_name (pid_t pid, char *name, size_t len);
|
||||
|
||||
|
||||
+1
-1
Submodule libite updated: e4c5a0e77e...8db314623a
@@ -31,81 +31,6 @@
|
||||
#include "libite/lite.h"
|
||||
|
||||
|
||||
/**
|
||||
* pidfile_read - Reads a PID value from a pidfile.
|
||||
* @pidfile: File containing PID, usually in /var/run/<PROC>.pid
|
||||
*
|
||||
* This function takes a @pidfile and returns the PID found therein.
|
||||
*
|
||||
* Returns:
|
||||
* On invalid @pidfile -1 and @errno set to %EINVAL, when @pidfile does not exist -1
|
||||
* and errno set to %ENOENT. When the pidfile is empty or when its contents cannot
|
||||
* be translated this function returns zero (0), on success this function returns
|
||||
* a PID value greater than one. PID 1 is reserved for the system init process.
|
||||
*/
|
||||
pid_t pidfile_read(char *pidfile)
|
||||
{
|
||||
pid_t pid = 0;
|
||||
char buf[16];
|
||||
FILE *fp;
|
||||
|
||||
if (!pidfile) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!fexist(pidfile))
|
||||
return -1;
|
||||
|
||||
fp = fopen(pidfile, "r");
|
||||
if (!fp)
|
||||
return -1;
|
||||
|
||||
if (fgets(buf, sizeof(buf), fp)) {
|
||||
errno = 0;
|
||||
pid = (pid_t)strtol(buf, NULL, 10);
|
||||
if (errno)
|
||||
pid = 0; /* Failed conversion. */
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
/**
|
||||
* pidfile_poll - Poll for the existence of a pidfile and return PID
|
||||
* @cmd: Process name, or command, called to expect a pidfile
|
||||
* @path: Path to pidfile to poll for
|
||||
*
|
||||
* This function polls for the pidfile at @path for at most 5 seconds
|
||||
* before timing out. If the file is created within that time span the
|
||||
* file is read and its PID contents returned.
|
||||
*
|
||||
* Returns:
|
||||
* The PID read from @path, or zero on timeout.
|
||||
*/
|
||||
pid_t pidfile_poll(char *cmd, char *path)
|
||||
{
|
||||
pid_t pid = 0;
|
||||
int tries = 0;
|
||||
|
||||
/* Timeout = 100 * 50ms = 5s */
|
||||
while (!fexist(path) && tries++ < 100) {
|
||||
/* Wait 50ms between retries */
|
||||
usleep(50000);
|
||||
}
|
||||
|
||||
if (!fexist(path)) {
|
||||
_e("Timeout! No PID found for %s, pidfile %s does not exist?", cmd, path);
|
||||
pid = 0;
|
||||
} else {
|
||||
pid = pidfile_read(path);
|
||||
}
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* pid_alive - Check if a given process ID is running
|
||||
* @pid: Process ID to check for.
|
||||
|
||||
Reference in New Issue
Block a user