Project:
PWM
Code Location:
git://github.com/Cougar/pwm.gitmaster
/
Outline
exec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* * pwm/exec.c * * Copyright (c) Tuomo Valkonen 1999-2001. * * You may distribute and modify this program under the terms of either * the Clarified Artistic License or the GNU GPL, version 2 or later. */ #include <unistd.h> #include <sys/types.h> #include <sys/signal.h> #include <signal.h> #include <time.h> #include <string.h> #include <stdlib.h> #include "common.h" #include "exec.h" #include "frame.h" #include "menu.h" #include "property.h" #include "signal.h" #define SHELL_PATH "/bin/sh" #define SHELL_NAME "sh" #define SHELL_ARG "-c" static void do_exec(const char *cmd) { char *argv[4]; wglobal.dpy=NULL; if(cmd==NULL) return; argv[0]=SHELL_NAME; argv[1]=SHELL_ARG; argv[2]=(char*)cmd; /* stupid execve... */ argv[3]=NULL; execvp(SHELL_PATH, argv); die_err_obj(cmd); } void wm_exec(const char *cmd) { int pid; char *tmp; int tmp_len; pid=fork(); if(pid<0) warn_err(); if(pid!=0) return; setup_environ(SCREEN->xscr); close(wglobal.conn); tmp_len = strlen(cmd)+8; tmp=ALLOC_N(char, tmp_len); if(tmp==NULL) die_err(); snprintf(tmp, tmp_len, "exec %s", cmd); do_exec(tmp); } void setup_environ(int scr) { char *tmp, *ptr; char *display; int tmp_len; display=XDisplayName(wglobal.display); tmp_len = strlen(display)+16; tmp=ALLOC_N(char, tmp_len); if(tmp==NULL){ warn_err(); return; } snprintf(tmp, tmp_len, "DISPLAY=%s", display); ptr=strchr(tmp, ':'); if(ptr!=NULL){ ptr=strchr(ptr, '.'); if(ptr!=NULL) *ptr='\0'; } if(scr>=0){ int curr_len=strlen(tmp); snprintf(tmp+curr_len, tmp_len-curr_len, ".%i", scr); } putenv(tmp); /*XFree(display);*/ } /* * Restart and exit */ #define CKILL_TIMEOUT 60 static bool wait_exit() { time_t timeout; if(wglobal.n_alive==0) return TRUE; timeout=CKILL_TIMEOUT+time(NULL); while(1){ sleep(1); if(wglobal.n_alive==0) return TRUE; if(timeout>time(NULL)) return FALSE; } } static void terminate_chld() { int i; for(i=0; i<wglobal.n_children; i++){ if(wglobal.children[i]!=0) kill(wglobal.children[i], SIGTERM); } if(wait_exit()) return; if(wglobal.children[i]!=0) kill(wglobal.children[i], SIGKILL); wait_exit(); } void wm_exitret(int retval) { if(wglobal.parent!=0) kill(wglobal.parent, SIGTERM); else terminate_chld(); deinit(); exit(retval); } void wm_exit() { wm_exitret(EXIT_SUCCESS); } void wm_restart_other(const char *cmd) { if(wglobal.parent!=0){ set_string_property(COMM_WIN, wglobal.atom_private_ipc, cmd); kill(wglobal.parent, SIGUSR1); deinit(); exit(0); }else{ terminate_chld(); set_string_property(COMM_WIN, wglobal.atom_private_ipc, NULL); setup_environ(-1); deinit(); if(cmd!=NULL){ do_exec(cmd); /* failure - try to restart ourselves */ } execvp(wglobal.argv[0], wglobal.argv); die_err_obj(wglobal.argv[0]); } } void wm_restart() { wm_restart_other(NULL); }
