2 * Copyright (C) 2012 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 (* Running the daemon as root is a mistake. It must be run as a
26 let euid = geteuid () in
28 eprintf "whenjobsd: this daemon must run as the local user, NOT root\n";
32 (* $HOME must be defined and must exist and be a directory and must be
33 * owned by the current user.
38 eprintf "whenjobsd: $HOME environment variable must be defined\n";
43 with Unix_error (err, fn, _) ->
44 eprintf "whenjobsd: %s: %s ($HOME): %s\n" fn home (error_message err);
46 if stat.st_kind != S_DIR then (
47 eprintf "whenjobsd: %s ($HOME): not a directory\n" home;
51 if stat.st_uid != euid then (
52 eprintf "whenjobsd: %s ($HOME): not owned by the current user (uid %d)\n"
57 (* Parse the command line arguments. *)
58 let debug = ref false in
59 let do_fork = ref true in
61 let display_version () =
62 printf "%s %s\n" Config.package_name Config.package_version;
66 let argspec = Arg.align [
67 "-d", Arg.Set debug, " Enable extra debugging messages";
68 "-f", Arg.Clear do_fork, " Don't fork into background";
69 "-V", Arg.Unit display_version, " Display version number and exit";
70 "--version", Arg.Unit display_version, " Display version number and exit";
73 let anon_fun _ = raise (Arg.Bad "unknown command line argument") in
79 For documentation see the whenjobs(1) and whenjobsd(8) man pages.
84 Arg.parse argspec anon_fun usage_msg;
87 let do_fork = !do_fork in
89 (* Make the $HOME/.whenjobs directory if it doesn't exist. *)
90 let jobsdir = sprintf "%s/.whenjobs" home in
91 (try mkdir jobsdir 0o700 with Unix_error _ -> ());
93 (* Create the socket. *)
94 Daemon.init jobsdir debug;
96 (* Fork into background. *)
99 if pid > 0 then exit 0;
101 (* chdir / so we don't prevent filesystems from being unmounted. *)
104 (* Close file descriptors. *)
109 (* Create a new session. *)
113 Sys.set_signal Sys.sighup Sys.Signal_ignore;
115 (* Update the PID file since we just forked. *)
116 Whenlock.update_pid ();
120 Syslog.notice "daemon started: version=%s uid=%d home=%s"
121 Config.package_version euid home;
123 (* If there is a jobs.cmo file, load it. *)
125 let file = sprintf "%s/jobs.cmo" jobsdir in
126 if Sys.file_exists file then
127 try Daemon.reload_file () with Failure _ -> () in
129 (* Go into main loop. *)