Fixed bug - fd of LOCKFILE should be closed before spawn

This commit is contained in:
Ashish Kumar Yadav 2021-08-03 16:58:21 +05:30
parent e2201ce340
commit 5ee5010a26
4 changed files with 91 additions and 65 deletions

View file

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

View file

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

View file

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

View file

@ -72,7 +72,7 @@ sendsignal(int sig, union sigval sv)
exit(1); exit(1);
} }
close(fd); close(fd);
if (fl.l_type == F_UNLCK) { if (fl.l_type != F_WRLCK) {
fputs("Error: no running instance of dwmblocks.\n", stderr); fputs("Error: no running instance of dwmblocks.\n", stderr);
exit(3); exit(3);
} }