fix posible issue with statusbar when reloading wmfs

spawn() now return the child pid
This commit is contained in:
Philippe Pepiot
2010-04-20 03:32:22 +02:00
parent e6b26eafc2
commit 52dba6418e
5 changed files with 52 additions and 31 deletions

View File

@@ -180,45 +180,35 @@ init_root(void)
void void
init_status(void) init_status(void)
{ {
int fd;
struct stat st; struct stat st;
char *home; char *home;
conf.status_pid = -1;
estatus = False;
if(!conf.status_path) if(!conf.status_path)
{ {
if(!(home = getenv("HOME"))) if(!(home = getenv("HOME")))
{ {
warnx("HOME not set, can't launch status.sh"); warnx("HOME not set, can't launch status.sh");
estatus = False;
return; return;
} }
conf.status_path = emalloc(strlen(home) + strlen(DEF_STATUS) + 2, sizeof(char)); conf.status_path = emalloc(strlen(home) + strlen(DEF_STATUS) + 2, sizeof(char));
sprintf(conf.status_path, "%s/"DEF_STATUS, home); sprintf(conf.status_path, "%s/"DEF_STATUS, home);
} }
if(!(fd = open(conf.status_path, O_RDONLY)) if (stat(conf.status_path, &st) == -1)
|| !fopen(conf.status_path, "r"))
{ {
free(conf.status_path); warn("%s", conf.status_path);
estatus = False;
return; return;
} }
stat(conf.status_path, &st);
if(st.st_size && st.st_mode & S_IXUSR) if(st.st_size && st.st_mode & S_IXUSR)
{
estatus = True; estatus = True;
spawn(conf.status_path);
}
else else
warnx("status file specified in configuratin (status_path) or present in wmfs directory can't be executed, try 'chmod +x %s'.", conf.status_path); warnx("status file specified in configuratin (status_path) or present in wmfs directory can't be executed, try 'chmod +x %s'.", conf.status_path);
close(fd);
return; return;
} }

View File

@@ -348,6 +348,7 @@ typedef struct
uint pad; uint pad;
int status_timing; int status_timing;
char *status_path; char *status_path;
pid_t status_pid;
char *autostart_path; char *autostart_path;
char *autostart_command; char *autostart_command;
struct struct

View File

@@ -200,44 +200,69 @@ get_mouse_pos(void)
/** Execute a sh command /** Execute a sh command
* \param cmd Command * \param cmd Command
* \return child pid
*/ */
void int
spawn(const char *format, ...) spawn(const char *format, ...)
{ {
char *sh = NULL; char *sh = NULL;
char cmd[512]; char cmd[512];
va_list ap; va_list ap;
pid_t pid, ret;
if(strlen(format) > 511) int p[2], len;
{
warnx("spawn(): Error, command too long.");
return;
}
va_start(ap, format); va_start(ap, format);
vsprintf(cmd, format, ap); len = vsnprintf(cmd, sizeof(cmd), format, ap);
va_end(ap); va_end(ap);
if(!strlen(cmd)) if (len >= sizeof(cmd))
return; {
warnx("command too long (> 512 bytes)");
return -1;
}
if(!(sh = getenv("SHELL"))) if(!(sh = getenv("SHELL")))
sh = "/bin/sh"; sh = "/bin/sh";
if(fork() == 0) if (pipe(p) == -1)
{ {
if(fork() == 0) warn("pipe");
return -1;
}
if((pid = fork()) == 0)
{
close(p[0]);
if((pid = fork()) == 0)
{ {
if(dpy) if(dpy)
close(ConnectionNumber(dpy)); close(ConnectionNumber(dpy));
setsid(); setsid();
execl(sh, sh, "-c", cmd, (char*)NULL); execl(sh, sh, "-c", cmd, (char*)NULL);
exit(EXIT_SUCCESS); exit(EXIT_FAILURE);
} }
write(p[1], &pid, sizeof(pid_t));
close(p[1]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
else if (pid != -1)
{
close(p[1]);
if (sizeof(pid_t) != read(p[0], &ret, sizeof(pid_t)))
{
warn("read");
ret = -1;
}
close(p[0]);
waitpid(pid, NULL, 0);
}
else
{
warn("fork");
ret = -1;
}
return; return ret;
} }
/** Swap two pointer. /** Swap two pointer.

View File

@@ -148,7 +148,7 @@ thread_process(void *arg)
pthread_detach(pthread_self()); pthread_detach(pthread_self());
do do
{ {
spawn(conf.status_path); conf.status_pid = spawn(conf.status_path);
sleep(conf.status_timing); sleep(conf.status_timing);
} while (!exiting && conf.status_timing != 0); } while (!exiting && conf.status_timing != 0);
} }
@@ -271,6 +271,9 @@ uicb_reload(uicb_t cmd)
{ {
quit(); quit();
if (conf.status_pid != (pid_t)-1)
kill(conf.status_pid, SIGQUIT);
for(; argv_global[0] && argv_global[0] == ' '; ++argv_global); for(; argv_global[0] && argv_global[0] == ' '; ++argv_global);
execlp(argv_global, argv_global, NULL); execlp(argv_global, argv_global, NULL);

View File

@@ -33,6 +33,8 @@
#ifndef WMFS_H #ifndef WMFS_H
#define WMFS_H #define WMFS_H
#define _BSD_SOURCE /* vsnprintf */
#define _POSIX_SOURCE /* kill */
/* Lib headers */ /* Lib headers */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -278,7 +280,7 @@ char* alias_to_str(char *conf_choice);
/* }}} */ /* }}} */
XRectangle get_mouse_pos(void); XRectangle get_mouse_pos(void);
char *char_to_str(const char c); char *char_to_str(const char c);
void spawn(const char *str, ...); int spawn(const char *str, ...);
void swap_ptr(void **x, void **y); void swap_ptr(void **x, void **y);
void uicb_spawn(uicb_t); void uicb_spawn(uicb_t);
char *clean_value(char *str); char *clean_value(char *str);