add macro for fatal errors

dwm lets the OS do cleanup if the program errors out
This commit is contained in:
Devin J. Pohly 2020-08-18 19:39:34 -05:00
parent d615d3836c
commit 83f60e89b0

44
dwl.c
View file

@ -40,6 +40,8 @@
#endif #endif
/* macros */ /* macros */
#define BARF(fmt, ...) do { fprintf(stderr, fmt "\n", ##__VA_ARGS__); exit(EXIT_FAILURE); } while (0)
#define EBARF(fmt, ...) BARF(fmt ": %s", ##__VA_ARGS__, strerror(errno))
#define MAX(A, B) ((A) > (B) ? (A) : (B)) #define MAX(A, B) ((A) > (B) ? (A) : (B))
#define MIN(A, B) ((A) < (B) ? (A) : (B)) #define MIN(A, B) ((A) < (B) ? (A) : (B))
#define CLEANMASK(mask) (mask & ~WLR_MODIFIER_CAPS) #define CLEANMASK(mask) (mask & ~WLR_MODIFIER_CAPS)
@ -1280,20 +1282,13 @@ run(char *startup_cmd)
/* Add a Unix socket to the Wayland display. */ /* Add a Unix socket to the Wayland display. */
const char *socket = wl_display_add_socket_auto(dpy); const char *socket = wl_display_add_socket_auto(dpy);
if (!socket) { if (!socket)
perror("startup: display_add_socket_auto"); BARF("startup: display_add_socket_auto");
wlr_backend_destroy(backend);
exit(EXIT_FAILURE);
}
/* Start the backend. This will enumerate outputs and inputs, become the DRM /* Start the backend. This will enumerate outputs and inputs, become the DRM
* master, etc */ * master, etc */
if (!wlr_backend_start(backend)) { if (!wlr_backend_start(backend))
perror("startup: backend_start"); BARF("startup: backend_start");
wlr_backend_destroy(backend);
wl_display_destroy(dpy);
exit(EXIT_FAILURE);
}
/* Now that outputs are initialized, choose initial selmon based on /* Now that outputs are initialized, choose initial selmon based on
* cursor position, and set default cursor image */ * cursor position, and set default cursor image */
@ -1311,16 +1306,11 @@ run(char *startup_cmd)
setenv("WAYLAND_DISPLAY", socket, 1); setenv("WAYLAND_DISPLAY", socket, 1);
if (startup_cmd) { if (startup_cmd) {
startup_pid = fork(); startup_pid = fork();
if (startup_pid < 0) { if (startup_pid < 0)
perror("startup: fork"); EBARF("startup: fork");
wl_display_destroy(dpy);
exit(EXIT_FAILURE);
}
if (startup_pid == 0) { if (startup_pid == 0) {
execl("/bin/sh", "/bin/sh", "-c", startup_cmd, (void *)NULL); execl("/bin/sh", "/bin/sh", "-c", startup_cmd, (void *)NULL);
perror("startup: execl"); EBARF("startup: execl");
wl_display_destroy(dpy);
exit(EXIT_FAILURE);
} }
} }
/* Run the Wayland event loop. This does not return until you exit the /* Run the Wayland event loop. This does not return until you exit the
@ -1586,10 +1576,8 @@ setup(void)
void void
sigchld(int unused) sigchld(int unused)
{ {
if (signal(SIGCHLD, sigchld) == SIG_ERR) { if (signal(SIGCHLD, sigchld) == SIG_ERR)
perror("can't install SIGCHLD handler"); EBARF("can't install SIGCHLD handler");
exit(EXIT_FAILURE);
}
while (0 < waitpid(-1, NULL, WNOHANG)) while (0 < waitpid(-1, NULL, WNOHANG))
; ;
} }
@ -1600,9 +1588,7 @@ spawn(const Arg *arg)
if (fork() == 0) { if (fork() == 0) {
setsid(); setsid();
execvp(((char **)arg->v)[0], (char **)arg->v); execvp(((char **)arg->v)[0], (char **)arg->v);
fprintf(stderr, "dwl: execvp %s", ((char **)arg->v)[0]); EBARF("dwl: execvp %s failed", ((char **)arg->v)[0]);
perror(" failed");
exit(EXIT_FAILURE);
} }
} }
@ -1941,10 +1927,8 @@ main(int argc, char *argv[])
// Wayland requires XDG_RUNTIME_DIR for creating its communications // Wayland requires XDG_RUNTIME_DIR for creating its communications
// socket // socket
if (!getenv("XDG_RUNTIME_DIR")) { if (!getenv("XDG_RUNTIME_DIR"))
fprintf(stderr, "XDG_RUNTIME_DIR must be set\n"); BARF("XDG_RUNTIME_DIR must be set");
exit(EXIT_FAILURE);
}
/* The Wayland display is managed by libwayland. It handles accepting /* The Wayland display is managed by libwayland. It handles accepting
* clients from the Unix socket, manging Wayland globals, and so on. */ * clients from the Unix socket, manging Wayland globals, and so on. */