1 /* $NetBSD: cleansrv.c,v 1.2 2006/05/12 19:33:02 perseant Exp $ */
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant@hhhh.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #ifdef USE_CLIENT_SERVER
37 #include <sys/syslog.h>
38 #include <sys/param.h>
39 #include <sys/mount.h>
40 #include <sys/socket.h>
42 #include <ufs/ufs/inode.h>
43 #include <ufs/lfs/lfs.h>
51 #define LFS_CLEANERD_SOCKDIR "/tmp/.lfs_cleanerd"
52 #define LFS_CLEANERD_SOCKFILE LFS_CLEANERD_SOCKDIR "/socket"
54 int control_socket
= -1;
56 extern struct clfs
**fsp
;
58 struct lfs_cleanerd_cmd
{
65 check_control_socket(void)
68 struct lfs_cleanerd_cmd cmd
;
71 if (control_socket
< 0)
75 ioctl(control_socket
, FIONREAD
, &c
);
78 read(control_socket
, (char *)&cmd
, sizeof(cmd
));
81 case 'C': /* Add filesystem for cleaning */
83 nfsp
= (struct clfs
**)realloc(fsp
,
91 fsp
[nfss
- 1] = (struct clfs
*)malloc(sizeof(**fsp
));
92 if (fsp
[nfss
- 1] == NULL
) {
93 syslog(LOG_ERR
, "%s: couldn't alloc memory: %m"
99 if ((r
= init_fs(fsp
[nfss
- 1], cmd
.data
)) < 0) {
100 syslog(LOG_ERR
, "%s: couldn't init: "
101 "error code %d", cmd
.data
, r
);
102 handle_error(fsp
, nfss
- 1);
106 syslog(LOG_NOTICE
, "unknown message type %d", cmd
.cmd
);
113 send_fss_to_master(int argc
, char **argv
)
115 struct sockaddr_un sun
;
116 struct lfs_cleanerd_cmd cmd
;
119 strcpy(sun
.sun_path
, LFS_CLEANERD_SOCKFILE
);
120 sun
.sun_family
= AF_LOCAL
;
121 sun
.sun_len
= sizeof(sa_family_t
) + 1 + strlen(sun
.sun_path
);
123 s
= socket(PF_LOCAL
, SOCK_DGRAM
, 0);
125 syslog(LOG_DEBUG
, "open failed: %m");
130 for (i
= 0; i
< argc
; i
++) {
131 strncpy(cmd
.data
, argv
[i
], PATH_MAX
);
132 cmd
.len
= 2 * sizeof(int) + strlen(cmd
.data
) + 1;
133 r
= sendto(s
, &cmd
, sizeof(cmd
), 0, (struct sockaddr
*)&sun
,
136 syslog(LOG_DEBUG
, "sendto failed: %m");
144 sig_donothing(int sig
)
147 dlog("caught sigio");
153 if (control_socket
>= 0) {
154 close(control_socket
);
155 unlink(LFS_CLEANERD_SOCKFILE
);
156 rmdir(LFS_CLEANERD_SOCKDIR
);
161 try_to_become_master(int argc
, char **argv
)
163 struct sockaddr_un sun
;
169 if (mkdir(LFS_CLEANERD_SOCKDIR
, 0700) < 0) {
173 fd
= open("/var/run/lfs_cleanerd.pid", O_RDONLY
);
175 read(fd
, scratch
, 80);
178 if (kill(pid
, 0) == 0) {
179 send_fss_to_master(argc
, argv
);
186 * Master is no longer present even though directory
187 * exists. Remove the socket and proceed. There is
188 * a race condition here which could result in more than
189 * one master daemon. That would not be a catastrophe.
191 if (unlink(LFS_CLEANERD_SOCKFILE
) != 0)
196 * Create the socket and bind it in the namespace
198 control_socket
= socket(PF_LOCAL
, SOCK_DGRAM
, 0);
199 strcpy(sun
.sun_path
, LFS_CLEANERD_SOCKFILE
);
200 sun
.sun_family
= AF_LOCAL
;
201 sun
.sun_len
= sizeof(sa_family_t
) + 1 + strlen(sun
.sun_path
);
202 bind(control_socket
, (struct sockaddr
*)&sun
, sizeof(sun
));
204 /* Clean up when we leave */
205 atexit(cleanup_socket
);
208 * Wake us when there is i/o on this socket. We don't need
209 * to actually do anything when we get the signal, but we
210 * have to install a signal handler so LFCNSEGWAIT will be
211 * interrupted when data comes in on the socket.
213 fcntl(control_socket
, F_SETOWN
, getpid());
214 flags
= fcntl(control_socket
, F_GETFL
, NULL
);
216 fcntl(control_socket
, F_SETFL
, flags
);
217 signal(SIGIO
, sig_donothing
);
219 /* And finally record our pid */
220 pidfile("lfs_cleanerd");
222 #endif /* USE_CLIENT_SERVER */