2 * Copyright (c) 1995 - 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <arla_local.h>
39 char *default_log_file
= "syslog";
42 static char *pid_filename
;
44 #define _PATH_VAR_RUN "/var/run"
45 #define _PATH_DEV_NNPFS0 "/dev/nnpfs0"
46 #define _PATH_DEV_STDERR "/dev/stderr"
54 write_pid_file (const char *progname
)
58 asprintf (&pid_filename
, _PATH_VAR_RUN
"/%s.pid", progname
);
59 if (pid_filename
== NULL
)
61 fp
= fopen (pid_filename
, "w");
64 fprintf (fp
, "%u", (unsigned)getpid());
69 delete_pid_file (void)
71 if (pid_filename
!= NULL
) {
72 unlink (pid_filename
);
85 arla_warnx (ADEBMISC
, "fatal signal received");
118 arla_err (1, ADEBERROR
, errno
, "fork");
120 signal(SIGUSR1
, sigusr1
);
121 signal(SIGCHLD
, sigchild
);
126 arla_err (1, ADEBERROR
, errno
, "setsid");
127 fd
= open(_PATH_DEVNULL
, O_RDWR
, 0);
129 arla_err (1, ADEBERROR
, errno
, "open " _PATH_DEVNULL
);
130 dup2 (fd
, STDIN_FILENO
);
131 dup2 (fd
, STDOUT_FILENO
);
132 dup2 (fd
, STDERR_FILENO
);
139 arla_start (char *device_file
, const char *cache_dir
)
141 struct kernel_args kernel_args
;
143 signal (SIGINT
, sigint
);
144 signal (SIGTERM
, sigint
);
145 signal (SIGHUP
, sighup
);
146 umask (S_IRWXG
|S_IRWXO
); /* 077 */
148 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_DUMPABLE)
149 prctl(PR_SET_DUMPABLE
, 1);
152 nnpfs_message_init ();
153 kernel_opendevice (device_file
);
155 kernel_args
.num_workers
= num_workers
;
156 kernel_args
.num_bufs
= num_workers
* 2; /* XXX */
158 write_pid_file ("arlad");
160 if (chroot (cache_dir
) < 0)
161 arla_err (1, ADEBERROR
, errno
, "chroot %s", cache_dir
);
164 arla_err (1, ADEBERROR
, errno
, "chdir /");
167 kill(getppid(), SIGUSR1
);
169 kernel_interface(&kernel_args
);
174 get_default_cache_dir (void)
179 char *device_file
= _PATH_DEV_NNPFS0
;
181 static struct getargs args
[] = {
182 {"cache-dir", 0, arg_string
, &cache_dir
,
183 "cache directory", "directory"},
184 {"check-consistency", 'C', arg_flag
, &cm_consistency
,
185 "if we want extra paranoid consistency checks", NULL
},
186 {"cpu-usage", 0, arg_flag
, &cpu_usage
,
188 {"conffile", 'c', arg_string
, &conf_file
,
189 "path to configuration file", "file"},
190 {"connected-mode", 0, arg_string
, &connected_mode_string
,
191 "initial connected mode [conncted|fetch-only|disconnected]", NULL
},
192 {"debug", 0, arg_string
, &debug_levels
,
193 "what to write in the log", NULL
},
194 {"device", 'd', arg_string
, &device_file
,
195 "the NNPFS device to use [/dev/nnpfs0]", "path"},
196 {"dynroot", 'D', arg_flag
, &dynroot_enable
,
197 "if dynroot is enabled", NULL
},
198 {"largefile", 0, arg_negative_flag
, &use_o_largefile
,
199 "stop using O_LARGEFILE flag when opening cache files", NULL
},
200 {"log", 'l', arg_string
, &log_file
,
201 "where to write log (stderr (default), syslog, or path to file)", NULL
},
202 {"fake-mp", 0, arg_flag
, &fake_mp
,
203 "enable fake mountpoints", NULL
},
204 {"fake-stat", 0, arg_flag
, &fake_stat
,
205 "build stat info from afs rights", NULL
},
206 {"fork", 'n', arg_negative_flag
, &fork_flag
,
207 "don't fork and demonize", NULL
},
208 {"port", 0, arg_integer
, &client_port
,
209 "port number to use", "number"},
210 {"recover", 'z', arg_negative_flag
, &recover
,
211 "don't recover state", NULL
},
212 {"root-volume",0, arg_string
, &root_volume
},
214 {"rxkad-level", 'r', arg_string
, &rxkad_level_string
,
215 "the rxkad level to use (clear, auth or crypt)", NULL
},
217 {"sysname", 's', arg_string
, &argv_sysname
,
218 "set the sysname of this system", NULL
},
219 {"tracefile", 0, arg_string
, &trace_file
,
220 "record nnpfs traffic in file", NULL
},
221 {"workers", 0, arg_integer
, &num_workers
,
222 "number of worker threads", NULL
},
223 {"version", 0, arg_flag
, &version_flag
,
225 {"help", 0, arg_flag
, &help_flag
,
232 arg_printusage (args
, sizeof(args
)/sizeof(*args
), NULL
, "[device]");
237 main (int argc
, char **argv
)
242 setprogname (argv
[0]);
246 if (getarg (args
, sizeof(args
)/sizeof(*args
), argc
, argv
, &optind
))
256 print_version (NULL
);
270 default_log_file
= _PATH_DEV_STDERR
;
279 arla_start (device_file
, cache_dir
);