libite
Functions
pidfilefn.c File Reference
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
Include dependency graph for pidfilefn.c:

Functions

char * chomp (char *str)
 Perl like chomp function, chop off last char(s) if newline. More...
 
pid_t pidfile_read (const char *pidfile)
 Reads a PID value from a pidfile. More...
 
pid_t pidfile_poll (const char *pidfile)
 Poll for the existence of a pidfile and return PID. More...
 
int pidfile_signal (const char *pidfile, int signal)
 Send signal to a PID and cleanup pidfile afterwards. More...
 

Detailed Description

Author
Joachim Wiberg
Date
2009-2021

Function Documentation

◆ chomp()

char* chomp ( char *  str)

Perl like chomp function, chop off last char(s) if newline.

Parameters
strString to chomp

This function is like Perl chomp, but it's set to chop of all trailing newlines. Useful in combination with fgets().

Returns
str, or NULL with errno set, if str is not a valid pointer.
Exceptions
EINVALif the input argument is not a valid pointer.

◆ pidfile_poll()

pid_t pidfile_poll ( const char *  pidfile)

Poll for the existence of a pidfile and return PID.

Parameters
pidfilePath to pidfile to poll for

This function polls for the pidfile at pidfile 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 pidfile, or zero on timeout.

◆ pidfile_read()

pid_t pidfile_read ( const char *  pidfile)

Reads a PID value from a pidfile.

Parameters
pidfileFile containing PID, usually in /var/run/PROCNAME.pid

This function takes a pidfile and returns the PID found therein.

Returns
On invalid pidfile, -1 with errno set. If 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.
Note
PID 1 is reserved for the system init process.
Exceptions
EINVALon invalid pidfile, or
ENOENTwhen pidfile does not exist.

◆ pidfile_signal()

int pidfile_signal ( const char *  pidfile,
int  signal 
)

Send signal to a PID and cleanup pidfile afterwards.

Parameters
pidfileFile containing PID, usually in /var/run/PROCNAME.pid
signalSignal to send to PID found in pidfile.

If signal is any of SIGTERM or SIGKILL, or if kill(2) returns -1, the pidfile is removed.

Returns
POSIX OK(0) on success, non-zero otherwise.