2 * Master migration daemon
4 * The master migration daemon waits for events on a file and
5 * spawns a child process to handle the event
7 * This code was written by Peter Lawthers, and placed in the public
8 * domain for use by DMAPI implementors and app writers.
10 * Standard disclaimer:
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
12 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
15 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
16 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
19 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
20 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 #include <sys/types.h>
26 #include <sys/resource.h>
37 extern int optind, optopt, opterr;
41 dm_sessid_t sid = DM_NO_SESSION;
43 extern int setup_dmapi(dm_sessid_t *);
44 extern void err_msg(char *, ...);
45 extern void errno_msg(char *, ...);
47 void event_loop(dm_sessid_t);
48 int set_events(dm_sessid_t, void *, size_t);
49 int mk_daemon(char *);
50 void spawn_kid(dm_sessid_t, dm_token_t, char *);
60 fprintf(stderr, "Usage: %s ", prog);
61 fprintf(stderr, " <-v verbose> ");
62 fprintf(stderr, " <-l logfile> ");
63 fprintf(stderr, "filesystem \n");
75 char *fsname, *logfile;
84 while ((c = getopt(argc, argv, "vl:")) != EOF) {
102 fsname = argv[optind];
103 if (fsname == NULL) {
108 * If no logfile name is specified, we'll just send
109 * all output to some file in /tmp
112 logfile = LOG_DEFAULT;
116 * Turn ourselves into a daemon
119 error = mk_daemon(logfile);
126 * Now we have our filesystem name and possibly a size threshold
127 * to look for. Init the dmapi, and get a filesystem handle so
128 * we can set up our events
130 error = setup_dmapi(&sid);
134 if (dm_path_to_fshandle(fsname, &fs_hanp, &fs_hlen) == -1) {
135 errno_msg("Can't get filesystem handle");
141 * Set the event disposition so that our session will receive
142 * the managed region events (read, write, and truncate)
144 error = set_events(sid, fs_hanp, fs_hlen);
150 * Now wait forever for messages, spawning kids to
158 * Main event loop processing
165 size_t bufsize, rlen;
170 * We take a swag at a buffer size. If it's wrong, we can
173 bufsize = sizeof(dm_eventmsg_t) + sizeof(dm_data_event_t) + HANDLE_LEN;
175 msgbuf = (void *)malloc(bufsize);
176 if (msgbuf == NULL) {
177 err_msg("Can't allocate memory for buffer\n");
182 error = dm_get_events(sid, ALL_AVAIL_MSGS, DM_EV_WAIT, bufsize,
185 if (errno == E2BIG) {
187 msgbuf = (void *)malloc(rlen);
188 if (msgbuf == NULL) {
189 err_msg("Can't resize msg buffer\n");
194 errno_msg("Error getting events from DMAPI");
199 * Walk thru the message buffer, pull out each individual
200 * message, and dispatch the messages to child processes
201 * with the sid, token, and data. The children will
202 * respond to the events.
204 msg = (dm_eventmsg_t *)msgbuf;
205 while (msg != NULL ) {
207 fprintf(stderr, "Received %s, token %d\n",
208 (msg->ev_type == DM_EVENT_READ ? "read" :
209 (msg->ev_type == DM_EVENT_WRITE ? "write" : "trunc")), msg->ev_token);
211 switch (msg->ev_type) {
214 spawn_kid(sid, msg->ev_token, RESTORE_FILE);
218 case DM_EVENT_TRUNCATE:
219 spawn_kid(sid, msg->ev_token, INVAL_FILE);
223 err_msg("Invalid msg type %d\n", msg->ev_type);
226 msg = DM_STEP_TO_NEXT(msg, dm_eventmsg_t *);
237 * Fork and exec our worker bee to work on the file. If
238 * there is any error in fork/exec'ing the file, we have to
239 * supply the error return to the event. Once the child gets
240 * started, they will respond to the event for us.
255 * We're in the child. Try and exec the worker bee
257 sprintf(sidbuf, "%d", sid);
258 sprintf(tokenbuf, "%d", token);
260 fprintf(stderr, "execl(%s, %s, %s, -s, %s, -t, %s, 0)\n",
261 WORKER_BEE, WORKER_BEE, action, sidbuf,
264 execlp(WORKER_BEE, WORKER_BEE, action, "-s", sidbuf,
265 "-t", tokenbuf, NULL);
266 errno_msg("execlp of worker bee failed");
267 (void)dm_respond_event(sid, token, DM_RESP_ABORT,
273 err_msg("Can't fork worker bee\n");
274 (void)dm_respond_event(sid, token, DM_RESP_ABORT, errno,
284 * Set the event disposition of the managed region events
292 dm_eventset_t eventlist;
294 DMEV_ZERO(eventlist);
295 DMEV_SET(DM_EVENT_READ, eventlist);
296 DMEV_SET(DM_EVENT_WRITE, eventlist);
297 DMEV_SET(DM_EVENT_TRUNCATE, eventlist);
299 if (dm_set_disp(sid, fs_hanp, fs_hlen, DM_NO_TOKEN, &eventlist,
302 errno_msg("Can't set event disposition");
312 * Dissassociate ourselves from our tty, and close all files
323 if ((pid = fork()) == -1)
333 * Determine how many open files we've got and close
336 if (getrlimit(RLIMIT_NOFILE, &lim) < 0 ) {
337 errno_msg("Can't determine max number of open files");
340 for (i=0; i<lim.rlim_cur; i++)
344 * For error reporting, we re-direct stdout and stderr to a
347 if ((fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0) {
348 errno_msg("Can't open logfile %s", logfile);
351 (void)dup2(fd, STDOUT_FILENO);
352 (void)dup2(fd, STDERR_FILENO);
362 struct sigaction act;
365 * Set up signals so that we can wait for spawned children
367 act.sa_handler = migin_exit;
369 sigemptyset(&act.sa_mask);
371 (void)sigaction(SIGHUP, &act, NULL);
372 (void)sigaction(SIGINT, &act, NULL);
373 (void)sigaction(SIGQUIT, &act, NULL);
374 (void)sigaction(SIGTERM, &act, NULL);
375 (void)sigaction(SIGUSR1, &act, NULL);
376 (void)sigaction(SIGUSR1, &act, NULL);
377 (void)sigaction(SIGUSR2, &act, NULL);
386 * We could try and kill off all our kids, but we're not
387 * 'Mr. Mean', so we just wait for them to die.
389 err_msg("%s: waiting for all children to die...\n", Progname);
390 while (wait3((int *)0, WNOHANG, (struct rusage *)0) > 0)
393 fprintf(stdout, "\n");
399 * Should do a set_disp to 0 and then drain the session
400 * queue as well. On the SGI, we'll need to make the
401 * filesystem handle global so that we can get at it
404 error = dm_destroy_session(sid);
406 errno_msg("Can't shut down session\n");