-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.c
157 lines (138 loc) · 3.03 KB
/
lock.c
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
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include "lock.h"
#define P_LOCK "/var/lock"
/*
* Find out name to use for lockfile when locking tty.
*/
static char lockfile[128]; /* UUCP lock file of terminal */
static char username[16];
static int pid;
static int retry=6;
int getLockRetries(void)
{
return retry;
}
int decrementLockRetries(void)
{
retry--;
return retry;
}
char *mbasename(char *s, char *res, int reslen)
{
char *p;
if(strncmp(s, "/dev/", 5) == 0)
{
/* In /dev */
strncpy(res, s + 5, reslen - 1);
res[reslen - 1]=0;
for(p=res; *p; p++)
if(*p == '/')
*p='_';
}
else
{
/* Outside of /dev. Do something sensible. */
if((p=strrchr(s, '/')) == NULL)
p=s;
else
p++;
strncpy(res, p, reslen - 1);
res[reslen - 1]=0;
}
return res;
}
int lockfile_create(void)
{
int fd, n;
char buf[81];
n=umask(022);
/* Create lockfile compatible with UUCP-1.2 and minicom */
if((fd=open(lockfile, O_WRONLY | O_CREAT | O_EXCL, 0666)) < 0)
{
return 0;
}
else
{
snprintf(buf, sizeof (buf), "%05d tty_talk %.20s\n", (int)getpid(),
username);
if(write(fd, buf, strlen(buf)) <= 0)
{
fputs("couldn't write to lockfile\n", stderr);
}
close(fd);
}
umask(n);
return 1;
}
void lockfile_remove(void)
{
if(lockfile[0])
unlink(lockfile);
}
int have_lock_dir(char* dial_tty)
{
struct stat stt;
char buf[128];
if(stat(P_LOCK, &stt) == 0)
{
snprintf(lockfile, sizeof (lockfile),
"%s/LCK..%s",
P_LOCK, mbasename(dial_tty, buf, sizeof (buf)));
}
else
{
fprintf(stderr, "Lock directory %s does not exist\n", P_LOCK);
exit(-1);
}
return 1;
}
int get_lock(char* dial_tty)
{
char buf[128];
int fd, n=0;
have_lock_dir(dial_tty);
if((fd=open(lockfile, O_RDONLY)) >= 0)
{
n=read(fd, buf, 127);
close(fd);
if(n > 0)
{
pid= -1;
if(n == 4)
/* Kermit-style lockfile. */
pid= *((int *)buf);
else
{
/* Ascii lockfile. */
buf[n]=0;
sscanf(buf, "%d", &pid);
}
if(pid > 0 && kill((pid_t)pid, 0) < 0 &&
errno == ESRCH)
{
fprintf(stderr, "Lockfile is stale. Overriding it..\n");
sleep(1);
unlink(lockfile);
}
else
n=0;
}
if(n == 0)
{
if(retry == 1) /* Last retry */
fprintf(stderr, "Device %s is locked.\n", dial_tty);
return 0;
}
}
lockfile_create();
return 1;
}