@@ -71,13 +71,18 @@ static int fcntl_unlock(int fd, off_t off, off_t len)
return fcntl(fd, F_SETLKW, &fl);
}
+/* The PIDs of all children */
+static pid_t *cpids;
+
static void
kill_children()
{
siginfo_t infop;
+ int i;
signal(SIGINT, SIG_IGN);
- kill(0, SIGINT);
+ for (i = 0; cpids[i]; i++)
+ kill(cpids[i], SIGINT);
while (waitid(P_ALL, 0, &infop, WEXITED) != -1);
}
@@ -161,14 +166,21 @@ int main(int argc, char *argv[])
signal(SIGINT, sighandler);
+ cpids = malloc((num + 1) * sizeof(pid_t));
+ if (!cpids)
+ err(1, "malloc");
+ cpids[num] = 0;
+
for (i = 0; i < num; i++) {
- switch (fork()) {
+ pid_t pid = fork();
+ switch (pid) {
case 0:
signal(SIGINT, SIG_DFL);
return do_child(lockfd, i, to_lockers[0], from_lockers[1]);
case -1:
err(1, "fork failed");
}
+ cpids[i] = pid;
}
close(to_lockers[0]);
@@ -208,5 +220,6 @@ int main(int argc, char *argv[])
printf("%ld.%09ld\n", total.tv_sec, total.tv_nsec);
kill_children();
+ free(cpids);
return 0;
}