void ExecExitScript() { char *file, *paths; paths = wstrconcat(wusergnusteppath(), "/Library/WindowMaker"); paths = wstrappend(paths, ":"DEF_CONFIG_PATHS); file = wfindfile(paths, DEF_EXIT_SCRIPT); wfree(paths); if (file) { if (system(file) != 0) { wsyserror(_("%s:could not execute exit script"), file); } #if 0 if (fork()==0) { execl("/bin/sh", "/bin/sh", "-c", file, NULL); wsyserror(_("%s:could not execute exit script"), file); exit(1); } #endif wfree(file); } }
void Restart(char *manager, Bool abortOnFailure) { char *prog=NULL; char *argv[MAX_RESTART_ARGS]; int i; if (manager && manager[0]!=0) { prog = argv[0] = strtok(manager, " "); for (i=1; i<MAX_RESTART_ARGS; i++) { argv[i]=strtok(NULL, " "); if (argv[i]==NULL) { break; } } } if (dpy) { #ifdef XSMP_ENABLED wSessionDisconnectManager(); #endif XCloseDisplay(dpy); dpy = NULL; } if (!prog) { execvp(Arguments[0], Arguments); wfatal(_("failed to restart Window Maker.")); } else { execvp(prog, argv); wsyserror(_("could not exec %s"), prog); } if (abortOnFailure) exit(7); }
void ExecuteShellCommand(WScreen *scr, char *command) { static char *shell = NULL; pid_t pid; /* * This have a problem: if the shell is tcsh (not sure about others) * and ~/.tcshrc have /bin/stty erase ^H somewhere on it, the shell * will block and the command will not be executed. if (!shell) { shell = getenv("SHELL"); if (!shell) shell = "/bin/sh"; } */ shell = "/bin/sh"; pid = fork(); if (pid==0) { SetupEnvironment(scr); #ifdef HAVE_SETSID setsid(); #endif execl(shell, shell, "-c", command, NULL); wsyserror("could not execute %s -c %s", shell, command); Exit(-1); } else if (pid < 0) { wsyserror("cannot fork a new process"); } else { _tuple *data = wmalloc(sizeof(_tuple)); data->scr = scr; data->command = wstrdup(command); wAddDeathHandler(pid, (WDeathHandler*)shellCommandHandler, data); } }
static char* getuserhomedir(char *username) { struct passwd *user; user = getpwnam(username); if (!user) { wsyserror(_("could not get password entry for user %s"), username); return NULL; } if (!user->pw_dir) { return "/"; } else { return user->pw_dir; } }
char* wgethomedir() { char *home = getenv("HOME"); struct passwd *user; if (home) return home; user = getpwuid(getuid()); if (!user) { wsyserror(_("could not get password entry for UID %i"), getuid()); return "/"; } if (!user->pw_dir) { return "/"; } else { return user->pw_dir; } }
static char* username() { char *tmp; tmp = getlogin(); if (!tmp) { struct passwd *user; user = getpwuid(getuid()); if (!user) { wsyserror(_("could not get password entry for UID %i"), getuid()); return NULL; } if (!user->pw_name) { return NULL; } else { return user->pw_name; } } return tmp; }
int showCrashDialog(int sig) { int crashAction; dpy = XOpenDisplay(NULL); if (dpy) { /* XXX TODO make sure that window states are saved and restored via netwm */ XGrabServer(dpy); crashAction = wShowCrashingDialogPanel(sig); XCloseDisplay(dpy); dpy = NULL; } else { wsyserror(_("cannot open connection for crashing dialog panel. Aborting.")); crashAction = WMAbort; } if (crashAction == WMStartAlternate) { int i; wmessage(_("trying to start alternate window manager...")); for (i=0; i<WMGetArrayItemCount(wPreferences.fallbackWMs); i++) { Restart(WMGetFromArray(wPreferences.fallbackWMs, i), False); } wfatal(_("failed to start alternate window manager. Aborting.")); return 0; } else if (crashAction == WMAbort) return 0; else return 1; }
static pid_t downloadFile(WMScreen *scr, _Panel *panel, char *file) { pid_t pid; pid = fork(); if (pid < 0) { wsyserror("could not fork() process"); WMRunAlertPanel(scr, GetWindow(panel), _("Error"), "Could not start download. fork() failed", _("OK"), NULL, NULL); return -1; } if (pid != 0) { return pid; } close(ConnectionNumber(WMScreenDisplay(scr))); exit(1); }
int MonitorLoop(int argc, char **argv) { pid_t pid, exited; char **child_argv= wmalloc(sizeof(char*)*(argc+2)); int i, status; time_t last_start; Bool error = False; for (i= 0; i < argc; i++) child_argv[i]= argv[i]; child_argv[i++]= "--for-real"; child_argv[i]= NULL; for (;;) { last_start= time(NULL); /* Start Window Maker */ pid = fork(); if (pid == 0) { execvp(child_argv[0], child_argv); wsyserror(_("Error respawning Window Maker")); exit(1); } else if (pid < 0) { wsyserror(_("Error respawning Window Maker")); exit(1); } do { if ((exited=waitpid(-1, &status, 0)) < 0) { wsyserror(_("Error during monitoring of Window Maker process.")); error = True; break; } } while (exited != pid); if (error) break; child_argv[argc]= "--for-real-"; /* Check if the wmaker process exited due to a crash */ if (WIFSIGNALED(status) && (WTERMSIG(status) == SIGSEGV || WTERMSIG(status) == SIGBUS || WTERMSIG(status) == SIGILL || WTERMSIG(status) == SIGABRT || WTERMSIG(status) == SIGFPE)) { /* If so, we check when was the last restart. * If it was less than 3s ago, it's a bad sign, so we show * the crash panel and ask the user what to do */ if (time(NULL) - last_start < 3) { if (showCrashDialog(WTERMSIG(status)) == 0) { return 1; } } wwarning(_("Window Maker exited due to a crash (signal %i) and will be restarted."), WTERMSIG(status)); } else break; } return 0; }
char* wexpandpath(char *path) { char *origpath = path; char buffer2[PATH_MAX+2]; char buffer[PATH_MAX+2]; int i; memset(buffer, 0, PATH_MAX+2); if (*path=='~') { char *home; path++; if (*path=='/' || *path==0) { home = wgethomedir(); if (strlen(home) > PATH_MAX) goto error; strcat(buffer, home); } else { int j; j = 0; while (*path!=0 && *path!='/') { if (j > PATH_MAX) goto error; buffer2[j++] = *path; buffer2[j] = 0; path++; } home = getuserhomedir(buffer2); if (!home || strlen(home) > PATH_MAX) goto error; strcat(buffer, home); } } i = strlen(buffer); while (*path!=0 && i <= PATH_MAX) { char *tmp; if (*path=='$') { int j = 0; path++; /* expand $(HOME) or $HOME style environment variables */ if (*path=='(') { path++; while (*path!=0 && *path!=')') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); buffer2[j] = 0; } if (*path==')') { path++; tmp = getenv(buffer2); } else { tmp = NULL; } if (!tmp) { if ((i += strlen(buffer2)+2) > PATH_MAX) goto error; buffer[i] = 0; strcat(buffer, "$("); strcat(buffer, buffer2); if (*(path-1)==')') { if (++i > PATH_MAX) goto error; strcat(buffer, ")"); } } else { if ((i += strlen(tmp)) > PATH_MAX) goto error; strcat(buffer, tmp); } } else { while (*path!=0 && *path!='/') { if (j > PATH_MAX) goto error; buffer2[j++] = *(path++); buffer2[j] = 0; } tmp = getenv(buffer2); if (!tmp) { if ((i += strlen(buffer2)+1) > PATH_MAX) goto error; strcat(buffer, "$"); strcat(buffer, buffer2); } else { if ((i += strlen(tmp)) > PATH_MAX) goto error; strcat(buffer, tmp); } } } else { buffer[i++] = *path; path++; } } if (*path!=0) goto error; return wstrdup(buffer); error: errno = ENAMETOOLONG; wsyserror(_("could not expand %s"), origpath); /* FIXME: too many functions handle a return value of NULL incorrectly */ exit(1); }