File: pid.c

package info (click to toggle)
icmpinfo 1.11-12
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 216 kB
  • ctags: 217
  • sloc: ansic: 1,024; makefile: 50
file content (52 lines) | stat: -rw-r--r-- 904 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

#define PIDFILE                 "/var/run/icmpinfo.pid"

extern char *pname;

void sig_handler(int);

void pid_file(void)
{
    FILE *fp;

    if ((fp = fopen(PIDFILE, "w")) != (FILE *)NULL) {
        fprintf(fp, "%d\n", getpid());
        fclose(fp);
    }
    else
    {
        fprintf(stderr, "\n%s: Could not write PID file `%s', terminating.\n",
            pname, PIDFILE);
        exit(1);
    }
    signal(SIGHUP, sig_handler);
    signal(SIGINT, sig_handler);
    signal(SIGTERM, sig_handler);
}

void sig_handler(int sig)
{
    unlink(PIDFILE);
    exit(0);
}

void pid_kill(void)
{
    FILE *fp;
    int pid;

    if ((fp = fopen(PIDFILE, "r")) != (FILE *)NULL)
    {
        if (fscanf(fp, "%d", &pid) == 1)
        {
            kill(pid, SIGHUP);
            sleep(1);
        }
        fclose(fp);
    }
}