summaryrefslogtreecommitdiff
path: root/package/miniinit/src/miniinit.c
blob: 69ad095c67cf6f3c4390e90a43ed1d059dbd5cd3 (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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main() 
{
  pid_t pid;

  printf("Started OpenADK miniinit\n");
  putenv("TERM=vt102");

  // Redirect stdio to /dev/console
  close(0);
  close(1);
  close(2);
  open("/dev/console", O_RDWR); // stdin
  dup(0); // stdout
  dup(0); // stderr

  pid = vfork();
  if (pid == 0) {
    setsid(); //new session
    putenv("HOME=/");
    putenv("PATH=/sbin:/usr/sbin:/bin:/usr/bin");
    putenv("SHELL=/bin/sh");
    putenv("USER=root");
    // Child: start a shell (e.g., /bin/sh)
    execl("/bin/sh", "/bin/sh", "-l", NULL);
    perror("execl");
    exit(1);
  } else if (pid > 0) {
    // Parent: wait for child to exit
    int status;
    waitpid(pid, &status, 0);
    printf("Shell exited, shutting down\n");	  
  } else if (pid < 0) {
    printf("vfork failed");
    sleep(1);
    return 0;
  }
  _exit(0);
}