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 let libdir = ref Libdir.libdir
26 (* Running the program as root is a mistake. It must be run as a
29 let euid = geteuid () in
31 eprintf "whenjobs: this program must not be run as root\n";
35 (* $HOME must be defined and must exist and be a directory and must be
36 * owned by the current user.
41 eprintf "whenjobs: $HOME environment variable must be defined\n";
46 with Unix_error (err, fn, _) ->
47 eprintf "whenjobs: %s: %s ($HOME): %s\n" fn home (error_message err);
49 if stat.st_kind != S_DIR then (
50 eprintf "whenjobs: %s ($HOME): not a directory\n" home;
54 if stat.st_uid != euid then (
55 eprintf "whenjobs: %s ($HOME): not owned by the current user (uid %d)\n"
60 (* Make the $HOME/.whenjobs directory if it doesn't exist. *)
61 let jobsdir = sprintf "%s/.whenjobs" home in
62 (try mkdir jobsdir 0o700 with Unix_error _ -> ());
67 (* Parse the command line arguments. *)
68 let mode = ref None in
69 let typ = ref "string" in
71 let set_mode m () = mode := Some m in
73 let display_version () =
74 printf "%s %s\n" Config.package_name Config.package_version;
78 let argspec = Arg.align [
79 "--daemon-start", Arg.Unit (set_mode `Daemon_start), " Start the daemon";
80 "--daemon-status", Arg.Unit (set_mode `Daemon_status), " Display the status of the daemon";
81 "--daemon-stop", Arg.Unit (set_mode `Daemon_stop), " Stop the daemon";
82 "--daemon-restart", Arg.Unit (set_mode `Daemon_restart), " Restart the daemon";
83 "-e", Arg.Unit (set_mode `Edit), " Edit and upload the script";
84 "--edit", Arg.Unit (set_mode `Edit), " Edit and upload the script";
85 "--get", Arg.Unit (set_mode `Get), " Display the variable";
86 "-l", Arg.Unit (set_mode `List), " List the script";
87 "--list", Arg.Unit (set_mode `List), " List the script";
88 "--lib", Arg.Set_string libdir, "dir Specify directory that contains pa_when.cmo";
89 "--set", Arg.Unit (set_mode `Set), " Set the variable";
90 "--type", Arg.Set_string typ, "bool|int|float|string Set the variable type";
91 "--upload", Arg.Unit (set_mode `Upload), " Upload the script";
92 "--variables", Arg.Unit (set_mode `Variables), " Display all variables and values";
93 "-V", Arg.Unit display_version, " Display version number and exit";
94 "--version", Arg.Unit display_version, " Display version number and exit";
98 let anon_fun str = args := str :: !args in
101 Whenjobs is a powerful but simple cron replacement.
102 Whenjobs copyright (C) 2012 Red Hat Inc.
109 Get and set variables:
111 whenjobs --get variable
112 whenjobs --set variable value
114 Start and stop the per-user daemon:
116 whenjobs --daemon-start | --daemon-stop | --daemon-status
118 For documentation see the whenjobs(1) man page.
123 Arg.parse argspec anon_fun usage_msg;
126 let args = List.rev !args in
128 let typ = match !typ with
129 | "bool"|"boolean" -> `Bool
130 | "string" -> `String
132 | "float"|"double" -> `Float
134 eprintf "whenjobs: --type: unknown type (%s)\n" t;
137 (* Depending on the selected mode, perform a different action. *)
140 eprintf "whenjobs: no operation selected.\n";
145 unused_error args "-e";
149 unused_error args "-l";
153 unused_error args "--upload";
157 if List.length args != 2 then (
158 eprintf "whenjobs --set variable value\n";
159 eprintf "If 'value' contains spaces, you may need to quote it.\n";
163 set_variable (List.hd args) (List.hd (List.tl args)) typ
166 if List.length args != 1 then (
167 eprintf "whenjobs --get variable\n";
171 get_variable (List.hd args)
174 unused_error args "--variables";
177 | Some `Daemon_start ->
178 unused_error args "--daemon-start";
181 | Some `Daemon_stop ->
182 unused_error args "--daemon-stop";
185 | Some `Daemon_restart ->
186 unused_error args "--daemon-restart";
189 | Some `Daemon_status ->
190 unused_error args "--daemon-status";
194 (* If there is no initial file, create an empty one containing the
197 let file = get_jobs_filename () in
198 if not (Sys.file_exists file) then
199 create_tutorial file;
201 (* Is $EDITOR set? If not, use a default. *)
202 let editor = try getenv "EDITOR" with Not_found -> "vi" in
204 (* Get the (size, MD5) of the file to tell if it changed. *)
206 try (lstat file).st_size, Digest.file file
207 with Unix_error (err, fn, _) ->
208 eprintf "whenjobs: %s: %s: %s\n" fn file (error_message err);
211 let old_stamp = file_stamp () in
213 let cmd = sprintf "%s %s" editor file in
214 if Sys.command cmd != 0 then (
215 eprintf "whenjobs: error editing file (is $EDITOR set correctly?)\n";
219 let new_stamp = file_stamp () in
221 if old_stamp <> new_stamp then
225 let file = get_jobs_filename () in
226 if not (Sys.file_exists file) then (
227 eprintf "whenjobs: there is no jobs file, use 'whenjobs -e' to create one\n";
230 let chan = open_in file in
232 printf "%s\n" (input_line chan);
235 (try loop () with End_of_file -> ());
239 (* Recompile the jobs file. *)
240 let file = get_jobs_filename () in
241 let cmo_file = sprintf "%s/jobs.cmo" jobsdir in
242 let cmd = sprintf "ocamlfind ocamlc -I +camlp4 -I %s -package unix,camlp4.lib -pp 'camlp4o %s/pa_when.cmo' -c %s -o %s"
243 !libdir !libdir file cmo_file in
244 if Sys.command cmd <> 0 then (
245 eprintf "whenjobs: could not compile jobs script, see earlier error messages\n";
246 eprintf "compile command was:\n%s\n" cmd;
250 (* Test-load the jobs file to ensure it makes sense. *)
253 Dynlink.loadfile cmo_file
256 eprintf "whenjobs: %s\n" (Dynlink.error_message err);
257 (* Since it failed, unlink it. *)
258 (try unlink cmo_file with Unix_error _ -> ());
262 (* OK now let's tell the daemon to reload it. *)
263 let client = start_client () in
264 (match Whenproto_clnt.When.V1.reload_file client () with
267 eprintf "whenjobs: reload: %s\n" msg;
268 suggest_check_server_logs ();
273 and set_variable name value typ =
274 let value = match typ with
277 | "true"|"t"|"yes"|"y"|"on"|"1" -> `bool_t true
278 | "false"|"f"|"no"|"n"|"off"|"0" -> `bool_t false
280 eprintf "whenjobs: variable does not have a boolean value\n";
283 | `String -> `string_t value
285 (try ignore (big_int_of_string value)
287 eprintf "whenjobs: variable is not an integer\n";
290 `int_t value (* the string is what we pass over the wire *)
292 (try `float_t (float_of_string value)
294 eprintf "whenjobs: variable is not a floating point number\n";
298 let client = start_client () in
299 Whenproto_clnt.When.V1.set_variable client (name, value);
302 and get_variable name =
303 let client = start_client () in
304 let value = Whenproto_clnt.When.V1.get_variable client name in
305 print_endline (string_of_variable value);
308 and list_variables () =
309 let client = start_client () in
310 let names = Whenproto_clnt.When.V1.get_variable_names client () in
313 let value = Whenproto_clnt.When.V1.get_variable client name in
314 printf "%s=%s\n" name (string_of_variable value)
318 and daemon_start () =
322 let client = start_client () in
323 (match Whenproto_clnt.When.V1.exit_daemon client () with
326 eprintf "whenjobs: daemon-stop: %s\n" msg;
327 suggest_check_server_logs ();
332 and daemon_restart () =
335 and daemon_status () =
338 and unused_error args op =
340 eprintf "whenjobs %s: unused parameters on the command line.\n" op;
345 and suggest_help () =
346 eprintf "Use 'whenjobs --help' for a summary of options or read whenjobs(1) man page.\n"
348 and suggest_check_server_logs () =
349 eprintf "Look at the server logs (/var/log/cron or /var/log/syslog usually) for\n";
350 eprintf "further information on why this daemon operation failed.\n"
352 and get_jobs_filename () =
353 sprintf "%s/jobs.ml" jobsdir
355 and create_tutorial file =
356 let chan = open_out file in
357 output_string chan Tutorial.tutorial;
360 and start_client () =
361 let addr = sprintf "%s/socket" jobsdir in
364 Whenproto_clnt.When.V1.create_client
365 (Rpc_client.Unix addr)
366 Rpc.Tcp (* not TCP, this is the same as SOCK_STREAM *)
368 | Unix_error ((ECONNREFUSED|ENOENT), _, _) ->
369 eprintf "whenjobs: error: the daemon ('whenjobsd') is not running\n";
370 eprintf "Use 'whenjobs --daemon-start' to start the daemon.\n";
372 | Unix_error (err, fn, _) ->
373 eprintf "whenjobs: %s: %s: %s\n" fn addr (error_message err);
377 and stop_client client =
378 Rpc_client.shut_down client
380 and string_of_variable = function
381 | `bool_t b -> string_of_bool b
383 | `int_t i -> i (* passed on the wire as a string *)
384 | `float_t f -> string_of_float f
389 (* Pretty print some of the exceptions that main can throw. *)
390 | Rpc.Rpc_server err ->
391 eprintf "whenjobs: rpc error: %s\n" (Rpc.string_of_server_error err);
392 suggest_check_server_logs ();
395 eprintf "whenjobs: error: %s\n" msg;
397 | Invalid_argument msg ->
398 eprintf "whenjobs: invalid argument: %s\n" msg;
401 eprintf "whenjobs: error: %s\n" (Printexc.to_string exn);