aboutsummaryrefslogtreecommitdiff
path: root/misc/tryprocess.c
blob: 821e6704a9465964be8c028c189e3e9766ec3c11 (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
#include "../ninitfeatures.h"
#include <unistd.h>
#include <signal.h>
#include "try_helper.h"

#define ioctl libc_ioctl
#include <termios.h>
#undef ioctl
#include <sys/ioctl.h>

#define w_sn_(a,b)   w(a); wn(b); w("\n")
#define w_sp_(a,b,c) w(a); wn((void *)(&b) - (void *)(&c)); w("\n")
#define w_sns(a,b,c) w(a); wn(b); w(c)
#define w_ss_(a,b)   w(a);  w(b); w("\n")
#define w_o(X)       w(fmt_o((unsigned char)X))
#define w_to(t,X)    w(t); w_o(X)

#include "../struct_root.h"
INIT_ROOT_DEFINE(ch, char*);
INIT_ROOT_DEFINE(ui, unsigned int);

#define UL sizeof(unsigned long)
#define MASK_LEN  (_NSIG/(8 * UL))
#define WORD(X)   ((X-1)/(8*UL))
#define MASK(X)   (((unsigned long)1) << (X-1) % (8*UL))

int main() {
  int i,k;
  struct sigaction sa;
  unsigned long uu[256];
  char *ss[4] = { "kbreq", "ctrlaltdel", "powerS", "levelU" };

  w("#define NINIT_SERVICES_CODED\t\"");
  for (k=4, i=0; i<4; i++) {
    w_o(k);
    k += 1 + s_len(ss[i]);
  }
  for (i=0; i<4; i++) {
    w(ss[i]);
    if (i<3) w("\\000");
  }
  w("\"\n");

  if (SIGWINCH < 256 && SIGINT < 256 && SIGPWR < 256 && SIGHUP < 256) {
    w("#define NINIT_SIGNAL_HANDLER_CODED\t\"");
    w_o(SIGWINCH); w_o(SIGINT); w_o(SIGPWR); w_o(SIGHUP);
    w("\"\n");
  }

  if (SIGTERM < 256 && SIGALRM < 256 && SIGSTOP < 256 &&
      SIGCONT < 256 && SIGHUP  < 256 && SIGINT  < 256 && SIGKILL < 256) {
    w("#define NSVC_SIGNAL_CODED\t\"");
    w_to("t",SIGTERM); w_to("a",SIGALRM); w_to("p",SIGSTOP);
    w_to("c",SIGCONT); w_to("h",SIGHUP);  w_to("i",SIGINT);  
    w_to("k",SIGKILL);
    w("\"\n");
  }

  w_sn_("#if 0\n\tdev_t\t", sizeof(dev_t));
  w_sn_("\tgid_t\t", sizeof(gid_t));
  w_sn_("\tuid_t\t", sizeof(uid_t));
  w_sn_("\tmode_t\t", sizeof(mode_t));

  w_sn_("#endif\n#define X_TIOCGPGRP\t", TIOCGPGRP);
  w("\n#ifdef SIGACTION_FUNCTIONS\n\n");

#ifndef SA_RESTORER
#define SA_RESTORER   0x04000000
#endif

  w_sn_("#define SA_RESTART\t", SA_RESTART);
  w_sn_("#define SA_NOCLDSTOP\t", SA_NOCLDSTOP);

  uu[0] = SA_RESTART | SA_NOCLDSTOP | SA_RESTORER;
#ifdef INIT_SKIP_SIGRETURN
  w("#define INIT_SKIP_SIGRETURN\n");
  uu[0] = SA_RESTART | SA_NOCLDSTOP;
#endif
  w_sn_("#define SA_FLAGS_number\t", uu[0]);

#if defined(__i386__)  
  w("#define SA_RESTORER_function\t__restore");
  if (uu[0] & SA_SIGINFO) w("_rt");
#endif

#if defined(__x86_64__)
  w("#define SA_RESTORER_function\t__restore_rt");
#endif
  w("\n");

  w("\n#if 0\n");
  /*  w_sn_("#define STAT_SIZE\t", sizeof(struct stat)); */
  w_sn_("#define SIGATION_SIZE\t", sizeof(sa));
  w_sn_("#define SIGSET_T_SIZE\t", sizeof(sa.sa_mask));
  w_sn_("#define SIGSET_WORD_N\t", WORD(SIGCHLD));
  w_sn_("#define SIGSET_MASK_N\t", MASK(SIGCHLD));

  w_sp_("\tsa_handler offset\t", sa.sa_handler, sa);
  w_sp_("\tsa_flags offset\t",	 sa.sa_flags, sa);
  w_sp_("\tsa_restorer offest\t",sa.sa_restorer, sa);
  w_sp_("\tsa_mask offset\t",    sa.sa_mask, sa);
  w("#endif\n\n");

  for (i=0; i<256; i++) uu[i] = 0;
  uu[WORD(SIGCHLD)] = MASK(SIGCHLD);
  w_sn_("#define SIGSET_MASK_LEN_N\t", MASK_LEN);
  w_sns("#define STATIC_SIGCHLD_MASK unsigned long sigchld_mask[",
	MASK_LEN, "] = { ");

  for (i=0;; ) {
    wn(uu[i]); 
    if (++i ==  MASK_LEN) break;
    w(", ");
  }

  w(" }\n"
    "\n#else\n"
    "#include \"struct_root.h\"\n");

  if (sizeof(struct ch) > sizeof(struct ui)) {
    w("#define INIT_NAME_IS_UINT\n");
    w_sn_("#define PROCESS_SIZE ", sizeof(struct ui));
    w("INIT_ROOT_DEFINE(process, uint32t) *root;\n");
  } else {
    w("#undef INIT_NAME_IS_UINT\n");
    w_sn_("#define PROCESS_SIZE ", sizeof(struct ch));
    w("INIT_ROOT_DEFINE(process, char*) *root;\n");
  }

  w("#endif\n");
  return 0;
}