summaryrefslogtreecommitdiffstats
path: root/kdm/backend/process.c
blob: 8e21d789bee0e0acd928153c50a7768e18487e88 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
322
323
324
325
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*

Copyright 1988, 1998  The Open Group
Copyright 2001-2004 Oswald Buddenhagen <ossi@kde.org>

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from the copyright holder.

*/

/*
 * xdm - display manager daemon
 * Author: Keith Packard, MIT X Consortium
 *
 * subdaemon and external process management and communication
 */

#include "dm.h"
#include "dm_error.h"

#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
#include <signal.h>
#include <unistd.h>
#ifdef _POSIX_PRIORITY_SCHEDULING
# include <sched.h>
#endif

extern char **environ;


SIGFUNC Signal( int sig, SIGFUNC handler )
{
#ifndef __EMX__
	struct sigaction sigact, osigact;
	sigact.sa_handler = handler;
	sigemptyset( &sigact.sa_mask );
# ifdef SA_RESTART
	sigact.sa_flags = SA_RESTART;
# else
	sigact.sa_flags = 0;
# endif
	sigaction( sig, &sigact, &osigact );
	return osigact.sa_handler;
#else
	return signal( sig, handler );
#endif
}


void
TerminateProcess( int pid, int sig )
{
	kill( pid, sig );
#ifdef SIGCONT
	kill( pid, SIGCONT );
#endif
}


static FD_TYPE CloseMask;
static int max = -1;

void
RegisterCloseOnFork( int fd )
{
	FD_SET( fd, &CloseMask );
	if (fd > max)
		max = fd;
}

void
ClearCloseOnFork( int fd )
{
	FD_CLR( fd, &CloseMask );
}

void
CloseNClearCloseOnFork( int fd )
{
	close( fd );
	FD_CLR( fd, &CloseMask );
}

static void
CloseOnFork( void )
{
	int fd;

	for (fd = 0; fd <= max; fd++)
		if (FD_ISSET( fd, &CloseMask ))
			close( fd );
	FD_ZERO( &CloseMask );
	max = -1;
}

int
Fork()
{
	int pid;

	sigset_t ss, oss;
	sigfillset( &ss );
	sigprocmask( SIG_SETMASK, &ss, &oss );

	if (!(pid = fork())) {
#ifdef SIGCHLD
		(void)Signal( SIGCHLD, SIG_DFL );
#endif
		(void)Signal( SIGTERM, SIG_DFL );
		(void)Signal( SIGINT, SIG_IGN ); /* for -nodaemon */
		(void)Signal( SIGPIPE, SIG_DFL );
		(void)Signal( SIGALRM, SIG_DFL );
		(void)Signal( SIGHUP, SIG_DFL );
		sigemptyset( &ss );
		sigprocmask( SIG_SETMASK, &ss, NULL );
		CloseOnFork();
		return 0;
	}

	sigprocmask( SIG_SETMASK, &oss, 0 );

	return pid;
}

int
Wait4( int pid )
{
	waitType result;

	while (waitpid( pid, &result, 0 ) < 0)
		if (errno != EINTR) {
			Debug( "Wait4(%d) failed: %m\n", pid );
			return 0;
		}
	return waitVal( result );
}


void
execute( char **argv, char **env )
{
	Debug( "execute: %[s ; %[s\n", argv, env );
	execve( argv[0], argv, env );
	/*
	 * In case this is a shell script which hasn't been
	 * made executable (or this is a SYSV box), do
	 * a reasonable thing
	 */
	if (errno != ENOENT) {
		char **newargv;
		FILE *f;
		int nu;
		char program[1024];

		/*
		 * emulate BSD kernel behaviour -- read
		 * the first line; check if it starts
		 * with "#!", in which case it uses
		 * the rest of the line as the name of
		 * program to run.	Else use "/bin/sh".
		 */
		if (!(f = fopen( argv[0], "r" )))
			return;
		if (!fGets( program, sizeof(program), f )) {
			fclose( f );
			return;
		}
		fclose( f );
		if (!strncmp( program, "#!", 2 ))
			newargv = parseArgs( 0, program + 2 );
		else
			newargv = addStrArr( 0, "/bin/sh", 7 );
		if (!newargv)
			return;
		nu = arrLen( newargv );
		if (!(argv = xCopyStrArr( nu, argv )))
			return;
		memcpy( argv, newargv, sizeof(char *) * nu );
		Debug( "shell script execution: %[s\n", argv );
		execve( argv[0], argv, env );
	}
}

