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
|
/*
##% gcc -Wall -g -o $STEM $FILE
*/
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
void
swrite(int fd, char *str)
{
if (write(fd, str, strlen(str)) < 0) {
perror("write");
exit(222);
}
}
int
main(int argc, char *argv[])
{
pid_t child;
int lockfd, dirfd;
char lockfile[32];
int pipefd[2];
struct timeval tv;
gettimeofday(&tv, NULL);
int64_t ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
char *path = getenv("NQDIR");
if (!path)
path = ".";
dirfd = open(path, O_RDONLY);
if (dirfd < 0) {
perror("dir open");
exit(111);
}
pipe(pipefd);
child = fork();
if (child == -1) {
perror("fork");
exit(111);
}
else if (child > 0) {
// background
close(pipefd[1]);
char c;
read(pipefd[0], &c, 1);
exit(0);
}
close(pipefd[0]);
child = fork();
if (child == -1) {
perror("fork");
exit(111);
}
else if (child > 0) {
int status;
sprintf(lockfile, ",%lx.%d", ms, child);
dprintf(1, "%s\n", lockfile);
// signal parent to exit
close(pipefd[1]);
wait(&status);
lockfd = openat(dirfd, lockfile, O_RDWR | O_APPEND);
if (lockfd < 0) {
perror("open");
exit(222);
}
fchmod(lockfd, 0600);
if (WIFEXITED(status))
dprintf(lockfd, "\n[exited with status %d.]\n",
WEXITSTATUS(status));
else
dprintf(lockfd, "\n[killed by signal %d.]\n",
WTERMSIG(status));
exit(0);
}
close(pipefd[1]);
sprintf(lockfile, ".,%lx.%d", ms, getpid());
lockfd = openat(dirfd, lockfile, O_CREAT | O_EXCL | O_RDWR | O_APPEND, 0600);
if (lockfd < 0) {
perror("open");
exit(222);
}
flock(lockfd, LOCK_EX);
// drop leading '.'
renameat(dirfd, lockfile, dirfd, lockfile+1);
swrite(lockfd, "exec");
int i;
for (i = 0; i < argc; i++) {
int j, l = strlen(argv[i]);
swrite(lockfd, " '");
for (j = 0; j < l; j++) {
if (argv[i][j] == '\'')
swrite(lockfd, "'\\''");
else
write(lockfd, argv[i]+j, 1);
}
swrite(lockfd, "'");
}
DIR *dir = fdopendir(dirfd);
if (!dir) {
perror("fdopendir");
exit(111);
}
struct dirent *ent;
again:
while ((ent = readdir(dir))) {
if (ent->d_name[0] == ',' &&
strcmp(ent->d_name, lockfile+1) < 0) {
int f = openat(dirfd, ent->d_name, O_RDWR);
if (flock(f, LOCK_EX | LOCK_NB) == -1 &&
errno == EWOULDBLOCK) {
flock(f, LOCK_EX); // sit it out
rewinddir(dir);
goto again;
}
fchmod(f, 0600);
close(f);
}
}
closedir(dir);
// ready to run
write(lockfd, "\n", 1);
fchmod(lockfd, 0700);
dup2(lockfd, 2);
dup2(lockfd, 1);
close(lockfd);
close(dirfd);
execvp(argv[1], argv+1);
return 111;
}
|