util  Check-in [7a21b1f19e]

Overview
Comment:fix bug where bgrd also scooped stderr, thereby breaking my `surf` setup in neovim
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7a21b1f19e4ce989e8b7edd716ab08d16c01d84962ac5d8d5c0a40c3b5ce357e
User & Date: lexi on 2019-04-23 02:01:14
Other Links: manifest | tags
Context
2019-04-30
23:26
add tenki check-in: ae8a97d783 user: lexi tags: trunk
2019-04-23
02:01
fix bug where bgrd also scooped stderr, thereby breaking my `surf` setup in neovim check-in: 7a21b1f19e user: lexi tags: trunk
2019-04-17
17:02
added more comments check-in: 4b7174b913 user: lexi tags: trunk
Changes

Modified bgrd.c from [d69c7e1283] to [930615166d].

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
		close(rd);
		return 0;
	}

	child: {
		close(rd);
		
		// redirect stdout and stderr to our pty master
		dup2(wr, 1);
		dup2(wr, 2);
		close(wr);

		execv(argv[1],argv+2);
		return 1; // THIS SHOULD NEVER HAPPEN
	}
}







|

<






166
167
168
169
170
171
172
173
174

175
176
177
178
179
180
		close(rd);
		return 0;
	}

	child: {
		close(rd);
		
		// redirect stdout (NOT stderr) to our pty master
		dup2(wr, 1);

		close(wr);

		execv(argv[1],argv+2);
		return 1; // THIS SHOULD NEVER HAPPEN
	}
}

Modified xpriv.c from [afa1600fae] to [c9d2d3baea].

142
143
144
145
146
147
148















149
150
151
152
153
154
155
156
...
202
203
204
205
206
207
208

209
210
211
212
213
214
215
216
...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
...
296
297
298
299
300
301
302

303
304
305
306
307

308
309
310
311
312
313
314





315
316
317
318
319
320
321
...
326
327
328
329
330
331
332

333

334








335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
bool run;

struct termios initial_state;

void sigusr(int a) { if (global -> op == mode_kill) run = false; }
void sigterm(int a) { run = false; }
















