File: lock.c

package info (click to toggle)
diald 0.99.4-7.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,100 kB
  • ctags: 935
  • sloc: ansic: 7,109; tcl: 977; sh: 880; perl: 306; makefile: 109
file content (97 lines) | stat: -rw-r--r-- 2,243 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
/*
 * lock.c - lock/unlock the serial device.
 *
 * This code is derived from chat.c.
 */

#include "diald.h"


/*
 *	Create a lock file for the named lock device
 */
char * lock(char *dev)
{
    char *lock_file;
    char hdb_lock_buffer[12];
    int fd, pid, n;
    char *p;

    if ((p = strrchr(dev, '/')) != NULL)
	dev = p + 1;
    lock_file = malloc(strlen(lock_prefix) + strlen(dev) + 1);
    if (lock_file == NULL)
	return NULL;
    strcat(strcpy(lock_file, lock_prefix), dev);

    while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
	if (errno == EEXIST
	    && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
	    /* Read the lock file to find out who has the device locked */
	    if (pidstring) {
		n = read(fd, hdb_lock_buffer, 11);
		if (n > 0) {
		    hdb_lock_buffer[n] = 0;
		    pid = atoi(hdb_lock_buffer);
		}
	    } else {
		n = read(fd, &pid, sizeof(pid));
	    }
	    if (n <= 0) {
		mon_syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
		close(fd);
	    } else {
		if (kill(pid, 0) == -1 && errno == ESRCH) {
		    /* pid no longer exists - remove the lock file */
		    if (unlink(lock_file) == 0) {
			close(fd);
			mon_syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
			       dev, pid);
			continue;
		    } else
			mon_syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
			       dev);
#if 1
		} else if (pid == getpid()) {
		    if (unlink(lock_file) == 0) {
			close(fd);
			mon_syslog(LOG_ERR, "Removed stale lock on %s (pid %d - ME?!?)",
			       dev, pid);
			continue;
		    } else
			mon_syslog(LOG_WARNING, "Couldn't remove stale lock on %s (pid %d - ME?!?)",
			       dev);
#endif
		} else
		    mon_syslog(LOG_NOTICE, "Device %s is locked by pid %d",
			   dev, pid);
	    }
	    close(fd);
	} else
	    mon_syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
	free(lock_file);
	return NULL;
    }

    if (pidstring) {
    	sprintf(hdb_lock_buffer, "%10d\n", getpid());
    	write(fd, hdb_lock_buffer, 11);
    } else {
    	pid = getpid();
    	write(fd, &pid, sizeof pid);
    }

    close(fd);
    return lock_file;
}

/*
 *	Remove our lockfile
 */
void unlock(char *lock_file)
{
    if (lock_file) {
	unlink(lock_file);
	free(lock_file);
    }
}