annotate dglwatcher.c @ 192:addc4e3456c4

Begin adding systemd compatibility. The Makefile can be used to build and install rlgwebd. On systems with systemd, the unit file controls starting and stopping the service, and the shell script is not needed. The unit file uses KillMode=none because socat does not actually stop rlgwebd, it only asks it to stop and exits without waiting for a response. Until a better stopping method is introduced, this setting prevents systemd from killing all the rlgwebd processes as soon as socat exits.
author John "Elwin" Edwards
date Fri, 01 Jan 2016 16:11:34 -0500
parents fba1b34e7554
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
1 #include <stdio.h>
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
2 #include <stdlib.h>
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
3 #include <string.h>
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
4 #include <sys/inotify.h>
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
5 #include <sys/select.h>
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
6 #include <unistd.h>
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
7 #include <limits.h>
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
8 #include <dirent.h>
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
9
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
10 struct watchdir {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
11 int wd;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
12 char *name;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
13 };
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
14
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
15 char ibuf[sizeof(struct inotify_event) + NAME_MAX + 1];
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
16
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
17 int startwatch(int ifd, char *dir, struct watchdir *w) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
18 DIR *dstream;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
19 struct dirent *ent;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
20
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
21 w->name = dir;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
22 w->wd = inotify_add_watch(ifd, dir, IN_CREATE|IN_DELETE|IN_DELETE_SELF);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
23 if (w->wd < 0) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
24 fprintf(stderr, "Could not watch %s\n", dir);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
25 return 1;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
26 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
27 dstream = opendir(dir);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
28 if (dstream == NULL) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
29 fprintf(stderr, "%s is not a readable directory\n", dir);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
30 inotify_rm_watch(ifd, w->wd);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
31 w->wd = -1;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
32 return 1;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
33 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
34 ent = readdir(dstream);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
35 while (ent != NULL) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
36 if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
37 printf("E %s/%s\n", dir, ent->d_name);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
38 ent = readdir(dstream);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
39 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
40 closedir(dstream);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
41 fflush(stdout);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
42 return 0;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
43 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
44
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
45 int main(int argc, char *argv[]) {
166
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
46 int ifd, rsize, off, done, nwatchers, i, result;
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
47 char typecode;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
48 struct inotify_event *iev;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
49 fd_set rfds;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
50 struct watchdir *watchers;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
51
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
52 done = 0;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
53 nwatchers = argc - 1;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
54 iev = (struct inotify_event *) ibuf;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
55 ifd = inotify_init();
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
56 if (nwatchers == 0) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
57 watchers = malloc(sizeof(struct watchdir));
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
58 nwatchers = 1;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
59 startwatch(ifd, ".", watchers);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
60 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
61 else {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
62 watchers = malloc(nwatchers * sizeof(struct watchdir));
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
63 for (i = 0; i < nwatchers; i++) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
64 startwatch(ifd, argv[i+1], watchers + i);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
65 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
66 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
67
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
68 while (!done) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
69 FD_ZERO(&rfds);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
70 FD_SET(0, &rfds);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
71 FD_SET(ifd, &rfds);
166
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
72 result = select(ifd + 1, &rfds, NULL, NULL, NULL);
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
73 if (result < 0) {
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
74 /* Something went wrong. */
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
75 break;
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
76 }
155
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
77 if (FD_ISSET(ifd, &rfds)) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
78 off = 0;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
79 rsize = read(ifd, ibuf, sizeof(struct inotify_event) + NAME_MAX + 1);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
80 while (off < rsize) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
81 iev = (struct inotify_event *) (ibuf + off);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
82 if (iev->mask & IN_CREATE)
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
83 typecode = 'C';
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
84 else if (iev->mask & IN_DELETE)
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
85 typecode = 'D';
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
86 else
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
87 typecode = '?';
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
88 for (i = 0; i < nwatchers; i++) {
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
89 if (watchers[i].wd == iev->wd)
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
90 printf("%c %s/%s\n", typecode, watchers[i].name, iev->name);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
91 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
92 off += sizeof(struct inotify_event) + iev->len;
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
93 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
94 fflush(stdout);
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
95 }
245a2959f504 Begin support for watching dgamelaunch games.
John "Elwin" Edwards
parents:
diff changeset
96 if (FD_ISSET(0, &rfds)) {
166
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
97 result = read(0, &typecode, 1);
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
98 if (result <= 0) {
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
99 /* EOF on stdin: controlling process probably died. */
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards
parents: 155
diff changeset
100 done = 1;
66fef65c34e7 watcher: exit if errors occur.
John "Elwin" Edwards