2 * ion/utils/ion-statusd/ion-statusd.c
4 * Copyright (c) Tuomo Valkonen 2004-2006.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
14 #include <sys/types.h>
17 #include <libtu/util.h>
18 #include <libtu/optparser.h>
19 #include <libtu/errorlog.h>
20 #include <libtu/locale.h>
21 #include <libtu/misc.h>
22 #include <libtu/prefix.h>
23 #include <libextl/readconfig.h>
24 #include <libmainloop/select.h>
25 #include <libmainloop/signal.h>
26 #include <libmainloop/defer.h>
32 #include "../../version.h"
35 static OptParserOpt ion_opts[]={
36 /*{OPT_ID('d'), "display", OPT_ARG, "host:dpy.scr",
37 DUMMY_TR("X display to use")},*/
39 {'c', "conffile", OPT_ARG, "config_file",
40 DUMMY_TR("Configuration file")},
42 {'s', "searchdir", OPT_ARG, "dir",
43 DUMMY_TR("Add directory to search path")},
45 /*{OPT_ID('s'), "session", OPT_ARG, "session_name",
46 DUMMY_TR("Name of session (affects savefiles)")},*/
48 {'h', "help", 0, NULL,
49 DUMMY_TR("Show this help")},
51 {'V', "version", 0, NULL,
52 DUMMY_TR("Show program version")},
54 {OPT_ID('a'), "about", 0, NULL,
55 DUMMY_TR("Show about text")},
57 {'q', "quiet", 0, NULL,
58 DUMMY_TR("Quiet mode")},
60 {'m', "meter", OPT_ARG, "meter_module",
61 DUMMY_TR("Load a meter module")},
67 static const char statusd_copy[]=
68 "Ion-statusd " ION_VERSION ", copyright (c) Tuomo Valkonen 2004-2005.";
71 static const char statusd_license[]=DUMMY_TR(
72 "This program is free software; you can redistribute it and/or\n"
73 "modify it under the terms of the GNU Lesser General Public\n"
74 "License as published by the Free Software Foundation; either\n"
75 "version 2.1 of the License, or (at your option) any later version.\n"
77 "This program is distributed in the hope that it will be useful,\n"
78 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
79 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
80 "Lesser General Public License for more details.\n");
83 /* new_informs=TRUE because we should always print period when
84 * initialisation is done.
86 static bool new_informs=TRUE;
87 static ExtlTab configtab;
92 printf(TR("Usage: %s [options]\n\n"), libtu_progname());
93 for(i=0; ion_opts[i].descr!=NULL; i++)
94 ion_opts[i].descr=TR(ion_opts[i].descr);
95 optparser_printhelp(OPTP_MIDLONG, ion_opts);
100 static void flush_informs()
110 static void mainloop()
114 sigemptyset(&trapset);
115 sigaddset(&trapset, SIGALRM);
116 sigaddset(&trapset, SIGCHLD);
117 mainloop_trap_signals(&trapset);
120 int kill_sig=mainloop_check_signals();
121 if(kill_sig!=0 && kill_sig!=SIGUSR1){
122 if(kill_sig==SIGTERM)
125 kill(getpid(), kill_sig);
128 mainloop_execute_deferred();
137 extern bool statusd_register_exports();
138 extern void statusd_unregister_exports();
141 static void stdout_closed(int fd, void *data)
147 int main(int argc, char*argv[])
149 const char *mod=NULL;
156 if(setlocale(LC_ALL, "")==NULL)
157 warn("setlocale() call failed.");
160 configtab=extl_table_none();
164 #ifdef CF_RELOCATABLE
165 prefix_set(argv[0], STATUSD_LOCATION);
170 if(!statusd_register_exports())
173 prefix_wrap_simple(extl_add_searchdir, EXTRABINDIR);
174 prefix_wrap_simple(extl_add_searchdir, MODULEDIR);
175 prefix_wrap_simple(extl_add_searchdir, ETCDIR);
176 prefix_wrap_simple(extl_add_searchdir, SHAREDIR);
177 prefix_wrap_simple(extl_add_searchdir, LCDIR);
178 extl_set_userdirs("ion3");
180 optparser_init(argc, argv, OPTP_MIDLONG, ion_opts);
182 extl_read_config("ioncore_luaext", NULL, TRUE);
184 while((opt=optparser_get_opt())){
187 display=optparser_get_arg();
190 extl_add_searchdir(optparser_get_arg());
193 extl_set_sessiondir(optparser_get_arg());
199 printf("%s\n", ION_VERSION);
202 printf("%s\n\n%s", statusd_copy, TR(statusd_license));
207 const char *f=optparser_get_arg();
208 if(extl_read_savefile(f, &t)){
209 extl_unref_table(configtab);
212 warn(TR("Unable to load configuration file %s"), f);
220 mod=optparser_get_arg();
221 if(strchr(mod, '/')==NULL && strchr(mod, '.')==NULL){
222 mod2=scat("statusd_", mod);
227 if(extl_read_config(mod, NULL, !quiet))
233 warn(TR("Invalid command line."));
239 if(loaded==0 && !quiet){
240 warn(TR("No meters loaded."));
251 * Inform that meter \var{name} has value \var{value}.
254 void statusd_inform(const char *name, const char *value)
256 printf("%s: %s\n", name, value);
262 * Get configuration table for module \var{name}
265 ExtlTab statusd_get_config(const char *name)
268 return extl_ref_table(configtab);
271 if(extl_table_gets_t(configtab, name, &t))
274 return extl_create_table();
280 * Get last file modification time.
283 double statusd_last_modified(const char *fname)
290 if(stat(fname, &st)!=0){
291 /*warn_err_obj(fname);*/
295 return (double)(st.st_mtime>st.st_ctime ? st.st_mtime : st.st_ctime);
300 ExtlTab statusd_getloadavg()
302 ExtlTab t=extl_create_table();
309 extl_table_sets_d(t, "1min", l[0]);
311 extl_table_sets_d(t, "5min", l[1]);
313 extl_table_sets_d(t, "15min", l[2]);