Use O_CLOEXEC flag instead of ugly hack

pull/47/head
Ashish Kumar Yadav 3 years ago
parent 5ee5010a26
commit 56b636492a

@ -32,7 +32,6 @@ static void updateblock(Block *block, int sigval);
static void updatestatus();
static void writepid();
static int lfd;
static Block *dirtyblock;
static Display *dpy;
static sigset_t blocksigmask;
@ -52,7 +51,6 @@ buttonhandler(int sig, siginfo_t *info, void *ucontext)
char button[] = { '0' + (info->si_value.sival_int & 0xff), '\0' };
char *arg[] = { block->pathc, button, NULL };
close(lfd);
close(ConnectionNumber(dpy));
setsid();
execv(arg[0], arg);
@ -184,7 +182,6 @@ updateblock(Block *block, int sigval)
cleanup();
exit(1);
case 0:
close(lfd);
close(ConnectionNumber(dpy));
close(fd[0]);
if (fd[1] != STDOUT_FILENO) {
@ -268,9 +265,11 @@ updatestatus()
void
writepid()
{
int fd;
struct flock fl;
if ((lfd = open(LOCKFILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
if ((fd = open(LOCKFILE, O_RDWR | O_CREAT | O_CLOEXEC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
perror("writepid - open");
exit(1);
}
@ -278,7 +277,7 @@ writepid()
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if (fcntl(lfd, F_SETLK, &fl) == -1) {
if (fcntl(fd, F_SETLK, &fl) == -1) {
if (errno == EACCES || errno == EAGAIN) {
fputs("Error: another instance of dwmblocks is already running.\n", stderr);
exit(2);
@ -286,11 +285,11 @@ writepid()
perror("writepid - fcntl");
exit(1);
}
if (ftruncate(lfd, 0) == -1) {
if (ftruncate(fd, 0) == -1) {
perror("writepid - ftruncate");
exit(1);
}
if (dprintf(lfd, "%ld", (long)getpid()) < 0) {
if (dprintf(fd, "%ld", (long)getpid()) < 0) {
perror("writepid - dprintf");
exit(1);
}

@ -43,7 +43,7 @@ diff -ruN dwm-6.2-ori/config.def.h dwm-6.2/config.def.h
{ ClkClientWin, MODKEY, Button3, resizemouse, {0} },
diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
--- dwm-6.2-ori/dwm.c 2019-02-02 18:25:28.000000000 +0530
+++ dwm-6.2/dwm.c 2021-08-03 16:45:37.574169805 +0530
+++ dwm-6.2/dwm.c 2021-08-03 19:15:58.734660076 +0530
@@ -40,6 +40,7 @@
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
@ -113,15 +113,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
static unsigned int numlockmask = 0;
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
@@ -259,6 +272,7 @@
[PropertyNotify] = propertynotify,
[UnmapNotify] = unmapnotify
};
+static int dblfd = -1; /* file descriptor of DWMBLOCKSLOCKFILE */
static Atom wmatom[WMLast], netatom[NetLast];
static int running = 1;
static Cur *cursor[CurLast];
@@ -416,13 +430,13 @@
@@ -416,13 +429,13 @@
void
buttonpress(XEvent *e)
{
@ -137,7 +129,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
/* focus monitor if necessary */
if ((m = wintomon(ev->window)) && m != selmon) {
unfocus(selmon->sel, 1);
@@ -430,25 +444,29 @@
@@ -430,25 +443,29 @@
focus(NULL);
}
if (ev->window == selmon->barwin) {
@ -181,7 +173,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
for (i = 0; i < LENGTH(buttons); i++)
if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
&& CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
@@ -695,7 +713,7 @@
@@ -695,7 +712,7 @@
void
drawbar(Monitor *m)
{
@ -190,7 +182,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
int boxs = drw->fonts->h / 9;
int boxw = drw->fonts->h / 6 + 2;
unsigned int i, occ = 0, urg = 0;
@@ -703,9 +721,32 @@
@@ -703,9 +720,32 @@
/* draw status first so it can be overdrawn by tags later */
if (m == selmon) { /* status is only drawn on selected monitor */
@ -226,7 +218,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
}
for (c = m->clients; c; c = c->next) {
@@ -724,11 +765,17 @@
@@ -724,11 +764,17 @@
urg & 1 << i);
x += w;
}
@ -246,7 +238,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
if (m->sel) {
drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
@@ -1119,17 +1166,24 @@
@@ -1119,17 +1165,24 @@
motionnotify(XEvent *e)
{
static Monitor *mon = NULL;
@ -279,7 +271,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
}
void
@@ -1564,6 +1618,7 @@
@@ -1564,6 +1617,7 @@
netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
/* init cursors */
cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
@ -287,12 +279,13 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
cursor[CurResize] = drw_cur_create(drw, XC_sizing);
cursor[CurMove] = drw_cur_create(drw, XC_fleur);
/* init appearance */
@@ -1637,6 +1692,36 @@
@@ -1637,6 +1691,37 @@
}
void
+sigdwmblocks(const Arg *arg)
+{
+ static int fd = -1;
+ struct flock fl;
+ union sigval sv;
+
@ -302,17 +295,17 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 0;
+ if (dblfd != -1) {
+ if (fcntl(dblfd, F_GETLK, &fl) != -1 && fl.l_type == F_WRLCK)
+ if (fd != -1) {
+ if (fcntl(fd, F_GETLK, &fl) != -1 && fl.l_type == F_WRLCK)
+ goto signal;
+ close(dblfd);
+ close(fd);
+ fl.l_type = F_WRLCK;
+ }
+ if ((dblfd = open(DWMBLOCKSLOCKFILE, O_RDONLY)) == -1)
+ if ((fd = open(DWMBLOCKSLOCKFILE, O_RDONLY | O_CLOEXEC)) == -1)
+ return;
+ if (fcntl(dblfd, F_GETLK, &fl) == -1 || fl.l_type != F_WRLCK) {
+ close(dblfd);
+ dblfd = -1;
+ if (fcntl(fd, F_GETLK, &fl) == -1 || fl.l_type != F_WRLCK) {
+ close(fd);
+ fd = -1;
+ return;
+ }
+signal:
@ -324,15 +317,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
spawn(const Arg *arg)
{
if (arg->v == dmenucmd)
@@ -1644,6 +1729,7 @@
if (fork() == 0) {
if (dpy)
close(ConnectionNumber(dpy));
+ close(dblfd);
setsid();
execvp(((char **)arg->v)[0], (char **)arg->v);
fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
@@ -1805,7 +1891,7 @@
@@ -1805,7 +1890,7 @@
XSetWindowAttributes wa = {
.override_redirect = True,
.background_pixmap = ParentRelative,
@ -341,7 +326,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
};
XClassHint ch = {"dwm", "dwm"};
for (m = mons; m; m = m->next) {
@@ -1847,6 +1933,41 @@
@@ -1847,6 +1932,41 @@
(unsigned char *) &(c->win), 1);
}
@ -383,7 +368,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
int
updategeom(void)
{
@@ -1987,9 +2108,27 @@
@@ -1987,9 +2107,27 @@
void
updatestatus(void)
{

@ -43,7 +43,7 @@ diff -ruN dwm-6.2-ori/config.def.h dwm-6.2/config.def.h
{ ClkClientWin, MODKEY, Button3, resizemouse, {0} },
diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
--- dwm-6.2-ori/dwm.c 2020-08-17 23:51:19.057243495 +0530
+++ dwm-6.2/dwm.c 2021-08-03 16:47:58.869157901 +0530
+++ dwm-6.2/dwm.c 2021-08-03 19:19:36.009135193 +0530
@@ -40,6 +40,7 @@
#include <X11/extensions/Xinerama.h>
#endif /* XINERAMA */
@ -121,15 +121,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
static unsigned int numlockmask = 0;
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
@@ -295,6 +308,7 @@
[ResizeRequest] = resizerequest,
[UnmapNotify] = unmapnotify
};
+static int dblfd = -1; /* file descriptor of DWMBLOCKSLOCKFILE */
static Atom wmatom[WMLast], netatom[NetLast], xatom[XLast];
static int running = 1;
static Cur *cursor[CurLast];
@@ -303,6 +317,7 @@
@@ -303,6 +316,7 @@
static Drw *drw;
static Monitor *mons, *selmon;
static Window root, wmcheckwin;
@ -137,7 +129,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
/* configuration, allows nested code to access above variables */
#include "config.h"
@@ -452,13 +467,13 @@
@@ -452,13 +466,13 @@
void
buttonpress(XEvent *e)
{
@ -153,7 +145,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
/* focus monitor if necessary */
if ((m = wintomon(ev->window)) && m != selmon) {
unfocus(selmon->sel, 1);
@@ -466,25 +481,29 @@
@@ -466,25 +480,29 @@
focus(NULL);
}
if (ev->window == selmon->barwin) {
@ -197,7 +189,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
for (i = 0; i < LENGTH(buttons); i++)
if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
&& CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
@@ -789,23 +808,47 @@
@@ -789,23 +807,47 @@
void
drawbar(Monitor *m)
{
@ -252,7 +244,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
for (c = m->clients; c; c = c->next) {
occ |= c->tags;
if (c->isurgent)
@@ -822,11 +865,17 @@
@@ -822,11 +864,17 @@
urg & 1 << i);
x += w;
}
@ -272,7 +264,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
if (m->sel) {
drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
@@ -837,7 +886,9 @@
@@ -837,7 +885,9 @@
drw_rect(drw, x, 0, w, bh, 1, 1);
}
}
@ -283,7 +275,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
}
void
@@ -1243,17 +1294,24 @@
@@ -1243,17 +1293,24 @@
motionnotify(XEvent *e)
{
static Monitor *mon = NULL;
@ -316,7 +308,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
}
void
@@ -1750,6 +1808,7 @@
@@ -1750,6 +1807,7 @@
xatom[XembedInfo] = XInternAtom(dpy, "_XEMBED_INFO", False);
/* init cursors */
cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
@ -324,12 +316,13 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
cursor[CurResize] = drw_cur_create(drw, XC_sizing);
cursor[CurMove] = drw_cur_create(drw, XC_fleur);
/* init appearance */
@@ -1825,6 +1884,36 @@
@@ -1825,6 +1883,37 @@
}
void
+sigdwmblocks(const Arg *arg)
+{
+ static int fd = -1;
+ struct flock fl;
+ union sigval sv;
+
@ -339,17 +332,17 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 0;
+ if (dblfd != -1) {
+ if (fcntl(dblfd, F_GETLK, &fl) != -1 && fl.l_type == F_WRLCK)
+ if (fd != -1) {
+ if (fcntl(fd, F_GETLK, &fl) != -1 && fl.l_type == F_WRLCK)
+ goto signal;
+ close(dblfd);
+ close(fd);
+ fl.l_type = F_WRLCK;
+ }
+ if ((dblfd = open(DWMBLOCKSLOCKFILE, O_RDONLY)) == -1)
+ if ((fd = open(DWMBLOCKSLOCKFILE, O_RDONLY | O_CLOEXEC)) == -1)
+ return;
+ if (fcntl(dblfd, F_GETLK, &fl) == -1 || fl.l_type != F_WRLCK) {
+ close(dblfd);
+ dblfd = -1;
+ if (fcntl(fd, F_GETLK, &fl) == -1 || fl.l_type != F_WRLCK) {
+ close(fd);
+ fd = -1;
+ return;
+ }
+signal:
@ -361,15 +354,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
spawn(const Arg *arg)
{
if (arg->v == dmenucmd)
@@ -1832,6 +1921,7 @@
if (fork() == 0) {
if (dpy)
close(ConnectionNumber(dpy));
+ close(dblfd);
setsid();
execvp(((char **)arg->v)[0], (char **)arg->v);
fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
@@ -2011,7 +2101,7 @@
@@ -2011,7 +2100,7 @@
XSetWindowAttributes wa = {
.override_redirect = True,
.background_pixmap = ParentRelative,
@ -378,7 +363,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
};
XClassHint ch = {"dwm", "dwm"};
for (m = mons; m; m = m->next) {
@@ -2058,6 +2148,41 @@
@@ -2058,6 +2147,41 @@
(unsigned char *) &(c->win), 1);
}
@ -420,7 +405,7 @@ diff -ruN dwm-6.2-ori/dwm.c dwm-6.2/dwm.c
int
updategeom(void)
{
@@ -2198,10 +2323,27 @@
@@ -2198,10 +2322,27 @@
void
updatestatus(void)
{

Loading…
Cancel
Save