Renamed getcmd to updateblock

This commit is contained in:
Ashish Kumar Yadav 2021-01-20 13:17:26 +05:30
parent 9329d0dfe6
commit 8c7c4d777b

View file

@ -27,12 +27,12 @@ typedef struct {
static void buttonhandler(int sig, siginfo_t *info, void *ucontext); static void buttonhandler(int sig, siginfo_t *info, void *ucontext);
static void cleanup(); static void cleanup();
static void getcmd(Block *block, int sigval);
static void setroot(); static void setroot();
static void setupsignals(); static void setupsignals();
static void sighandler(int sig, siginfo_t *si, void *ucontext); static void sighandler(int sig, siginfo_t *si, void *ucontext);
static void statusloop(); static void statusloop();
static void termhandler(int sig); static void termhandler(int sig);
static void updateblock(Block *block, int sigval);
static int updatestatus(); static int updatestatus();
static void writepid(); static void writepid();
@ -74,60 +74,6 @@ cleanup()
XCloseDisplay(dpy); XCloseDisplay(dpy);
} }
void
getcmd(Block *block, int sigval)
{
int fd[2];
if (pipe(fd) == -1) {
perror("getcmd - pipe");
exit(1);
}
switch (fork()) {
case -1:
perror("getcmd - fork");
exit(1);
case 0:
close(ConnectionNumber(dpy));
close(fd[0]);
if (fd[1] != STDOUT_FILENO) {
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
perror("getcmd - child - dup2");
exit(1);
}
close(fd[1]);
}
if (sigval == NILL) {
char *arg[] = { block->pathu, NULL };
execv(arg[0], arg);
} else {
char buf[12];
char *arg[] = { block->pathu, buf, NULL };
snprintf(buf, sizeof buf, "%d", sigval);
execv(arg[0], arg);
}
perror("getcmd - child - execv");
_exit(127);
default:
{
size_t trd = 0;
ssize_t rd;
close(fd[1]);
do
rd = read(fd[0], block->cmdoutcur + trd, CMDLENGTH - trd);
while (rd > 0 && (trd += rd) < CMDLENGTH);
if (rd == -1) {
perror("getcmd - read");
exit(1);
}
close(fd[0]);
}
}
}
void void
setroot() setroot()
{ {
@ -194,7 +140,7 @@ sighandler(int sig, siginfo_t *info, void *ucontext)
sig -= SIGRTMIN; sig -= SIGRTMIN;
for (Block *block = blocks; block->pathu; block++) for (Block *block = blocks; block->pathu; block++)
if (block->signal == sig) if (block->signal == sig)
getcmd(block, info->si_value.sival_int); updateblock(block, info->si_value.sival_int);
setroot(); setroot();
} }
@ -208,7 +154,7 @@ statusloop()
sigprocmask(SIG_BLOCK, &blocksigmask, NULL); sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->pathu; block++) for (Block *block = blocks; block->pathu; block++)
if (block->interval >= 0) if (block->interval >= 0)
getcmd(block, NILL); updateblock(block, NILL);
setroot(); setroot();
sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL); sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL);
t.tv_sec = INTERVALs, t.tv_nsec = INTERVALn; t.tv_sec = INTERVALs, t.tv_nsec = INTERVALn;
@ -223,7 +169,7 @@ statusloop()
sigprocmask(SIG_BLOCK, &blocksigmask, NULL); sigprocmask(SIG_BLOCK, &blocksigmask, NULL);
for (Block *block = blocks; block->pathu; block++) for (Block *block = blocks; block->pathu; block++)
if (block->interval > 0 && i % block->interval == 0) if (block->interval > 0 && i % block->interval == 0)
getcmd(block, NILL); updateblock(block, NILL);
setroot(); setroot();
sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL); sigprocmask(SIG_UNBLOCK, &blocksigmask, NULL);
t.tv_sec = INTERVALs, t.tv_nsec = INTERVALn; t.tv_sec = INTERVALs, t.tv_nsec = INTERVALn;
@ -238,6 +184,60 @@ termhandler(int sig)
exit(0); exit(0);
} }
void
updateblock(Block *block, int sigval)
{
int fd[2];
if (pipe(fd) == -1) {
perror("updateblock - pipe");
exit(1);
}
switch (fork()) {
case -1:
perror("updateblock - fork");
exit(1);
case 0:
close(ConnectionNumber(dpy));
close(fd[0]);
if (fd[1] != STDOUT_FILENO) {
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
perror("updateblock - child - dup2");
exit(1);
}
close(fd[1]);
}
if (sigval == NILL) {
char *arg[] = { block->pathu, NULL };
execv(arg[0], arg);
} else {
char buf[12];
char *arg[] = { block->pathu, buf, NULL };
snprintf(buf, sizeof buf, "%d", sigval);
execv(arg[0], arg);
}
perror("updateblock - child - execv");
_exit(127);
default:
{
size_t trd = 0;
ssize_t rd;
close(fd[1]);
do
rd = read(fd[0], block->cmdoutcur + trd, CMDLENGTH - trd);
while (rd > 0 && (trd += rd) < CMDLENGTH);
if (rd == -1) {
perror("updateblock - read");
exit(1);
}
close(fd[0]);
}
}
}
/* returns whether block outputs have changed and updates statustext if they have */ /* returns whether block outputs have changed and updates statustext if they have */
int int
updatestatus() updatestatus()