File: killprev.c

package info (click to toggle)
zmailer 2.99.51.52pre3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 16,596 kB
  • ctags: 7,422
  • sloc: ansic: 90,470; sh: 3,608; makefile: 2,784; perl: 1,585; python: 115; awk: 22
file content (74 lines) | stat: -rw-r--r-- 1,728 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 *	Copyright 1988 by Rayan S. Zachariassen, all rights reserved.
 *	This will be free software, but only when it is finished.
 */

#include "hostenv.h"
#include "mailer.h"
#include "zmsignal.h" /* used for kill(2) */
#include "libz.h"
#include "libc.h"

#ifndef	_IOFBF
#define	_IOFBF	0
#endif	/* !_IOFBF */

/* send a signal to an already-running daemon process or process group */

int
killprevious(sig, pidfile)
	int sig;
	const char *pidfile;
{
	int c, rc;
	FILE *fp;
	char *path, buf[128];

	path = emalloc((unsigned)(strlen(postoffice) + strlen(pidfile) + 2));
	sprintf(path, "%s/%s", postoffice, pidfile);
	if (sig != 0 && (fp = fopen(path, "r")) != NULL) {
		setvbuf(fp, buf, _IOFBF, sizeof buf);
		if (fscanf(fp, "%d", &c) != 1)
			fprintf(stderr,
				"couln't make sense of contents of %s!\n",
				path);
		else if (c == getpid())
			printf("was about to commit suicide\n");
		else if ((sig < 0 && kill(-c, -sig) == 0 && sig == -SIGTERM)
			 || (sig > 0 && kill(c, sig) == 0 && sig == SIGTERM)) {
			printf("killed previous daemon%s = %d\n",
			       (sig > 0) ? ", pid" : "s, pgrp", c);
			sleep(5); /* Give process time to die */
		}
		fclose(fp);
	}
	rc = 0;
	if ((sig == 0 || sig == SIGTERM || -sig == SIGTERM)
	    && (fp = fopen(path, "w+")) != NULL) {
		setvbuf(fp, buf, _IOFBF, sizeof buf);
		fprintf(fp, "%d\n", (int)getpid());
		rc = fflush(fp);
		if (fclose(fp) != 0)
		  rc |= 1; /* indicate error */
		if (rc != 0)
		  unlink(path);
	}
	free(path);
	return rc;
}

int
killpidfile(pidfile)
	const char *pidfile;
{
	int rc;
	char *path;

	path = emalloc((unsigned)(strlen(postoffice) + strlen(pidfile) + 2));
	sprintf(path, "%s/%s", postoffice, pidfile);

	rc = unlink(path);

	free(path);
	return rc;
}