File: superd.c

package info (click to toggle)
superd 0.0.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 136 kB
  • ctags: 85
  • sloc: ansic: 721; makefile: 103; sh: 27
file content (265 lines) | stat: -rw-r--r-- 6,483 bytes parent folder | download | duplicates (3)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "wrappers.h"
#include "ux_client_socket.h"
#include "ux_server_socket.h"
#include "network_server_socket.h"
#include "sendfd.h"
#include "recvfd.h"
#include "daemonize.h"
#include "getportnum.h"

struct minion {
  pid_t pid;
  int uxcsd;
  short ready;
};



// we love globals
int MINIONS = 20;
char unix_socket_file[64];
struct minion *minions;
int uxlsd;
int nlsd;
struct sockaddr_un uxaddr;
int uxaddrlen = sizeof(uxaddr);
char *serverprocess[128]; // surely 128 command line args are enough
char DEBUGFILE[64] = "/dev/null"; // writes to /dev/null are fast
FILE *debugmsg = NULL;



// singal handlers
void sigchld(int signo) {
  pid_t pid;
  int stat;
  int counter;

  while ((pid = waitpid(-1, &stat, WNOHANG)) > 0) {
    fprintf(debugmsg, "minion %i terminated\n", (int)pid);
    // find the minion that died and mark it as such
    for(counter = 0; counter < MINIONS; counter++) {
      if(minions[counter].pid == pid) {
	minions[counter].pid = -1;
	close(minions[counter].uxcsd);
      }
    }
      
  }
  return;
}

void sigdie(int signo) {
  int i;

  for(i = 0; i < MINIONS; i++) {
    if(minions[i].pid > -1)
      kill(minions[i].pid, SIGTERM);
  }
  unlink((const char *)unix_socket_file);
  shutdown(nlsd, 2);
  exit(0);
}



void spawnaminion(int whichone){
  pid_t pid;
  int counter;
  int received_fd;
  int uxcsd;

  fprintf(debugmsg, "spawning a minion\n");
  pid = fork();

  if(pid == 0) {
    // we don't want to call the same signal handlers as our parent
    Signal(SIGTERM, SIG_DFL);
    Signal(SIGINT, SIG_DFL);
    // child
    close(uxlsd);
    // close out connections to other minions
    for(counter = 0; counter < MINIONS; counter++) 
      if(minions[counter].pid > -1)
	close(minions[counter].pid);
    uxcsd = ux_client_socket((const char *)unix_socket_file);
    // get the fd from the parent
    if((received_fd = recvfd(uxcsd)) < 0) 
      err_sys("recvfd failed");
    fprintf(debugmsg, "done blocking in child\n");
    dup2(received_fd, 0);
    dup2(received_fd, 1);
    execv(serverprocess[0], serverprocess);
    fprintf(debugmsg, "exec returned badly\n");
    exit(1);
  }

  minions[whichone].pid = pid;
  minions[whichone].ready = 1;

  // wait for the minion to connect. it shouldn't take too long.
  minions[whichone].uxcsd = accept(uxlsd, (struct sockaddr *)&uxaddr, &uxaddrlen);


}


void minioncount() {
  int counter;

  for (counter = 0; counter < MINIONS; counter++) {
    if(minions[counter].pid == -1)
      spawnaminion(counter);
  }
}
    
void printusage(char *argv) {
    fprintf(stderr, "usage: %s [-m numminions] [-l listenq] port absolutepathtobinary [args] ...\n", argv);
    exit(1);
}