int
runAndWait( char **args, char **env )
{
	int pid, ret;

	switch (pid = Fork()) {
	case 0:
		execute( args, env );
		LogError( "Can't execute %\"s: %m\n", args[0] );
		exit( 127 );
	case -1:
		LogError( "Can't fork to execute %\"s: %m\n", args[0] );
		return 1;
	}
	ret = Wait4( pid );
	return waitVal( ret );
}

FILE *
pOpen( char **what, char m, int *pid )
{
	int dp[2];

	if (pipe( dp ))
		return 0;
	switch ((*pid = Fork())) {
	case 0:
		if (m == 'r')
			dup2( dp[1], 1 );
		else
			dup2( dp[0], 0 );
		close( dp[0] );
		close( dp[1] );
		execute( what, environ );
		LogError( "Can't execute %\"s: %m\n", what[0] );
		exit( 127 );
	case -1:
		close( dp[0] );
		close( dp[1] );
		LogError( "Can't fork to execute %\"s: %m\n", what[0] );
		return 0;
	}
	if (m == 'r') {
		close( dp[1] );
		return fdopen( dp[0], "r" );
	} else {
		close( dp[0] );
		return fdopen( dp[1], "w" );
	}
}

int
pClose( FILE *f, int pid )
{
	fclose( f );
	return Wait4( pid );
}

char *
locate( const char *exe )
{
	int len;
	char *path, *name, *thenam, nambuf[PATH_MAX+1];
	char *pathe;

	if (!(path = getenv( "PATH" ))) {
		LogError( "Can't execute %'s: $PATH not set.\n", exe );
		return 0;
	}
	len = strlen( exe );
	name = nambuf + PATH_MAX - len;
	memcpy( name, exe, len + 1 );
	*--name = '/';
	do {
		if (!(pathe = (char*)strchr( path, ':' )))
			pathe = path + strlen( path );
		len = pathe - path;
		if (len && !(len == 1 && *path == '.')) {
			thenam = name - len;
			if (thenam >= nambuf) {
				memcpy( thenam, path, len );
				if (!access( thenam, X_OK )) {
					StrDup( &name, thenam );
					return name;
				}
			}
		}
		path = pathe;
	} while (*path++ != '\0');
	LogError( "Can't execute %'s: not in $PATH.\n", exe );
	return 0;
}


static GTalk *curtalk;

void
GSet( GTalk *tlk )
{
	curtalk = tlk;
}

