tmain.c - plan9port - [fork] Plan 9 from user space
git clone git://src.adamsgaard.dk/plan9port
Log
Files
Refs
README
LICENSE
---
tmain.c (1692B)
---
     1 #include 
     2 #include 
     3 #include 
     4 #include "dat.h"
     5 
     6 Biobuf bout;
     7 
     8 void
     9 usage(void)
    10 {
    11         fprint(2, "usage: auxstats [system [executable]]\n");
    12         exits("usage");
    13 }
    14 
    15 int pid;
    16 
    17 void
    18 notifyf(void *v, char *msg)
    19 {
    20         USED(v);
    21 
    22         if(strstr(msg, "child"))
    23                 noted(NCONT);
    24         if(pid)
    25                 postnote(PNPROC, pid, msg);
    26         exits(nil);
    27 }
    28 
    29 void
    30 main(int argc, char **argv)
    31 {
    32         char *sys, *exe;
    33         int i;
    34         Waitmsg *w;
    35 
    36         ARGBEGIN{
    37         default:
    38                 usage();
    39         }ARGEND
    40 
    41         notify(notifyf);
    42 
    43         sys = nil;
    44         exe = nil;
    45         if(argc > 0)
    46                 sys = argv[0];
    47         if(argc > 1)
    48                 exe = argv[1];
    49         if(argc > 2)
    50                 usage();
    51 
    52         if(sys){
    53                 if(exe == nil)
    54                         exe = "auxstats";
    55                 for(;;){
    56                         switch(pid = fork()){
    57                         case -1:
    58                                 sysfatal("fork: %r");
    59                         case 0:
    60                                 rfork(RFNOTEG);
    61                                 execlp("ssh", "ssh", "-nTC", sys, exe, nil);
    62                                 _exit(97);
    63                         default:
    64                                 if((w = wait()) == nil)
    65                                         sysfatal("wait: %r");
    66                                 if(w->pid != pid)
    67                                         sysfatal("wait got wrong pid");
    68                                 if(atoi(w->msg) == 97)
    69                                         sysfatal("exec ssh failed");
    70                                 free(w);
    71                                 fprint(2, "stats: %s hung up; sleeping 60\n", sys);
    72                                 sleep(60*1000);
    73                                 fprint(2, "stats: redialing %s\n", sys);
    74                                 break;
    75                         }
    76                 }
    77         }
    78 
    79         for(i=0; statfn[i]; i++)
    80                 statfn[i](1);
    81 
    82         Binit(&bout, 1, OWRITE);
    83         for(;;){
    84                 Bflush(&bout);
    85                 sleep(900);
    86                 for(i=0; statfn[i]; i++)
    87                         statfn[i](0);
    88         }
    89 }
    90 
    91 char buf[16384];
    92 char *line[100];
    93 char *tok[100];
    94 int nline, ntok;
    95 
    96 void
    97 readfile(int fd)
    98 {
    99         int n;
   100 
   101         if(fd == -1){
   102                 nline = 0;
   103                 return;
   104         }
   105 
   106         seek(fd, 0, 0);
   107         n = read(fd, buf, sizeof buf-1);
   108         if(n < 0)
   109                 n = 0;
   110         buf[n] = 0;
   111 
   112         nline = getfields(buf, line, nelem(line), 0, "\n");
   113 }
   114 
   115 void
   116 tokens(int i)
   117 {
   118         if(i >= nline){
   119                 ntok = 0;
   120                 return;
   121         }
   122         ntok = tokenize(line[i], tok, nelem(tok));
   123 }