aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/lp/LOCK.c
blob: 4ca1ec3ab0cf9ae682afca0fba6ecbe489d3ecdf (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
#include <u.h>
#include <libc.h>

/* MAXHOSTNAMELEN is in sys/param.h */
#define MAXHOSTNAMELEN	64

char lockstring[MAXHOSTNAMELEN+8];

void
main(int argc, char *argv[]) {
	char *lockfile;
	int fd, ppid, ssize;
	struct Dir *statbuf;

	if (argc != 4) {
		fprint(2, "usage: LOCK lockfile hostname ppid\n");
		exits("lock failed on usage");
	}
	lockfile = argv[1];
	if ((fd=create(lockfile, OLOCK|ORDWR, 0666)) < 0) {
		exits("lock failed on create");
	}
	ppid = atoi(argv[3]);
	ssize = sprint(lockstring, "%s %s\n", argv[2], argv[3]);
	if (write(fd, lockstring, ssize) != ssize) {
		fprint(2, "LOCK:write(): %r\n");
		exits("lock failed on write to lockfile");
	}

	switch(fork()) {
	default:
		exits("");
	case 0:
		break;
	case -1:
		fprint(2, "LOCK:fork(): %r\n");
		exits("lock failed on fork");
	}

	for(;;) {
		statbuf = dirfstat(fd);
		if(statbuf == nil)
			break;
		if (statbuf->length == 0){
			free(statbuf);
			break;
		}
		free(statbuf);
		if (write(fd, "", 0) < 0)
			break;
		sleep(3000);
	}

	close(fd);
	postnote(PNGROUP, ppid, "kill");
	exits("");
}