From 7c9e476867bc4271919dfc367514be0033426344 Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Mon, 18 May 2020 04:16:34 -0400 Subject: exit if event_dir gets deleted out from under us --- jsmond.c | 90 ++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 37 deletions(-) (limited to 'jsmond.c') diff --git a/jsmond.c b/jsmond.c index b0d6d6d..d5e2606 100644 --- a/jsmond.c +++ b/jsmond.c @@ -47,13 +47,14 @@ int button = -1; /* -b */ char *event_dir = EVENTDIR; /* -d */ char *js_node_name = JSNODE; /* -j */ int autodiscover = 1; /* cleared if user supplies joydev args */ -fake_mode_t fake_mode = fm_auto_keycode; /* -b, -m */ +fake_mode_t fake_mode = fm_auto_keycode; /* -k, -b, -m */ /* joystick stuff */ char *joynames[MAX_STICKS + 1]; int last_joyname = 0; int joyfds[MAX_STICKS + 1]; +/* executable name, for warnings/errors */ const char *self; /* X stuff */ @@ -64,6 +65,7 @@ int inotify_fd, watch_fd; char inotify_buf[sizeof(struct inotify_event) + NAME_MAX + 1]; struct inotify_event *inotify_ev = (struct inotify_event *)inotify_buf; +/* not going to include pages of usage info duplicating the man page */ void usage(void) { printf("jsmond v" VERSION " by B. Watson, WTFPL\n"); printf("Usage: %s [-i interval] [-b button | -k keycode] [-j name]\n", self); @@ -160,7 +162,8 @@ void send_fake_motion(void) { 0, 0, 0, - oldx, oldy); + oldx, + oldy); XSync(xdisp, 0); } @@ -204,15 +207,6 @@ void open_joysticks(void) { if(debug) fprintf(stderr, "opened %d devices\n", fdcount); } -void init_joysticks(void) { - int i; - - for(i = 0; i < MAX_STICKS; i++) - joyfds[i] = -1; - - open_joysticks(); -} - void check_inotify() { int res; @@ -222,15 +216,21 @@ void check_inotify() { if(debug) { fprintf(stderr, "read %d bytes from inotify_fd\n", res); if(!inotify_ev->len) { - fprintf(stderr, "got event with no name: %x\n", inotify_ev->mask); + fprintf(stderr, "got event with no name: 0x%x\n", inotify_ev->mask); } else { - fprintf(stderr, "got event with name %s: %x\n", inotify_ev->name, inotify_ev->mask); + fprintf(stderr, "got event with name %s: 0x%x\n", inotify_ev->name, inotify_ev->mask); } } + if(inotify_ev->mask & IN_DELETE_SELF) { + /* not much we can do to recover from this */ + die("someone deleted our device directory!"); + } usleep(EVENT_DELAY); /* might not need, be paranoid */ open_joysticks(); } else { if(errno != EAGAIN) { + /* not sure what could cause this, so no idea how/if we + recover from it */ fprintf(stderr, "%s: failed to read from inotify_fd: %s\n", self, strerror(errno)); exit(1); @@ -346,7 +346,11 @@ void parse_args(int argc, char **argv) { if(argc > 1 && strcmp(argv[1], "--help") == 0) usage(); while(++argv, --argc) { - if(argv[0][0] == '-') { + if(argv[0][0] != '-') { + /* no dash, treat as a joystick device */ + add_joystick_name(*argv); + autodiscover = 0; + } else { if(argv[0][1] && argv[0][2]) die("spaces required between options and arguments, please"); nextarg = argv[1]; @@ -408,9 +412,6 @@ void parse_args(int argc, char **argv) { die("unrecognized argument, try --help"); break; } - } else { - add_joystick_name(*argv); - autodiscover = 0; } } } @@ -459,26 +460,7 @@ void daemonize(void) { chdir("/"); } -void init_inotify(void) { - inotify_fd = inotify_init1(IN_NONBLOCK); - if(inotify_fd < 0) { - fprintf(stderr, "%s: inotify_init1() failed: %s", - self, strerror(errno)); - exit(1); - } - - if(debug) fprintf(stderr, "inotify_init1() returned %d\n", inotify_fd); - - watch_fd = inotify_add_watch(inotify_fd, event_dir, IN_CREATE | IN_DELETE); - if(watch_fd < 0) { - fprintf(stderr, "%s: inotify_add_watch() failed: %s", - self, strerror(errno)); - exit(1); - } - - if(debug) fprintf(stderr, "inotify_add_watch() on %s returned %d\n", event_dir, inotify_fd); -} - +/* helpers for init_* functions */ void connect_to_x(void) { int ignoreme; @@ -529,6 +511,40 @@ void find_keycode() { } } +/* init_* functions. these are only called once each, from main() */ +void init_joysticks(void) { + int i; + + for(i = 0; i < MAX_STICKS; i++) + joyfds[i] = -1; + + open_joysticks(); +} + +void init_inotify(void) { + inotify_fd = inotify_init1(IN_NONBLOCK); + if(inotify_fd < 0) { + fprintf(stderr, "%s: inotify_init1() failed: %s", + self, strerror(errno)); + exit(1); + } + + if(debug) fprintf(stderr, "inotify_init1() returned %d\n", inotify_fd); + + watch_fd = inotify_add_watch( + inotify_fd, + event_dir, + IN_CREATE | IN_DELETE | IN_DELETE_SELF); + + if(watch_fd < 0) { + fprintf(stderr, "%s: inotify_add_watch(\"%s\") failed: %s\n", + self, event_dir, strerror(errno)); + exit(1); + } + + if(debug) fprintf(stderr, "inotify_add_watch() on %s returned %d\n", event_dir, inotify_fd); +} + void init_x(void) { connect_to_x(); -- cgit v1.2.3