Home | History | Annotate | Line # | Download | only in lfs_cleanerd
cleansrv.c revision 1.1
      1 /* $NetBSD: cleansrv.c,v 1.1 2006/03/30 19:10:13 perseant Exp $	 */
      2 
      3 /*-
      4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #ifdef USE_CLIENT_SERVER
     40 
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <stdio.h>
     44 #include <sys/syslog.h>
     45 #include <sys/param.h>
     46 #include <sys/mount.h>
     47 #include <sys/socket.h>
     48 #include <sys/un.h>
     49 #include <ufs/ufs/inode.h>
     50 #include <ufs/lfs/lfs.h>
     51 
     52 #include "bufcache.h"
     53 #include "vnode.h"
     54 #include "lfs.h"
     55 #include "fdfs.h"
     56 #include "cleaner.h"
     57 
     58 #define LFS_CLEANERD_SOCKDIR	"/tmp/.lfs_cleanerd"
     59 #define LFS_CLEANERD_SOCKFILE	LFS_CLEANERD_SOCKDIR "/socket"
     60 
     61 int control_socket = -1;
     62 extern int nfss;
     63 extern struct clfs **fsp;
     64 
     65 struct lfs_cleanerd_cmd {
     66 	int  cmd;
     67 	int  len;
     68 	char data[PATH_MAX];
     69 };
     70 
     71 void
     72 check_control_socket(void)
     73 {
     74 	int c, r;
     75 	struct lfs_cleanerd_cmd cmd;
     76 	struct clfs **nfsp;
     77 
     78 	if (control_socket < 0)
     79 		return;
     80 
     81 	while(1) {
     82 		ioctl(control_socket, FIONREAD, &c);
     83 		if (c <= 0)
     84 			return;
     85 		read(control_socket, (char *)&cmd, sizeof(cmd));
     86 
     87 		switch(cmd.cmd) {
     88 		    case 'C': /* Add filesystem for cleaning */
     89 			++nfss;
     90 			nfsp = (struct clfs **)realloc(fsp,
     91 						       nfss * sizeof(*fsp));
     92 			if (nfsp == NULL) {
     93 				--nfss;
     94 				break;
     95 			}
     96 			fsp = nfsp;
     97 
     98 			fsp[nfss - 1] = (struct clfs *)malloc(sizeof(**fsp));
     99 			if (fsp[nfss - 1] == NULL) {
    100 				--nfsp;
    101 				break;
    102 			}
    103 
    104 			if ((r = init_fs(fsp[nfss - 1], cmd.data)) < 0) {
    105 				syslog(LOG_ERR, "%s: couldn't init: "
    106 				       "error code %d", cmd.data, r);
    107 				handle_error(fsp, nfss - 1);
    108 			}
    109 			break;
    110 		    default:
    111 			syslog(LOG_NOTICE, "unknown message type %d", cmd.cmd);
    112 			break;
    113 		}
    114 	}
    115 }
    116 
    117 static int
    118 send_fss_to_master(int argc, char **argv)
    119 {
    120 	struct sockaddr_un sun;
    121 	struct lfs_cleanerd_cmd cmd;
    122 	int i, r, s;
    123 
    124 	strcpy(sun.sun_path, LFS_CLEANERD_SOCKFILE);
    125 	sun.sun_family = AF_LOCAL;
    126 	sun.sun_len = sizeof(sa_family_t) + 1 + strlen(sun.sun_path);
    127 
    128 	s = socket(PF_LOCAL, SOCK_DGRAM, 0);
    129 	if (s < 0) {
    130 		syslog(LOG_DEBUG, "open failed: %m");
    131 		return -1;
    132 	}
    133 
    134 	cmd.cmd = 'C';
    135 	for (i = 0; i < argc; i++) {
    136 		strncpy(cmd.data, argv[i], PATH_MAX);
    137 		cmd.len = 2 * sizeof(int) + strlen(cmd.data) + 1;
    138 		r = sendto(s, &cmd, sizeof(cmd), 0, (struct sockaddr *)&sun,
    139 			   sizeof(sun));
    140 		if (r < 0) {
    141 			syslog(LOG_DEBUG, "sendto failed: %m");
    142 			return -1;
    143 		}
    144 	}
    145 	return 0;
    146 }
    147 
    148 static void
    149 sig_donothing(int sig)
    150 {
    151 	/* Do nothing */
    152 	dlog("caught sigio");
    153 }
    154 
    155 static void
    156 cleanup_socket(void)
    157 {
    158 	if (control_socket >= 0) {
    159 		close(control_socket);
    160 		unlink(LFS_CLEANERD_SOCKFILE);
    161 		rmdir(LFS_CLEANERD_SOCKDIR);
    162 	}
    163 }
    164 
    165 void
    166 try_to_become_master(int argc, char **argv)
    167 {
    168 	struct sockaddr_un sun;
    169 	int fd;
    170 	int pid;
    171 	int flags;
    172 	char scratch[80];
    173 
    174 	if (mkdir(LFS_CLEANERD_SOCKDIR, 0700) < 0) {
    175 		if (errno != EEXIST)
    176 			return;
    177 		pid = 0;
    178 		fd = open("/var/run/lfs_cleanerd.pid", O_RDONLY);
    179 		if (fd >= 0) {
    180 			read(fd, scratch, 80);
    181 			scratch[79] = '\0';
    182 			pid = atoi(scratch);
    183 			if (kill(pid, 0) == 0) {
    184 				send_fss_to_master(argc, argv);
    185 				exit(0);
    186 			}
    187 			close(fd);
    188 		}
    189 
    190 		/*
    191 		 * Master is no longer present even though directory
    192 		 * exists.  Remove the socket and proceed.  There is
    193 		 * a race condition here which could result in more than
    194 		 * one master daemon.  That would not be a catastrophe.
    195 		 */
    196 		if (unlink(LFS_CLEANERD_SOCKFILE) != 0)
    197 			return;
    198 	}
    199 
    200 	/*
    201 	 * Create the socket and bind it in the namespace
    202 	 */
    203 	control_socket = socket(PF_LOCAL, SOCK_DGRAM, 0);
    204 	strcpy(sun.sun_path, LFS_CLEANERD_SOCKFILE);
    205 	sun.sun_family = AF_LOCAL;
    206 	sun.sun_len = sizeof(sa_family_t) + 1 + strlen(sun.sun_path);
    207 	bind(control_socket, (struct sockaddr *)&sun, sizeof(sun));
    208 
    209 	/* Clean up when we leave */
    210 	atexit(cleanup_socket);
    211 
    212 	/*
    213 	 * Wake us when there is i/o on this socket.  We don't need
    214 	 * to actually do anything when we get the signal, but we
    215 	 * have to install a signal handler so LFCNSEGWAIT will be
    216 	 * interrupted when data comes in on the socket.
    217 	 */
    218 	fcntl(control_socket, F_SETOWN, getpid());
    219 	flags = fcntl(control_socket, F_GETFL, NULL);
    220 	flags |= O_ASYNC;
    221 	fcntl(control_socket, F_SETFL, flags);
    222 	signal(SIGIO, sig_donothing);
    223 
    224 	/* And finally record our pid */
    225 	pidfile("lfs_cleanerd");
    226 }
    227 #endif /* USE_CLIENT_SERVER */
    228