enum res register_window(const char* id, bool weak) {
	// the id field denotes the path to the shared memory
	// in use, and allows the user to have multiple
	// contexts by creating aliases to the binary
	int fd = shm_open(id, O_CREAT | O_EXCL | O_RDWR, 0600);
	ftruncate(fd, sizeof(struct signal));
	if (fd == -1) return fail_shm;
	
................................................................................
		}
		XCloseDisplay(xdpy);
	} else {
		// now we start ssh-agent and set the proper environment
		// variables
		pid_t ssha;


		/* messy part */ {
			const char* tmp; //tmpdir defined?
			if (!(tmp = getenv("TMPDIR"))) tmp = "/tmp";
			size_t tmpsz = strlen(tmp);
			
			char sockn[tmpsz + 1 + sizeof "ssh."
				+ 11];
			strcpy(sockn, tmp);
................................................................................
			// assuming ascii…
			for(uint8_t*i=rndid;i<rndid+11;++i) {
				*i = '0' + (*i % (25 * 2 + 10));
				if (*i > '9') *i += 7;
				if (*i > 'Z') *i += ('a' - 'Z');
			}

			if (ssha = fork()) {
				char pid_s_buf[16];
				char* pid_s = itoa(ssha, pid_s_buf, sizeof(pid_s_buf));
				while (access(sockn, F_OK));
					// avoid nasty race condition
				setenv("SSH_AGENT_PID", pid_s, true);
				setenv("SSH_AUTH_SOCK", sockn, true);
				s -> agent = ssha;
			} else {
				close(1); close(0);
				execlp("ssh-agent","ssh-agent","-D", "-a",sockn,0);
			}
		}
		
		pid_t sad;
		int p;
		if (sad = fork()) {
			int added;
			waitpid(sad, &added, 0);
			if (added == 0) {
				if (weak == false) {
................................................................................
	XSync(dpy,false);
	return ok;
}

int main(int sz, char** argv) {
	enum mode op = mode_go;
	bool init_weak = false;


	for(int i = 1; i<sz; ++i) {
		char* v = argv[i];
		if (*v != '-') return bad(fail_arg);
		char* opt = v + 1;

	read_opt:
		switch(*opt) {
			case 'a': op = mode_register; break;
			case 'k': op = mode_kill; break;
			case 'l': op = mode_lock; break;
			case 'h': op = mode_usage; break;
			case 'w': init_weak = true; break;





			default: return bad(fail_opt);
		}
		if(opt[1] != 0) { ++opt; goto read_opt; }
	}

	size_t nsz;
	const char* basename = argv[0], *p;
................................................................................
	char shid[nsz + sizeof shmem_prefix + 0];
	strncpy(shid,shmem_prefix,sizeof shmem_prefix);
	strncpy(shid + sizeof shmem_prefix - 1, basename, nsz);

	if (op == mode_go) {
		int fd;
		if ((fd = shm_open(shid, O_RDWR, 0600)) == -1) {

			execlp("urxvtc", "urxvtc", "-bg", "[80]#4b0024",

					"-e", argv[0], (init_weak?"-aw":"-a"), 0);








		} else {
			struct signal*s = mmap(0, sizeof(struct signal),
					PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
			return bad(activate_window(s->wid));
		}
	} else if (op == mode_register)
		return bad(register_window(shid,init_weak));
	else if (op == mode_kill)
		return bad(kill_window(shid));
	else if (op == mode_usage) {
		write(1,"\e[1musage:\e[0m ",15);
		write(1, argv[0], strlen(argv[0]));
		write(1, " [-aklw [arg]]\n",16);
		return fail_parse;
	}
}








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







 







>
|







 







|
<
<
<
<
<
<
<
|
<
<
|
<
<







 







>





>







>
>
>
>
>







 







>
|
>
|
>
>
>
>
>
>
>
>






|










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
...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
...
241
242
243
244
245
246
247
248







249


250


251
252
253
254
255
256
257
...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
...
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
bool run;

struct termios initial_state;

void sigusr(int a) { if (global -> op == mode_kill) run = false; }
void sigterm(int a) { run = false; }

void spawn(pid_t ssha, const char* const sockn) {
	if (ssha = fork()) {
		char pid_s_buf[16];
		char* pid_s = itoa(ssha, pid_s_buf, sizeof(pid_s_buf));
		while (access(sockn, F_OK));
			// avoid nasty race condition
		setenv("SSH_AGENT_PID", pid_s, true);
		setenv("SSH_AUTH_SOCK", sockn, true);
		global -> agent = ssha;
	} else {
		close(1); close(0);
		execlp("ssh-agent","ssh-agent","-D", "-a",sockn,0);
	}
}

enum res register_window(const char* const id, bool weak, const char* const name) {
	// the id field denotes the path to the shared memory
	// in use, and allows the user to have multiple
	// contexts by creating aliases to the binary
	int fd = shm_open(id, O_CREAT | O_EXCL | O_RDWR, 0600);
	ftruncate(fd, sizeof(struct signal));
	if (fd == -1) return fail_shm;
	
................................................................................
		}
		XCloseDisplay(xdpy);
	} else {
		// now we start ssh-agent and set the proper environment
		// variables
		pid_t ssha;

		if (name == NULL) {
			/* messy part */ 
			const char* tmp; //tmpdir defined?
			if (!(tmp = getenv("TMPDIR"))) tmp = "/tmp";
			size_t tmpsz = strlen(tmp);
			
			char sockn[tmpsz + 1 + sizeof "ssh."
				+ 11];
			strcpy(sockn, tmp);
................................................................................
			// assuming ascii…
			for(uint8_t*i=rndid;i<rndid+11;++i) {
				*i = '0' + (*i % (25 * 2 + 10));
				if (*i > '9') *i += 7;
				if (*i > 'Z') *i += ('a' - 'Z');
			}

			spawn(ssha, sockn);







		} else spawn(ssha, name);


	


		pid_t sad;
		int p;
		if (sad = fork()) {
			int added;
			waitpid(sad, &added, 0);
			if (added == 0) {
				if (weak == false) {
................................................................................
	XSync(dpy,false);
	return ok;
}

int main(int sz, char** argv) {
	enum mode op = mode_go;
	bool init_weak = false;
	const char* init_named = NULL;

	for(int i = 1; i<sz; ++i) {
		char* v = argv[i];
		if (*v != '-') return bad(fail_arg);
		char* opt = v + 1;
		bool seen_string_arg = false;
	read_opt:
		switch(*opt) {
			case 'a': op = mode_register; break;
			case 'k': op = mode_kill; break;
			case 'l': op = mode_lock; break;
			case 'h': op = mode_usage; break;
			case 'w': init_weak = true; break;
			case 'n': if (i == sz -1 || seen_string_arg)
						  return bad(fail_arg);
					  init_named = argv[i+1];
					  seen_string_arg = true;
					  ++ i; break;
			default: return bad(fail_opt);
		}
		if(opt[1] != 0) { ++opt; goto read_opt; }
	}

	size_t nsz;
	const char* basename = argv[0], *p;
................................................................................
	char shid[nsz + sizeof shmem_prefix + 0];
	strncpy(shid,shmem_prefix,sizeof shmem_prefix);
	strncpy(shid + sizeof shmem_prefix - 1, basename, nsz);

	if (op == mode_go) {
		int fd;
		if ((fd = shm_open(shid, O_RDWR, 0600)) == -1) {
			const char* args[] = {
				"urxvtc", "-bg", "[90]#4b0024",
				          "-e",  argv[0],
						  (init_weak?"-aw":"-a"), 0, 0, 0};

			const uint8_t argsz = sizeof args/sizeof(const char*);
			if (init_named != NULL) {
				args[argsz - 3] = "-n"; // im sorry
				args[argsz - 2] = init_named;
			}

			execvp("urxvtc", (char* const*)args);
		} else {
			struct signal*s = mmap(0, sizeof(struct signal),
					PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
			return bad(activate_window(s->wid));
		}
	} else if (op == mode_register)
		return bad(register_window(shid,init_weak,init_named));
	else if (op == mode_kill)
		return bad(kill_window(shid));
	else if (op == mode_usage) {
		write(1,"\e[1musage:\e[0m ",15);
		write(1, argv[0], strlen(argv[0]));
		write(1, " [-aklw [arg]]\n",16);
		return fail_parse;
	}
}