4 * (c) 2010 Martin Mares <mj@ucw.cz>
8 * Things that are not implemented:
11 * - exact behavior of accumulator/R1/R2 (the manual lacks details)
12 * - exact behavior of negative zero
13 * - I/O instructions for devices that are not emulated (paper tape
14 * reader and puncher, card reader and puncher, magnetic tape unit)
18 #define UNUSED __attribute__((unused))
19 #define NORETURN __attribute__((noreturn))
21 #undef ENABLE_DAEMON_MODE
33 static int cpu_quota = -1;
34 static int print_quota = -1;
36 static void (*error_hook)(char *msg);
38 // Minsk-2 has 37-bit words in sign-magnitude representation (bit 36 = sign)
39 typedef unsigned long long int word;
42 #define WORD_MASK 01777777777777ULL
43 #define SIGN_MASK 01000000000000ULL
44 #define VAL_MASK 00777777777777ULL
46 static int wsign(word w)
48 return (w & SIGN_MASK) ? -1 : 1;
51 static word wabs(word w)
56 #define WF(w) (wsign(w) < 0 ? '-' : '+'), wabs(w)
58 static long long wtoll(word w)
66 static word wfromll(long long x)
68 word w = ((x < 0) ? -x : x) & VAL_MASK;
74 static double wtofrac(word w)
76 return (double)wtoll(w) / (double)(1ULL << 36);
79 static word wfromfrac(double d)
81 return wfromll((long long)(d * (double)(1ULL << 36)));
84 static int int_in_range(long long x)
86 return (x >= -(long long)VAL_MASK && x <= (long long)VAL_MASK);
89 static int frac_in_range(double d)
91 return (d > -1. && d < 1.);
94 static int wexp(word w)
97 return (w & 0100 ? -exp : exp);
100 static word wputexp(word w, int exp)
102 return ((w & ~(word)0177) | ((exp < 0) ? 0100 | (-exp) : exp));
105 static int wmanti(word w)
107 return ((w >> 8) & ((1 << 28) - 1));
110 static double wtofloat(word w)
112 double x = wmanti(w);
113 return ldexp(x, wexp(w) - 28);
116 static int float_in_range(double x)
119 return (x <= ldexp((1 << 28) - 1, 63 - 28));
122 static word wfromfloat(double x, int normalized)
131 double m = frexp(x, &exp);
132 word mm = (word) ldexp(m, 28);
137 if (normalized || exp < -91)
155 static word mem[4096];
157 static word rd(int addr)
159 word val = addr ? mem[addr] : 0;
161 printf("\tRD %04o = %c%012llo\n", addr, WF(val));
165 static void wr(int addr, word val)
167 assert(!(val & ~(WORD_MASK)));
169 printf("\tWR %04o = %c%012llo\n", addr, WF(val));
175 NORETURN static void parse_error(char *russian_msg, char *english_msg)
178 error_hook("Parse error");
181 printf("Parse error (line %d): %s\n", lino, english_msg);
183 printf("Ошибка входа (стр. %d): %s\n", lino, russian_msg);
187 static void parse_in(void)
192 while (fgets(line, sizeof(line), stdin))
195 char *eol = strchr(line, '\n');
197 parse_error("Строка слишком долгая", "Line too long");
199 if (eol > line && eol[-1] == '\r')
203 if (!c[0] || c[0] == ';')
213 for (int i=0; i<4; i++)
217 if (*c >= '0' && *c <= '7')
218 addr = 8*addr + *c++ - '0';
220 parse_error("Плохая цифра", "Invalid number");
225 parse_error("Адрес слишком долгий", "Address too long");
233 parse_error("Плохой знак", "Invalid sign");
235 for (int i=0; i<12; i++)
239 if (*c >= '0' && *c <= '7')
240 w = 8*w + *c++ - '0';
242 parse_error("Плохая цифра", "Invalid number");
247 parse_error("Номер слишком долгий", "Number too long");
254 static word r1, r2, current_ins;
255 static int ip = 00050; // Standard program start location
258 NORETURN static void stop(char *russian_reason, char *english_reason)
261 error_hook(english_reason);
265 printf("System stopped -- %s\n", english_reason);
266 printf("IP:%04o ACC:%c%012llo R1:%c%012llo R2:%c%012llo\n", prev_ip, WF(acc), WF(r1), WF(r2));
270 printf("Машина остановлена -- %s\n", russian_reason);
271 printf("СчАК:%04o См:%c%012llo Р1:%c%012llo Р2:%c%012llo\n", prev_ip, WF(acc), WF(r1), WF(r2));
276 NORETURN static void over(void)
278 stop("Аварийный останов", "Overflow");
281 NORETURN static void notimp(void)
284 stop("Устройство разбитое", "Not implemented");
287 NORETURN static void noins(void)
290 stop("Эту команду не знаю", "Illegal instruction");
293 static uint16_t linebuf[128];
295 static uint16_t russian_chars[64] = {
296 '0', '1', '2', '3', '4', '5', '6', '7', // 0x
297 '8', '9', '+', '-', '/', ',', '.', ' ', // 1x
298 0x2169, '^', '(', ')', 0x00d7, '=', ';', '[', // 2x
299 ']', '*', '`', '\'', 0x2260, '<', '>', ':', // 3x
300 0x410, 0x411, 0x412, 0x413, 0x414, 0x415, 0x416, 0x417, // 4x
301 0x418, 0x419, 0x41a, 0x41b, 0x41c, 0x41d, 0x41e, 0x41f, // 5x
302 0x420, 0x421, 0x422, 0x423, 0x424, 0x425, 0x426, 0x427, // 6x
303 0x428, 0x429, 0x42b, 0x42c, 0x42d, 0x42e, 0x42f, 0x2013 // 7x
306 static uint16_t latin_chars[64] = {
307 '0', '1', '2', '3', '4', '5', '6', '7', // 0x
308 '8', '9', '+', '-', '/', ',', '.', ' ', // 1x
309 0x2169, '^', '(', ')', 0x00d7, '=', ';', '[', // 2x
310 ']', '*', '`', '\'', 0x2260, '<', '>', ':', // 3x
311 'A', 'B', 'W', 'G', 'D', 'E', 'V', 'Z', // 4x
312 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 5x
313 'R', 'S', 'T', 'U', 'F', 'H', 'C', ' ', // 6x
314 ' ', ' ', 'Y', 'X', ' ', ' ', 'Q', 0x2013 // 7x
317 static void print_line(int r)
320 * Meaning of bits of r:
321 * 0 = perform line feed
327 if (print_quota > 0 && !--print_quota)
328 stop("Бумага дошла - нужно ехать в Сибирь про новую", "Out of paper");
329 for (int i=0; i<128; i++)
338 putchar(0xc0 | (ch >> 6));
339 putchar(0x80 | (ch & 0x3f));
343 putchar(0xe0 | (ch >> 12));
344 putchar(0x80 | ((ch >> 6) & 0x3f));
345 putchar(0x80 | (ch & 0x3f));
350 memset(linebuf, 0, sizeof(linebuf));
358 static void print_ins(int x, int y)
362 int r = (x >> 9) & 7;
375 case 0: // Decimal float
376 fmt = "+dddddddx+xbd";
378 case 1: // Octal number
379 fmt = "+oooooooooooo";
381 case 2: // Decimal fixed
384 case 3: // Decimal unsigned
388 case 4: // One Russian symbol
391 case 5: // Russian text
394 case 6: // One Latin symbol
397 default: // Latin text
414 ch = (yy & (1ULL << bit)) ? '-' : '+';
418 ch = '0' + ((yy >> bit) & 1);
422 ch = '0' + ((yy >> bit) & 7);
426 ch = '0' + ((yy >> bit) & 15);
432 ch = russian_chars[(yy >> bit) & 077];
436 ch = latin_chars[(yy >> bit) & 077];
444 if (ch == '0' || ch == ' ')
450 pos = (pos+1) & 0177;
455 static void run(void)
464 int op = (w >> 30) & 0177; // Operation code
465 int ax = (w >> 28) & 3; // Address extensions not supported
466 int ix = (w >> 24) & 15; // Indexing
467 int x = (w >> 12) & 07777; // Operands (original form)
469 int xi=x, yi=y; // (indexed form)
471 printf("@%04o %c%02o %02o %04o %04o\n",
473 (w & SIGN_MASK) ? '-' : '+',
474 (int)((w >> 30) & 077),
475 (int)((w >> 24) & 077),
483 xi = (xi + (int)((i >> 12) & 07777)) & 07777;
484 yi = (yi + (int)(i & 07777)) & 07777;
486 printf("\tIndexing -> %04o %04o\n", xi, yi);
491 if (cpu_quota > 0 && !--cpu_quota)
492 stop("Тайм-аут", "CPU quota exceeded");
494 /* Arithmetic operations */
497 long long aa, bb, cc;
501 auto void afetch(void);
511 auto void astore(word result);
512 void astore(word result)
519 auto void astore_int(long long x);
520 void astore_int(long long x)
522 if (!int_in_range(x))
527 auto void astore_frac(double f);
528 void astore_frac(double f)
530 if (!frac_in_range(f))
532 astore(wfromfrac(f));
535 auto void astore_float(double f);
536 void astore_float(double f)
538 if (!float_in_range(f))
540 astore(wfromfloat(f, 0));
549 case 004 ... 007: // XOR
553 case 010 ... 013: // FIX addition
555 astore_int(wtoll(a) + wtoll(b));
557 case 014 ... 017: // FP addition
559 astore_float(wtofloat(a) + wtofloat(b));
561 case 020 ... 023: // FIX subtraction
563 astore_int(wtoll(a) - wtoll(b));
565 case 024 ... 027: // FP subtraction
567 astore_float(wtofloat(a) - wtofloat(b));
569 case 030 ... 033: // FIX multiplication
571 astore_frac(wtofrac(a) * wtofrac(b));
573 case 034 ... 037: // FP multiplication
575 astore_float(wtofloat(a) * wtofloat(b));
577 case 040 ... 043: // FIX division
583 astore_frac(ad / bd);
585 case 044 ... 047: // FP division
589 if (!bd || wexp(b) < -63)
591 astore_float(ad / bd);
593 case 050 ... 053: // FIX subtraction of abs values
595 astore_int(wabs(a) - wabs(b));
597 case 054 ... 057: // FP subtraction of abs values
599 astore_float(fabs(wtofloat(a)) - fabs(wtofloat(b)));
601 case 060 ... 063: // Shift logical
604 if (i <= -37 || i >= 37)
607 astore((a << i) & WORD_MASK);
611 case 064 ... 067: // Shift arithmetical
615 if (i <= -36 || i >= 36)
618 cc = (aa << i) & VAL_MASK;
621 astore((a & SIGN_MASK) | wfromll(cc));
623 case 070 ... 073: // And
627 case 074 ... 077: // Or
635 stop("Останов машины", "Halted");
636 case 0103: // I/O magtape
638 case 0104: // Disable rounding
640 case 0105: // Enable rounding
642 case 0106: // Interrupt control
644 case 0107: // Reverse tape
647 wr(yi, r1 = acc = rd(xi));
649 case 0111: // Move negative
650 wr(yi, acc = (r1 = rd(xi)) ^ SIGN_MASK);
652 case 0112: // Move absolute value
653 wr(yi, acc = (r1 = rd(xi)) & VAL_MASK);
655 case 0113: // Read from keyboard
657 case 0114: // Copy sign
658 wr(yi, acc = rd(yi) ^ ((r1 = rd(xi)) & SIGN_MASK));
660 case 0115: // Read code from R1 (obscure)
662 case 0116: // Copy exponent
663 wr(yi, acc = wputexp(rd(yi), wexp(r1 = rd(xi))));
665 case 0117: // I/O teletype
671 aa = (a >> 24) & 017777;
674 b = rd(y); // (a mountain range near Prague)
675 acc = ((aa-1) << 24) |
676 (((((a >> 12) & 07777) + (b >> 12) & 07777) & 07777) << 12) |
677 (((a & 07777) + (b & 07777)) & 07777);
685 case 0131: // Jump to subroutine
686 wr(y, acc = ((030ULL << 30) | ((ip & 07777ULL) << 12)));
689 case 0132: // Jump if positive
695 case 0133: // Jump if overflow
696 // Since we always trap on overflow, this instruction always jumps to the 1st address
699 case 0134: // Jump if zero
705 case 0135: // Jump if key pressed
706 // No keys are ever pressed, so always jump to 2nd
709 case 0136: // Interrupt masking
711 case 0137: // Used only when reading from tape
713 case 0140 ... 0147: // I/O
715 case 0150 ... 0154: // I/O
717 case 0160 ... 0161: // I/O
719 case 0162: // Printing
724 case 0170: // FIX multiplication, bottom part
726 if (wtofrac(a) * wtofrac(b) >= .1/(1ULL << 32))
728 acc = wfromll(((unsigned long long)wabs(a) * (unsigned long long)wabs(b)) & VAL_MASK);
729 // XXX: What should be the sign? The book does not define that.
742 case 0172: // Add exponents
745 i = wexp(a) + wexp(b);
746 if (i < -63 || i > 63)
751 case 0173: // Sub exponents
754 i = wexp(b) - wexp(a);
755 if (i < -63 || i > 63)
760 case 0174: // Addition in one's complement
767 // XXX: The effect on the accumulator is undocumented, but likely to be as follows:
770 case 0175: // Normalization
775 wr((yi+1) & 07777, 0);
783 while (!(a & (SIGN_MASK >> 1)))
790 wr((yi+1) & 07777, i);
793 case 0176: // Population count
796 for (int i=0; i<36; i++)
799 // XXX: Guessing that acc gets a copy of the result
808 printf("\tACC:%c%012llo R1:%c%012llo R2:%c%012llo\n", WF(acc), WF(r1), WF(r2));
812 NORETURN static void die(char *msg)
814 fprintf(stderr, "minsk: %s\n", msg);
818 /*** Daemon interface ***/
820 #ifdef ENABLE_DAEMON_MODE
823 * The daemon mode was a quick hack for the Po drate contest.
824 * Most parameters are hard-wired.
831 #include <sys/signal.h>
832 #include <sys/wait.h>
833 #include <sys/poll.h>
834 #include <sys/socket.h>
835 #include <netinet/in.h>
836 #include <arpa/inet.h>
839 #define DTRACE(msg, args...) fprintf(stderr, msg "\n", ##args)
840 #define DLOG(msg, args...) fprintf(stderr, msg "\n", ##args)
842 #define DTRACE(msg, args...) do { } while(0)
843 #define DLOG(msg, args...) syslog(LOG_INFO, msg, ##args)
846 #define MAX_CONNECTIONS 50 // Per daemon
847 #define MAX_CONNS_PER_IP 1 // Per IP
848 #define MAX_TRACKERS 200 // IP address trackers
849 #define TBF_MAX 5 // Max number of tokens in the bucket
850 #define TBF_REFILL_PER_SEC 0.2 // Bucket refill rate (buckets/sec)
852 #define PID_FILE "/var/run/pd-minsk.pid"
856 static char **spt_argv;
857 static char *spt_start, *spt_end;
859 static void setproctitle_init(int argc, char **argv)
862 char **env, **oldenv, *t;
866 /* Create a backup copy of environment */
869 for (i=0; oldenv[i]; i++)
870 len += strlen(oldenv[i]) + 1;
871 __environ = env = malloc(sizeof(char *)*(i+1));
873 if (!__environ || !t)
874 die("malloc failed");
875 for (i=0; oldenv[i]; i++)
878 len = strlen(oldenv[i]) + 1;
879 memcpy(t, oldenv[i], len);
884 /* Scan for consecutive free space */
885 spt_start = spt_end = argv[0];
886 for (i=0; i<argc; i++)
887 if (!i || spt_end+1 == argv[i])
888 spt_end = argv[i] + strlen(argv[i]);
889 for (i=0; oldenv[i]; i++)
890 if (spt_end+1 == oldenv[i])
891 spt_end = oldenv[i] + strlen(oldenv[i]);
895 setproctitle(const char *msg, ...)
902 if (spt_end > spt_start)
904 n = vsnprintf(buf, sizeof(buf), msg, args);
905 if (n >= (int) sizeof(buf) || n < 0)
906 sprintf(buf, "<too-long>");
907 n = spt_end - spt_start;
908 strncpy(spt_start, buf, n);
910 spt_argv[0] = spt_start;
916 static void sigchld_handler(int sig UNUSED)
920 static void sigalrm_handler(int sig UNUSED)
922 const char err[] = "--- Timed out. Time machine disconnected. ---\n";
923 write(1, err, sizeof(err));
924 DLOG("Connection timed out");
928 static void child_error_hook(char *err)
930 DLOG("Stopped: %s", err);
933 static void child(int sk2)
939 struct sigaction sact = {
940 .sa_handler = sigalrm_handler,
942 if (sigaction(SIGALRM, &sact, NULL) < 0)
943 die("sigaction: %m");
950 const char welcome[] = "+++ Welcome to our computer museum. +++\n+++ Our time machine will connect you to one of our exhibits. +++\n\n";
951 write(1, welcome, sizeof(welcome));
953 error_hook = child_error_hook;
963 struct tracker *tracker;
966 static struct conn connections[MAX_CONNECTIONS];
968 static struct conn *get_conn(struct in_addr *a)
970 for (int i=0; i<MAX_CONNECTIONS; i++)
972 struct conn *c = &connections[i];
975 memcpy(&c->addr, a, sizeof(struct in_addr));
982 static struct conn *pid_to_conn(pid_t pid)
984 for (int i=0; i<MAX_CONNECTIONS; i++)
986 struct conn *c = &connections[i];
993 static void put_conn(struct conn *c)
1000 struct in_addr addr;
1006 static struct tracker trackers[MAX_TRACKERS];
1008 static int get_tracker(struct conn *c)
1011 time_t now = time(NULL);
1014 for (i=0; i<MAX_TRACKERS; i++)
1017 if (!memcmp(&t->addr, &c->addr, sizeof(struct in_addr)))
1020 if (i < MAX_TRACKERS)
1022 if (now > t->last_access)
1024 t->tokens += (now - t->last_access) * (double) TBF_REFILL_PER_SEC;
1025 t->last_access = now;
1026 if (t->tokens > TBF_MAX)
1027 t->tokens = TBF_MAX;
1029 DTRACE("TBF: Using tracker %d (%.3f tokens)", i, t->tokens);
1034 for (int i=0; i<MAX_TRACKERS; i++)
1037 if (!t->active_conns && (min_i < 0 || t->last_access < trackers[min_i].last_access))
1042 DLOG("TBF: Out of trackers!");
1045 t = &trackers[min_i];
1047 DTRACE("TBF: Recycling tracker %d", min_i);
1049 DTRACE("TBF: Creating tracker %d", min_i);
1050 memset(t, 0, sizeof(*t));
1052 t->last_access = now;
1053 t->tokens = TBF_MAX;
1056 if (t->active_conns >= MAX_CONNS_PER_IP)
1058 DTRACE("TBF: Too many conns per IP");
1062 if (t->tokens >= 0.999)
1067 DTRACE("TBF: Passed (%d conns)", t->active_conns);
1072 DTRACE("TBF: Failed");
1078 static void put_tracker(struct conn *c)
1080 struct tracker *t = c->tracker;
1083 DLOG("put_tracker: no tracker?");
1087 if (t->active_conns <= 0)
1089 DLOG("put_tracker: no counter?");
1094 DTRACE("TBF: Put tracker (%d conns remain)", t->active_conns);
1097 static void run_as_daemon(int do_fork)
1099 int sk = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
1104 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
1105 die("setsockopt: %m");
1107 struct sockaddr_in sa = {
1108 .sin_family = AF_INET,
1109 .sin_port = ntohs(1969),
1110 .sin_addr.s_addr = INADDR_ANY,
1112 if (bind(sk, (struct sockaddr *) &sa, sizeof(sa)) < 0)
1114 if (listen(sk, 128) < 0)
1116 // if (fcntl(sk, F_SETFL, O_NONBLOCK) < 0)
1117 // die("fcntl: %m");
1126 FILE *f = fopen(PID_FILE, "w");
1129 fprintf(f, "%d\n", pid);
1136 setresgid(GID, GID, GID);
1137 setresuid(UID, UID, UID);
1141 struct sigaction sact = {
1142 .sa_handler = sigchld_handler,
1143 .sa_flags = SA_RESTART,
1145 if (sigaction(SIGCHLD, &sact, NULL) < 0)
1146 die("sigaction: %m");
1148 DLOG("Daemon ready");
1149 setproctitle("minsk: Listening");
1150 openlog("minsk", LOG_PID, LOG_LOCAL7);
1154 struct pollfd pfd[1] = {
1155 { .fd = sk, .events = POLLIN },
1158 int nfds = poll(pfd, 1, 60000);
1159 if (nfds < 0 && errno != EINTR)
1168 while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
1170 if (!WIFEXITED(status) || WEXITSTATUS(status))
1171 DLOG("Process %d exited with strange status %x", pid, status);
1173 struct conn *conn = pid_to_conn(pid);
1176 DTRACE("Connection with PID %d exited", pid);
1181 DTRACE("PID %d exited, matching no connection", pid);
1184 if (!(pfd[0].revents & POLLIN))
1187 socklen_t salen = sizeof(sa);
1188 int sk2 = accept(sk, (struct sockaddr *) &sa, &salen);
1198 DTRACE("Got connection: fd=%d", sk2);
1200 struct conn *conn = get_conn(&sa.sin_addr);
1201 const char *reason = NULL;
1204 if (!get_tracker(conn))
1206 DLOG("Connection from %s dropped: Throttling", inet_ntoa(sa.sin_addr));
1209 reason = "--- Sorry, but you are sending too many requests. Please slow down. ---\n";
1214 DLOG("Connection from %s dropped: Too many connections", inet_ntoa(sa.sin_addr));
1215 reason = "--- Sorry, maximum number of connections exceeded. Please come later. ---\n";
1221 DLOG("fork failed: %m");
1230 DLOG("Accepted connection from %s", inet_ntoa(sa.sin_addr));
1231 setproctitle("minsk: %s", inet_ntoa(sa.sin_addr));
1236 DLOG("Sending error message to %s", inet_ntoa(sa.sin_addr));
1237 setproctitle("minsk: %s ERR", inet_ntoa(sa.sin_addr));
1238 write(sk2, reason, strlen(reason));
1243 DTRACE("Created process %d", pid);
1252 static void run_as_daemon(int do_fork UNUSED)
1254 die("Daemon mode not supported in this version, need to recompile.");
1257 static void setproctitle_init(int argc UNUSED, char **argv UNUSED)
1263 static void init_memory(int set_password)
1267 // For the contest, we fill the whole memory with -00 00 0000 0000 (HALT),
1268 // not +00 00 0000 0000 (NOP). Otherwise, an empty program would reveal
1269 // the location of the password :)
1270 for (int i=0; i<MEM_SIZE; i++)
1271 mem[i] = 01000000000000ULL;
1273 // Store the password
1275 mem[pos++] = 0574060565373;
1276 mem[pos++] = 0371741405340;
1277 mem[pos++] = 0534051524017;
1280 for (int i=0; i<MEM_SIZE; i++)
1281 mem[i] = 00000000000000ULL;
1284 static const struct option longopts[] = {
1285 { "cpu-quota", required_argument, NULL, 'q' },
1286 { "daemon", no_argument, NULL, 'd' },
1287 { "nofork", no_argument, NULL, 'n' },
1288 { "english", no_argument, NULL, 'e' },
1289 { "set-password", no_argument, NULL, 's' },
1290 { "print-quota", required_argument, NULL, 'p' },
1291 { "trace", required_argument, NULL, 't' },
1292 { NULL, 0, NULL, 0 },
1295 static void usage(void)
1297 fprintf(stderr, "Options:\n\n");
1298 #ifdef ENABLE_DAEMON_MODE
1300 --daemon Run as daemon and listen for network connections\n\
1301 --nofork When run with --daemon, avoid forking\n\
1305 --english Print messages in English\n\
1306 --set-password Put hidden password in memory\n\
1307 --trace=<level> Enable tracing of program execution\n\
1308 --cpu-quota=<n> Set CPU quota to <n> instructions\n\
1309 --print-quota=<n> Set printer quota to <n> lines\n\
1314 int main(int argc, char **argv)
1317 int daemon_mode = 0;
1319 int set_password = 0;
1321 while ((opt = getopt_long(argc, argv, "", longopts, NULL)) >= 0)
1337 print_quota = atoi(optarg);
1340 cpu_quota = atoi(optarg);
1343 trace = atoi(optarg);
1351 setproctitle_init(argc, argv);
1352 init_memory(set_password);
1355 run_as_daemon(do_fork);