11#define _GNU_SOURCE
2+ #include <stdbool.h>
23#include <stdio.h>
34#include <stdlib.h>
45#include <string.h>
@@ -1341,10 +1342,9 @@ static int hyper_loop(void)
13411342 return 0 ;
13421343}
13431344
1344- int main ( int argc , char * argv [] )
1345+ static int hyper_setup_init_process ( void )
13451346{
1346- char * cmdline , * ctl_serial , * tty_serial ;
1347-
1347+ /* mount the base file systems */
13481348 if (mount ("proc" , "/proc" , "proc" , MS_NOSUID | MS_NODEV | MS_NOEXEC , NULL ) == -1 ) {
13491349 perror ("mount proc failed" );
13501350 return -1 ;
@@ -1381,12 +1381,31 @@ int main(int argc, char *argv[])
13811381 return -1 ;
13821382 }
13831383
1384- cmdline = read_cmdline ();
1385-
1384+ /* become the session leader */
13861385 setsid ();
13871386
1387+ /* set the controlling terminal */
13881388 ioctl (STDIN_FILENO , TIOCSCTTY , 1 );
13891389
1390+ setenv ("PATH" , "/bin:/sbin/:/usr/bin/:/usr/sbin/" , 1 );
1391+
1392+ return 0 ;
1393+ }
1394+
1395+ int main (int argc , char * argv [])
1396+ {
1397+ char * binary_name , * cmdline , * ctl_serial , * tty_serial ;
1398+ bool is_init ;
1399+
1400+ binary_name = basename (argv [0 ]);
1401+ is_init = strncmp (binary_name , "init" , 5 ) == 0 ;
1402+
1403+ if (is_init && hyper_setup_init_process () < 0 ) {
1404+ return -1 ;
1405+ }
1406+
1407+ cmdline = read_cmdline ();
1408+
13901409#ifdef WITH_VBOX
13911410 ctl_serial = "/dev/ttyS0" ;
13921411 tty_serial = "/dev/ttyS1" ;
@@ -1401,8 +1420,6 @@ int main(int argc, char *argv[])
14011420 tty_serial = "sh.hyper.channel.1" ;
14021421#endif
14031422
1404- setenv ("PATH" , "/bin:/sbin/:/usr/bin/:/usr/sbin/" , 1 );
1405-
14061423 hyper_epoll .ctl .fd = hyper_setup_ctl_channel (ctl_serial );
14071424 if (hyper_epoll .ctl .fd < 0 ) {
14081425 fprintf (stderr , "fail to setup hyper control serial port\n" );
0 commit comments