int
GFork( GPipe *pajp, const char *pname, char *cname,
       GPipe *ogp, char *cgname )
{
	int opipe[2], ipipe[2], ogpipe[2], igpipe[2], pid;

	if (pipe( opipe ))
		goto badp1;
	if (pipe( ipipe ))
		goto badp2;
	if (ogp) {
		if (pipe( ogpipe ))
			goto badp3;
		if (pipe( igpipe )) {
			close( ogpipe[0] );
			close( ogpipe[1] );
		  badp3:
			close( ipipe[0] );
			close( ipipe[1] );
		  badp2:
			close( opipe[0] );
			close( opipe[1] );
		  badp1:
			LogError( "Cannot start %s, pipe() failed", cname );
			if (cname)
				free( cname );
			return -1;
		}
	}
	RegisterCloseOnFork( opipe[1] );
	RegisterCloseOnFork( ipipe[0] );
	if (ogp) {
		RegisterCloseOnFork( ogpipe[1] );
		RegisterCloseOnFork( igpipe[0] );
	}
	switch (pid = Fork()) {
	case -1:
		close( opipe[0] );
		close( ipipe[1] );
		CloseNClearCloseOnFork( opipe[1] );
		CloseNClearCloseOnFork( ipipe[0] );
		if (ogp) {
			close( ogpipe[0] );
			close( igpipe[1] );
			CloseNClearCloseOnFork( ogpipe[1] );
			CloseNClearCloseOnFork( igpipe[0] );
		}
		LogError( "Cannot start %s, fork() failed\n", cname );
		if (cname)
			 free( cname );
		return -1;
	case 0:
		pajp->wfd = ipipe[1];
		RegisterCloseOnFork( ipipe[1] );
		pajp->rfd = opipe[0];
		RegisterCloseOnFork( opipe[0] );
		pajp->who = (char *)pname;
		if (ogp) {
			ogp->wfd = igpipe[1];
			RegisterCloseOnFork( igpipe[1] );
			ogp->rfd = ogpipe[0];
			RegisterCloseOnFork( ogpipe[0] );
			ogp->who = (char *)pname;
		}
		break;
	default:
		close( opipe[0] );
		close( ipipe[1] );
		pajp->rfd = ipipe[0];
		pajp->wfd = opipe[1];
		pajp->who = cname;
		if (ogp) {
			close( ogpipe[0] );
			close( igpipe[1] );
			ogp->rfd = igpipe[0];
			ogp->wfd = ogpipe[1];
			ogp->who = cgname;
		}
		break;
	}
	return pid;
}

int
GOpen( GProc *proc, char **argv, const char *what, char **env, char *cname,
       GPipe *gp )
{
	char **margv;
	int pip[2];
	char coninfo[32];

/* ###	GSet (proc->pipe); */
	if (proc->pid) {
		LogError( "%s already running\n", cname );
		if (cname)
			free( cname );
		return -1;
	}
	if (!(margv = xCopyStrArr( 1, argv ))) {
		if (cname)
			free( cname );
		return -1;
	}
	if (!StrApp( margv, progpath, what, (char *)0 )) {
		free( margv );
		if (cname)
			free( cname );
		return -1;
	}
	if (pipe( pip )) {
		LogError( "Cannot start %s, pipe() failed\n", cname );
		if (cname)
			free( cname );
		goto fail;
	}
	if (gp) {
		ClearCloseOnFork( gp->rfd );
		ClearCloseOnFork( gp->wfd );
	}
	proc->pid = GFork( &proc->pipe, 0, cname, 0, 0 );
	if (proc->pid) {
		close( pip[1] );
		if (gp) {
			RegisterCloseOnFork( gp->rfd );
			RegisterCloseOnFork( gp->wfd );
		}
	}
	switch (proc->pid) {
	case -1:
	  fail1:
		close( pip[0] );
	  fail:
		free( margv[0] );
		free( margv );
		return -1;
	case 0:
		(void)Signal( SIGPIPE, SIG_IGN );
		close( pip[0] );
		fcntl( pip[1], F_SETFD, FD_CLOEXEC );
		if (gp)
			sprintf( coninfo, "CONINFO=%d %d %d %d",
			         proc->pipe.rfd, proc->pipe.wfd, gp->rfd, gp->wfd );
		else
			sprintf( coninfo, "CONINFO=%d %d",
			         proc->pipe.rfd, proc->pipe.wfd );
		env = putEnv( coninfo, env );
		if (debugLevel & DEBUG_VALGRIND) {
			char **nmargv = xCopyStrArr( 1, margv );
			nmargv[0] = locate( "valgrind" );
			execute( nmargv, env );
		} else if (debugLevel & DEBUG_STRACE) {
			char **nmargv = xCopyStrArr( 1, margv );
			nmargv[0] = locate( "strace" );
			execute( nmargv, env );
		} else
			execute( margv, env );
		write( pip[1], "", 1 );
		exit( 1 );
	default:
		(void)Signal( SIGPIPE, SIG_IGN );
		if (Reader( pip[0], coninfo, 1 )) {
			Wait4( proc->pid );
			LogError( "Cannot execute %\"s (%s)\n", margv[0], cname );
			GClosen (&proc->pipe);
			goto fail1;
		}
		close( pip[0] );
		Debug( "started %s (%\"s), pid %d\n", cname, margv[0], proc->pid );
		free( margv[0] );
		free( margv );
		GSendInt( debugLevel );
		return 0;
	}
}

