File: pidfile.c

package info (click to toggle)
noflushd 2.7.5-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 792 kB
  • ctags: 302
  • sloc: ansic: 2,537; sh: 1,142; makefile: 85
file content (187 lines) | stat: -rw-r--r-- 4,059 bytes parent folder | download | duplicates (4)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * <pidfile.c>
 *
 * Copyright (C) 1999, 2000 Daniel Kobras
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * $Id: pidfile.c,v 1.7 2003/10/12 15:32:36 nold Exp $
 *
 */

#include "pidfile.h"
#include "bug.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

struct pidfile_s {
	char	*pidfile;
};

/* 
 * Check for another noflushd already running at <pid>.
 * XXX: This routine is hacky and has quite some evil assumptions
 *      but hopefully will do for a while.
 */

static pid_t check_pid(pid_t pid)
{
	int fd = -1;
	FILE *fp = NULL;
	pid_t ret = 0;
	char buf[1024], *start;

	/* Danger, will only work for pid less than 10^1017! */
	snprintf(buf, 1024, "/proc/%u", pid);
	fd = open(".", O_RDONLY);
	if (chdir(buf) == -1 ||
	    !(fp = fopen("status", "r")))
		goto _out;
	
	while (fgets(buf, 1024, fp) && strncmp(buf, "Name:", 5));
	
	/* Huh!? No Name: field!? */
	if (strncmp(buf, "Name:", 5))
		goto _out;
	
	start = buf + 5;
	while (*start && isspace(*start))
		start++;

	/* Aieee, start still contains trailing newline. Ugly. */
	if (!strcmp(start, "noflushd\n"))
		ret = pid;

_out:
	if (fp)
		fclose(fp);
	if (fd != -1)
		fchdir(fd);
	else
		chdir("/");

	return ret;

}

/* Take pid from pid file (as in <fd>) and check if another noflushd
 * is running with this pid. If yes and fail is non-zero, terminate.
 */

static pid_t check_pid_file(int fd, int fail)
{
	FILE *fp;
	pid_t pid;

	if (!(fp = fdopen(fd, "r"))) {
		ERR("Failed to fdopen");
		return 0;
	}
	if (1 != fscanf(fp, "%u", &pid))
		return 0;
	if (fail && check_pid(pid)) {
		DEBUG("Another noflushd is running at pid %u. "
		      "This computer's too small for two of us!",
			pid);
		exit(0);
	}
	return pid;
}

/*
 * Save our pid to pidfile. Terminate if file exists and
 * another noflushd is running with indicated pid.
 */

pidfile_t pidfile_get(char *pidfile)
{
	int fd = -1;
	FILE *fp = NULL;
	pidfile_t pf = NULL;

	pf = malloc(sizeof(struct pidfile_s));
	if (!pf)
		goto _err;

	/* O_EXCL has a race via NFS. But then /var/run really shouldn't be
	 * shared anyway, so let's keep it simple. This is more of a 
	 * convenience check after all. Nothing too crucial.
	 */
	if ((fd = open(pidfile, O_CREAT | O_EXCL | O_RDWR, 
	               S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
		if (errno == EEXIST) {
			fd = open(pidfile, O_CREAT | O_RDWR,
			          S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
			check_pid_file(fd, 1);
		} else {
			ERR("Cannot open pid file. Proceeding anyway. "
			     "Wish me luck");
			goto _err;
		}
	}
	/* Now it's okay to truncate the file. */
	ftruncate(fd, 0);
	lseek(fd, 0, SEEK_SET);
	if (!(fp = fdopen(fd, "w+"))) {
		ERR("fdopen failed");
		goto _err;
	}
	fprintf(fp, "%u\n", getpid());
	fflush(fp);
	close(fd);
	pf->pidfile=strdup(pidfile);
	return pf;
_err:
	if (fd != -1)
		close(fd);
	free(pf);
	return NULL;
	
}

/*
 * Remove pidfile.
 */

void pidfile_drop(pidfile_t pf)
{
	int fd;

	if (!pf)
		BUG("Invalid pidfile handle");

	if (!pf->pidfile || (fd = open(pf->pidfile, O_RDONLY)) == -1) {
		DEBUG("Cannot open pidfile.");
		goto _out;
	}
	if (check_pid_file(fd, 0) == getpid())
		unlink(pf->pidfile);
	else 
		DEBUG("Found alien pidfile, not removed.");
	
	close(fd);

_out:
	free(pf->pidfile);
	free(pf);
}