wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

commit c7f08a58cd31e44e2894211349fd773dbdd528d9
parent 8bbd3e810c0314ccd80d78951361e4834230807c
Author: Kris Maglione <bsdaemon@wmii.de>
Date:   Tue, 27 Jun 2006 05:44:54 -0400

Moved permissions checking code and wmiirc execution into wmiiwm


Diffstat:
cmd/wm/wm.c | 80++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
cmd/wm/wmii | 18+-----------------
rc/wmiirc | 8++------
3 files changed, 72 insertions(+), 34 deletions(-)

diff --git a/cmd/wm/wm.c b/cmd/wm/wm.c @@ -3,7 +3,10 @@ * See LICENSE file for license details. */ +#include <errno.h> #include <fcntl.h> +#include <pwd.h> +#include <stdarg.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> @@ -24,7 +27,16 @@ static char version[] = "wmiiwm - " VERSION ", (C)opyright MMIV-MMVI Anselm R. G static void usage() { - fprintf(stderr, "%s", "usage: wmiiwm -a <address> [-c] [-v]\n"); + fprintf(stderr, "%s", "usage: wmiiwm -a <address> [-r <wmiirc>] [-c] [-v]\n"); + exit(1); +} + +static void +error(char *errstr, ...) { + va_list ap; + va_start(ap, errstr); + vfprintf(stderr, errstr, ap); + va_end(ap); exit(1); } @@ -213,9 +225,9 @@ cleanup() int main(int argc, char *argv[]) { - int i; + int i, j; int checkwm = 0; - char *address = nil, *errstr; + char *address = nil, *wmiirc = nil, *namespace, *errstr; XSetWindowAttributes wa; /* command line args */ @@ -235,6 +247,12 @@ main(int argc, char *argv[]) else usage(); break; + case 'r': + if(i + 1 < argc) + wmiirc = argv[++i]; + else + usage(); + break; default: usage(); break; @@ -259,10 +277,8 @@ main(int argc, char *argv[]) XSelectInput(blz.display, blz.root, SubstructureRedirectMask | EnterWindowMask); XSync(blz.display, False); - if(other_wm_running) { - fputs("wmiiwm: another window manager is already running\n", stderr); - exit(1); - } + if(other_wm_running) + error("wmiiwm: another window manager is already running\n"); if(checkwm) { XCloseDisplay(blz.display); exit(0); @@ -271,13 +287,55 @@ main(int argc, char *argv[]) if(!address) usage(); + /* Check namespace permissions */ + if(!strncmp(address, "unix!", 5)) { + struct stat st; + namespace = strdup(&address[5]); + + for(i = strlen(namespace) - 1; i >= 0; i--) + if(namespace[i] == '/') break; + namespace[i+1] = '\0'; + if(stat(namespace, &st)) + error("wmiiwm: can't stat namespace directory \"%s\": %s\n", + namespace, strerror(errno)); + if(getuid() != st.st_uid) + error("wmiiwm: namespace directory \"%s\" exists, but is not owned by you", + namespace); + if(st.st_mode & 077) + error("wmiiwm: namespace directory \"%s\" exists, " + "but has group or world permissions", + namespace); + free(namespace); + } + XSetErrorHandler(0); x_error_handler = XSetErrorHandler(wmii_error_handler); errstr = nil; i = ixp_create_sock(address, &errstr); - if(i < 0) { - fprintf(stderr, "wmii: fatal: %s\n", errstr); - exit(1); + if(i < 0) + error("wmii: fatal: %s\n", errstr); + + /* start wmiirc */ + if(wmiirc) { + int name_len = strlen(wmiirc) + 6; + switch(fork()) { + case 0: + if(setsid() == -1) + error("wmiim: can't setsid: %s\n", strerror(errno)); + j = getdtablesize(); + for(i = 3; i < j; i++) + close(i); + { + char execstr[name_len]; + snprintf(execstr, name_len, "exec %s", wmiirc); + execl("/bin/sh", "sh", "-c", execstr, nil); + } + error("wmiiwm: can't exec \"%s\": %s\n", wmiirc, strerror(errno)); + case -1: + perror("wmiiwm: cannot fork wmiirc"); + default: + break; + } } /* IXP server */ @@ -291,7 +349,7 @@ main(int argc, char *argv[]) lbar = nil; key = nil; - user = getenv("USER"); + user = getlogin(); def.colrules.string = nil; def.colrules.size = 0; diff --git a/cmd/wm/wmii b/cmd/wm/wmii @@ -7,21 +7,5 @@ WMII_CONFPATH="$HOME/.wmii-4:CONFPREFIX/wmii-4" export WMII_CONFPATH WMII_NS_DIR="/tmp/ns.$USER.${DISPLAY%.0}" export WMII_NS_DIR WMII_ADDRESS="unix!$WMII_NS_DIR/wmii" export WMII_ADDRESS -# Make sure that the namespace directory exists and has correct permissions -mkdir -m 700 "$WMII_NS_DIR" 2>/dev/null || -if ls -ld "$WMII_NS_DIR" | - awk -v "user=`id -un`" \ - '{ if ($3 == user && match($1,"^drwx-----")) - exit 1 }' -then - echo "The socket directory \"$WMII_NS_DIR\" " \ - "is group or world writable " \ - "or is not owned by you" 1>&2 - exit 1 -fi - -wmiiwm -a $WMII_ADDRESS & -wmiiwmpid=$! mkdir $HOME/.wmii-4 2>/dev/null && welcome & -`PATH="$WMII_CONFPATH:$PATH" which wmiirc` & -wait $wmiiwmpid +wmiiwm -a $WMII_ADDRESS -r `PATH="$WMII_CONFPATH:$PATH" which wmiirc` & diff --git a/rc/wmiirc b/rc/wmiirc @@ -29,12 +29,8 @@ WMII_NORMCOLORS='#222222 #eeeeee #666666' export WMII_FONT WMII_SELCOLORS WMII_NORMCOLORS -# give wmiiwm a chance to start -while : -do - echo Start wmiirc | wmiir write /event >/dev/null 2>&1 && break - sleep 1 -done +# stop any running instances or wmiirc +echo Start wmiirc | wmiir write /event || exit 1 # WM CONFIGURATION wmiir write /ctl << EOF