static void
iGClosen( GPipe *pajp )
{
	CloseNClearCloseOnFork( pajp->rfd );
	CloseNClearCloseOnFork( pajp->wfd );
	pajp->rfd = pajp->wfd = -1;
}

void
GClosen (GPipe *pajp)
{
	iGClosen( pajp );
	if (pajp->who)
		free( pajp->who );
	pajp->who = 0;
}

int
GClose (GProc *proc, GPipe *gp, int force)
{
	int ret;

	if (!proc->pid) {
		Debug( "whoops, GClose while helper not running\n" );
		return 0;
	}
	iGClosen( &proc->pipe );
	if (gp)
		GClosen (gp);
	if (force)
		TerminateProcess( proc->pid, SIGTERM );
	ret = Wait4( proc->pid );
	proc->pid = 0;
	if (WaitSig( ret ) ? WaitSig( ret ) != SIGTERM :
	    (WaitCode( ret ) < EX_NORMAL || WaitCode( ret ) > EX_MAX))
		LogError( "Abnormal termination of %s, code %d, signal %d\n",
		          proc->pipe.who, WaitCode( ret ), WaitSig( ret ) );
	Debug( "closed %s\n", proc->pipe.who );
	if (proc->pipe.who)
		free( proc->pipe.who );
	proc->pipe.who = 0;
	return ret;
}

static void ATTR_NORETURN
GErr( void )
{
	Longjmp( curtalk->errjmp, 1 );
}

static void
GRead( void *buf, int len )
{
	if (Reader( curtalk->pipe->rfd, buf, len ) != len) {
		LogError( "Cannot read from %s\n", curtalk->pipe->who );
		GErr();
	}
}

static void
GWrite( const void *buf, int len )
{
	if (Writer( curtalk->pipe->wfd, buf, len ) != len) {
		LogError( "Cannot write to %s\n", curtalk->pipe->who );
		GErr();
	}
#ifdef _POSIX_PRIORITY_SCHEDULING
	if ((debugLevel & DEBUG_HLPCON))
		sched_yield();
#endif
}

void
GSendInt( int val )
{
	GDebug( "sending int %d (%#x) to %s\n", val, val, curtalk->pipe->who );
	GWrite( &val, sizeof(val) );
}

int
GRecvInt()
{
	int val;

	GDebug( "receiving int from %s ...\n", curtalk->pipe->who );
	GRead( &val, sizeof(val) );
	GDebug( " -> %d (%#x)\n", val, val );
	return val;
}

int
GRecvCmd( int *cmd )
{
	GDebug( "receiving command from %s ...\n", curtalk->pipe->who );
	if (Reader( curtalk->pipe->rfd, cmd, sizeof(*cmd) ) == sizeof(*cmd)) {
		GDebug( " -> %d\n", *cmd );
		return 1;
	}
	GDebug( " -> no data\n" );
	return 0;
}

void
GSendArr( int len, const char *data )
{
	GDebug( "sending array[%d] %02[*{hhx to %s\n",
	        len, len, data, curtalk->pipe->who );
	GWrite( &len, sizeof(len) );
	GWrite( data, len );
}

