aboutsummaryrefslogtreecommitdiff
path: root/sysvinit.c
blob: b9ee198c55717cf4623800c9fe2d67e60b41f1a2 (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
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <alloca.h>
#include "ninitfeatures.h"
#include "initreq.h"
#define IREQ sizeof(struct init_request)

static char read_buf[IREQ + 4];
struct init_request *req = (void *)read_buf;
static int infd,outfd,flag_fork;

#include "open_inout.h"
#include "uid.h"

void sighandler(int sig) { (void)sig; while (waitpid(-1,0,WNOHANG) > 0); }

static void do_it(char *A) {
  int r=0;
  if (flag_fork) { while ((r=fork()) == -1) nano_sleep(1,0); }
  if (r==0) {
    open_inout(INITROOT);
    msg("Starting service: ",A+1);
    write(infd, A, str_len(A));
    read(outfd, req, IREQ);
    
    close(infd); close(outfd);
    if (flag_fork) _exit(0);
  }
}

void print_env() {
  char *x = req->i.data;
  msg("request"," for INIT_CMD_SETENV");
  while (*x) {
    msg(x);
    x += str_len(x);
    x++;
  }
}

int main(int argc, char **argv) {
  struct pollfd pfd;
  unsigned long ul[2] = {0,0};
  int fd, timeout = -1;
  char power[] = "spowerS";

  errmsg_iam("ninit-sysvinit");
  
  if (argc>1 && !str_diff(argv[1], "powerS")) {
    char ch;
    msg("trying to read and remove: ", INIT_POWERSTATUS);

    fd=open(INIT_POWERSTATUS, O_RDONLY);
    if (fd>=0) { read(fd,&ch,1); close(fd); }
    unlink(INIT_POWERSTATUS);
    
    if ('a' <= ch && ch <= 'z') ch -= 32;
    switch (ch) {
      case 'L': case 'O': break;
      default: ch = 'F';
    }
    
    power[6] = ch;
    do_it(power);
    return 0;
  }

  fd = open(INIT_FIFO, O_RDWR | O_NONBLOCK);
  if (fd < 0) {
    SYS_mknod(INIT_FIFO,S_IFIFO|0600,0);
    return -1;
  }
  fcntl(fd,F_SETFD,FD_CLOEXEC);

  pfd.fd = fd;
  pfd.events = POLLIN;
  read_ulongs("sysvinit-timeout", ul, 2);

  if (ul[0] > 0)timeout = 1000 * ul[0];
  flag_fork = (ul[1] != 0);
  // msg(" timeout: ",fu(ul[0]), ",fork-mode: ",flag_fork ? "YES" : "NO");

  if (flag_fork) set_sa(SIGCHLD);

  while (1) {
    int flag_known = 0;
    switch (poll(&pfd,1,timeout)) {
    case -1:
      if (errno == EINTR) sighandler(SIGCHLD); break;
      return -1;	/* poll failed */
    case 1: {
      int r = read(fd, req, IREQ);
      if (r == (int)IREQ) {

#ifdef SYSVINIT_DUMP_INITREQ
	if ((r=open("/tmp/__##initreq", O_WRONLY | O_APPEND)) >= 0) 
	  {  write(r,req,IREQ);  close(r); }
#endif

	if (req->magic == INIT_MAGIC) {
	  char level[] = "slevelS";
	  char *A="";

	  switch (req->cmd) {
	  case INIT_CMD_SETENV:
	    print_env();
	    flag_known=1;
	    break;
	  case INIT_CMD_RUNLVL:
	    r = req->runlevel;
	    if ('a' <= r && r <= 'z') r -= 32;
	    level[6] = r;
	    A = level;
	    break;
	  case INIT_CMD_POWEROK:
	    A = power;
	    power[6] = 'O';
	    break;
	  case INIT_CMD_POWERFAIL:
	    A = power;
	    power[6] = 'F';
	    break;
	  case INIT_CMD_POWERFAILNOW:
	    A = power;
	    power[6] = 'L';
	  }

	  if (*A) { flag_known=1; do_it(A); }
	}	/* INIT_MAGIC	*/
      }		/* r == sizeof(...) */
      if (flag_known==0) carp("unknown", " request");
    }
    break;
    default:	/* timeout	*/
      return 0;
    }		/* poll		*/
  }
}