int main(int argc, char *argv[]) {
  int counter;
  int pollreturn;
  struct pollfd fdset[1];
  struct sockaddr_in inaddr;
  int inaddrlen = sizeof(inaddr);
  int ncsd;
  int debugmsgfd;
  int i;
  short LISTENQ = 10;
  int listenport;

  if(argc < 3) 
    printusage(argv[0]);

  // daemonize
  daemonize();

  // dynamically set up the minions
  minions = (struct minion *)malloc(sizeof(struct minion) * MINIONS);

  // read in the command line arguements
  while((i = getopt(argc, argv, "+m:l:d:")) != EOF) {
    if(i == '?') 
      printusage(argv[0]);
    switch(i) {
    case 'd':
      strcpy(DEBUGFILE, optarg);
      break;
    case 'l':
      LISTENQ = atoi(optarg);
      break;
    case 'm':
      MINIONS = atoi(optarg);
      break;
    }
  }

  // open file for debugging messages
  debugmsgfd = open(DEBUGFILE, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if(debugmsgfd < 0) {
    perror("open");
    fprintf(stderr, "superd couldn't open debug file.  continuing...\n");
    debugmsgfd = open("/dev/null", O_WRONLY, 0644);
  }

  // attach debug file to a stream and remove buffering
  debugmsg = fdopen(debugmsgfd, "a");
  if(debugmsg == NULL)
    err_quit("couldn't attach debug file to a stream");
  setbuf(debugmsg, NULL);
  fprintf(debugmsg, "start of superd debug\n");

  // check the path to the binary for absoluteness
  if(argv[optind + 1][0] != '/') {
    fprintf(stderr, "the specified binary must have an absolute path\n");
    printusage(argv[0]);
  }

  // set the name of the bin we want to run
  for(counter = 0; argv[counter + optind + 1] != NULL; counter++) 
    serverprocess[counter] = argv[counter + optind + 1];
  serverprocess[counter + optind + 1] = (char *)'\0';
  fprintf(debugmsg, "binary to run: %s\n", serverprocess[0]);

  // name our unix socket file appropriately
  snprintf(unix_socket_file, sizeof(unix_socket_file), "/tmp/%s.%i", argv[0], getpid()); 
  
  // register signal handlers
  Signal(SIGCHLD, sigchld);
  Signal(SIGTERM, sigdie);
  Signal(SIGINT, sigdie);

  // set up unix domain socket
  uxlsd = ux_server_socket((const char *)unix_socket_file, MINIONS);

  // set up network socket
  fprintf(debugmsg, "LISTENQ is %i\n", LISTENQ);
  if((listenport = atoi(argv[optind])) == 0)
    listenport = getportnum(argv[optind], "tcp");
  nlsd = network_server_socket(listenport, LISTENQ);
  fprintf(debugmsg, "looking for port... %s\n", argv[optind]);

  // minion setup
  for(counter = 0; counter < MINIONS; counter++)
    spawnaminion(counter);

  // let the user know what superd's pid is
  fprintf(stderr, "superd's pid is %i\n", (int)getpid());

  // redirect stderr messages
  if((dup2(debugmsgfd, 2)) < 0)
    perror("dup2");

  fdset[0].fd = nlsd;
  fdset[0].events = POLLIN;

  while(1) {
    fprintf(debugmsg, "polling...\n");
    pollreturn = poll(fdset, 1, -1);
    fprintf(debugmsg, "poll returned %i\n", pollreturn);
    if(pollreturn == -1) {
      if(errno == EINTR) {
	fprintf(debugmsg, "poll interrupted.  continuing...\n");
	minioncount();
	continue;
      } else {
	perror("poll");
	exit(1);
      } 
    } // end poll error (if) condition

    // we got a connection from the network
    if(fdset[0].revents & POLLIN) {
      for(counter = 0; counter < MINIONS; counter++) {
	if(minions[counter].ready) {
	  ncsd = accept(nlsd, (struct sockaddr *)&inaddr, &inaddrlen);
	  sendfd(minions[counter].uxcsd, ncsd);
	  close(ncsd);
	  minions[counter].ready = 0;
	  break;
	}
      }
    }

    // let's recheck for dead children in case we're on a system where slow
    // system calls are re-entered when interrupted by signals
    minioncount();

  } // end event (while) loop


}