static char *
iGRecvArr( int *rlen )
{
	int len;
	char *buf;

	GRead( &len, sizeof(len) );
	*rlen = len;
	GDebug( " -> %d bytes\n", len );
	if (!len)
		return (char *)0;
	if (!(buf = Malloc( len )))
		GErr();
	GRead( buf, len );
	return buf;
}

char *
GRecvArr( int *rlen )
{
	char *buf;

	GDebug( "receiving array from %s ...\n", curtalk->pipe->who );
	buf = iGRecvArr( rlen );
	GDebug( " -> %02[*{hhx\n", *rlen, buf );
	return buf;
}

static int
iGRecvArrBuf( char *buf )
{
	int len;

	GRead( &len, sizeof(len) );
	GDebug( " -> %d bytes\n", len );
	if (len)
		GRead( buf, len );
	return len;
}

int
GRecvArrBuf( char *buf )
{
	int len;

	GDebug( "receiving already allocated array from %s ...\n",
	        curtalk->pipe->who );
	len = iGRecvArrBuf( buf );
	GDebug( " -> %02[*{hhx\n", len, buf );
	return len;
}

int
GRecvStrBuf( char *buf )
{
	int len;

	GDebug( "receiving already allocated string from %s ...\n",
	        curtalk->pipe->who );
	len = iGRecvArrBuf( buf );
	GDebug( " -> %\".*s\n", len, buf );
	return len;
}

void
GSendStr( const char *buf )
{
	int len;

	GDebug( "sending string %\"s to %s\n", buf, curtalk->pipe->who );
	if (buf) {
		len = strlen( buf ) + 1;
		GWrite( &len, sizeof(len) );
		GWrite( buf, len );
	} else
		GWrite( &buf, sizeof(int) );
}

void
GSendNStr( const char *buf, int len )
{
	int tlen = len + 1;
	GDebug( "sending string %\".*s to %s\n", len, buf, curtalk->pipe->who );
	GWrite( &tlen, sizeof(tlen) );
	GWrite( buf, len );
	GWrite( "", 1 );
}

void
GSendStrN( const char *buf, int len )
{
	if (buf)
		GSendNStr( buf, StrNLen( buf, len ) );
	else
		GSendStr( buf );
}

char *
GRecvStr()
{
	int len;
	char *buf;

	GDebug( "receiving string from %s ...\n", curtalk->pipe->who );
	buf = iGRecvArr( &len );
	GDebug( " -> %\".*s\n", len, buf );
	return buf;
}

static void
iGSendStrArr( int num, char **data )
{
	char **cdata;

	GWrite( &num, sizeof(num) );
	for (cdata = data; --num >= 0; cdata++)
		GSendStr( *cdata );
}

/*
void
GSendStrArr (int num, char **data)
{
	GDebug( "sending string array[%d] to %s\n", num, curtalk->pipe->who );
	iGSendStrArr( num, data );
}
*/

char **
GRecvStrArr( int *rnum )
{
	int num;
	char **argv, **cargv;

	GDebug( "receiving string array from %s ...\n", curtalk->pipe->who );
	GRead( &num, sizeof(num) );
	GDebug( " -> %d strings\n", num );
	*rnum = num;
	if (!num)
		return (char **)0;
	if (!(argv = Malloc( num * sizeof(char *) )))
		GErr();
	for (cargv = argv; --num >= 0; cargv++)
		*cargv = GRecvStr();
	return argv;
}

void
GSendArgv( char **argv )
{
	int num;

	if (argv) {
		for (num = 0; argv[num]; num++);
		GDebug( "sending argv[%d] to %s ...\n", num, curtalk->pipe->who );
		iGSendStrArr( num + 1, argv );
	} else {
		GDebug( "sending NULL argv to %s\n", curtalk->pipe->who );
		GWrite( &argv, sizeof(int) );
	}
}

char **
GRecvArgv()
{
	int num;

	return GRecvStrArr( &num );
}