aboutsummaryrefslogtreecommitdiff
path: root/src/pid_file.c
blob: 616446391f9fa24eb8dcb5143709cd831ba6a1f5 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* This file is part of genrc
Copyryght (C) 2018 Sergey Poznyakoff
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
*/
#include "genrc.h"

struct file_pid_closure {
	struct genrc_pid_closure generic;
	char *filename;
};

pid_t
file_read_pid(char const *filename)
{
	FILE *fp;
	char buf[256];
	int n, ec, ok;
	unsigned long pid;
	char *end;
		
	fp = fopen(filename, "r");
	if (!fp) {
		if (errno == ENOENT)
			return 0;
		system_error(errno, "can't open file %s", filename);
		return -1;
	}
	ok = fgets(buf, sizeof(buf), fp) != NULL;
	ec = errno;
	fclose(fp);
	if (!ok) {
		system_error(ec, "error reading from %s", filename);
		return -1;
	}
	n = strlen(buf);
	if (n == 0) {
		genrc_error("invalid line read from %s", filename);
		return -1;
	}
	if (buf[n-1] == '\n')
		buf[n-1] = 0;
	errno = 0;
	pid = strtoul(buf, &end, 10);
	if (errno || *end) {
		genrc_error("invalid line read from %s", filename);
		return -1;
	}
	return pid_is_running(pid) ? pid : 0;
}

static int
pid_file_get(GENRC_PID_CLOSURE *clos, PIDLIST *plist)
{
	struct file_pid_closure *file_clos = (struct file_pid_closure *)clos;
	pid_t pid = file_read_pid(file_clos->filename);
	if (pid == -1)
		return -1;
	if (pid > 0)
		pidlist_add(plist, pid);
	return 0;
}

GENRC_PID_CLOSURE *
genrc_pid_file_init(int argc, char **argv)
{
	struct file_pid_closure *clos;
	
	if (argc != 2)
		usage_error("%s: expected format: FILE:<FILENAME>", argv[0]);
	clos = xmalloc(sizeof(*clos));
	clos->generic.pid = pid_file_get;
	clos->filename = xstrdup(argv[1]);
	return (GENRC_PID_CLOSURE *)clos;
}

Return to:

Send suggestions and report system problems to the System administrator.