1/*
2 * This file is part of the KDE libraries
3 * Copyright (c) 2007 Lubos Lunak <l.lunak@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License version 2 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include <config.h>
21#include <config-prefix.h>
22#include <config-kdeinit.h>
23
24#include <stdio.h>
25#include <string.h>
26#include <unistd.h>
27
28#define EXECUTE LIBEXEC_INSTALL_DIR "/start_kdeinit"
29
30#ifdef KDEINIT_OOM_PROTECT
31
32/*
33 The start_kdeinit wrapper is setuid, which means some shell variables like LD_LIBRARY_PATH
34 get unset before it's launched. However kdeinit is used to launch most of KDE, so such variables
35 should not be dropped. Therefore this wrapper for the setuid wrapper read the environment
36 and writes it to start_kdeinit's stdin, which after dropping privileges reads it and uses it
37 for launching the real kdeinit.
38*/
39int main(int argc, char **argv)
40{
41 int pipes[ 2 ];
42 if(argc == 0)
43 return 1;
44 if( pipe( pipes ) < 0 ) {
45 perror( "pipe()" );
46 return 1;
47 }
48 switch( fork()) {
49 case -1:
50 perror( "fork()" );
51 return 1;
52 default: /* parent, exec */
53 close( pipes[ 1 ] );
54 close( 0 ); /* stdin */
55 dup2( pipes[ 0 ], 0 );
56 close( pipes[ 0 ] );
57 argv[ 0 ] = (char*)EXECUTE;
58 execvp(EXECUTE, argv);
59 perror("start_kdeinit");
60 return 1;
61 case 0: { /* child, pass env and exit */
62 extern char** environ;
63 int i;
64 close( pipes[ 0 ] );
65 write( pipes[ 1 ], "environ", 7 ); /* header, just in case */
66 for( i = 0;
67 environ[ i ] != NULL;
68 ++i )
69 {}
70 write( pipes[ 1 ], &i, sizeof( int )); /* write count */
71 for( i = 0;
72 environ[ i ] != NULL;
73 ++i )
74 {
75 int len = strlen( environ[ i ] );
76 write( pipes[ 1 ], &len, sizeof( int )); /* write length */
77 write( pipes[ 1 ], environ[ i ], strlen( environ[ i ] ));
78 }
79 close( pipes[ 1 ] );
80 }
81 }
82 return 0;
83}
84
85#else /* not Linux, the simple non-setuid case */
86
87int main(int argc, char **argv)
88{
89 if(argc == 0)
90 return 1;
91 argv[0] = (char*)EXECUTE;
92 execvp(EXECUTE,argv);
93 perror("start_kdeinit");
94 return 1;
95}
96#endif
97