2 /* pndnotifyd - a daemon whose job is to monitor searchpaths for app changes (appearing, disappearing, changing).
3 * If a change is found, the discovery code is invoked and apps registered for the launchers to see
7 // TODO: Catch HUP and reparse config
8 // TODO: Should perhaps direct all printf's through a vsprintf handler to avoid redundant "if ! g_daemon_mode"
9 // TODO: During daemon mode, should perhaps syslog or log errors
11 #include <stdio.h> // for stdio
12 #include <unistd.h> // for exit()
13 #include <stdlib.h> // for exit()
14 #define __USE_GNU /* for strcasestr */
16 #include <time.h> // for time()
17 #include <ctype.h> // for isdigit()
18 #include <sys/types.h> // for umask
19 #include <sys/stat.h> // for umask
20 #include <dirent.h> // for opendir()
21 #include <signal.h> // for sigaction
24 #include "pnd_container.h"
26 #include "pnd_notify.h"
27 #include "../lib/pnd_pathiter.h"
28 #include "pnd_discovery.h"
29 #include "pnd_locate.h"
31 #include "pnd_utility.h"
32 #include "pnd_desktop.h"
33 #include "pnd_logger.h"
35 // this piece of code was simpler once; but need to grow it a bit and in a rush
36 // moving all these to globals rather than refactor the code a bit; tsk tsk..
38 // op mode; emitting stdout or no?
39 static unsigned char g_daemon_mode = 0;
43 pndn_rem, // will set default log level to here, so 'debug' is omitted
47 } pndnotify_loglevels_e;
53 char *searchpath = NULL;
54 char *notifypath = NULL;
55 time_t createtime = 0; // all 'new' .destops are created at or after this time; prev are old.
56 // dotfiles; this used to be a single pai .. now two pairs, a little unwieldy; pnd_box it up?
57 char *desktop_dotdesktoppath = NULL;
58 char *desktop_iconpath = NULL;
59 char *desktop_appspath = NULL;
60 char *menu_dotdesktoppath = NULL;
61 char *menu_iconpath = NULL;
62 char *menu_appspath = NULL;
64 char *run_searchpath; // searchpath to find pnd_run.sh
65 char *run_script; // name of pnd_run.sh script from config
66 char *pndrun; // full path to located pnd_run.sh
67 char *pndhup = NULL; // full path to located pnd_hup.sh
69 pnd_notify_handle nh = 0;
72 #define PNDNOTIFYD_LOGLEVEL "pndnotifyd.loglevel"
75 void consume_configuration ( void );
76 void setup_notifications ( void );
77 void sighup_handler ( int n );
78 void process_discoveries ( pnd_box_handle applist, char *emitdesktoppath, char *emiticonpath );
79 unsigned char perform_discoveries ( char *appspath, char *overridespath,
80 char *emitdesktoppath, char *emiticonpath );
82 int main ( int argc, char *argv[] ) {
84 unsigned char scanonlaunch = 1;
85 unsigned int interval_secs = 5;
89 /* iterate across args
91 for ( i = 1; i < argc; i++ ) {
93 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
94 //printf ( "Going daemon mode. Silent running.\n" );
96 } else if ( isdigit ( argv [ i ][ 0 ] ) ) {
97 interval_secs = atoi ( argv [ i ] );
98 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'n' ) {
101 printf ( "%s [-d] [##]\n", argv [ 0 ] );
102 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
103 printf ( "-n\tDo not scan on launch; default is to run a scan for apps when %s is invoked.\n", argv [ 0 ] );
104 printf ( "##\tA numeric value is interpreted as number of seconds between checking for filesystem changes. Default %u.\n",
106 printf ( "Signal: HUP the process to force reload of configuration and reset the notifier watch paths\n" );
114 if ( g_daemon_mode ) {
117 pnd_log_set_filter ( pndn_rem );
118 pnd_log_set_pretext ( "pndnotifyd" );
120 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
123 pnd_log ( pndn_rem, "Interval between checks is %u seconds\n", interval_secs );
125 // basic daemon set up
126 if ( g_daemon_mode ) {
128 // set a CWD somewhere else
133 // detach from terminal
134 if ( ( i = fork() ) < 0 ) {
135 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
139 exit ( 0 ); // exit parent
144 umask ( 022 ); // emitted files can be rwxr-xr-x
151 consume_configuration();
156 pnd_log ( pndn_rem, "PXML overrides searchpath is '%s'\n", overridespath );
157 pnd_log ( pndn_rem, "Notify searchpath is '%s'\n", notifypath );
159 pnd_log ( pndn_rem, "Desktop apps ---------------------------------\n" );
160 pnd_log ( pndn_rem, "Apps searchpath is '%s'\n", desktop_appspath );
161 pnd_log ( pndn_rem, ".desktop files emit to '%s'\n", desktop_dotdesktoppath );
162 pnd_log ( pndn_rem, ".desktop icon files emit to '%s'\n", desktop_iconpath );
164 pnd_log ( pndn_rem, "Menu apps ---------------------------------\n" );
165 pnd_log ( pndn_rem, "Apps searchpath is '%s'\n", menu_appspath );
166 pnd_log ( pndn_rem, ".desktop files emit to '%s'\n", menu_dotdesktoppath );
167 pnd_log ( pndn_rem, ".desktop icon files emit to '%s'\n", menu_iconpath );
169 /* set up signal handler
174 struct sigaction siggy;
175 siggy.sa_handler = sighup_handler;
176 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
177 siggy.sa_flags = 0; /* don't need anything */
179 sigaction ( SIGHUP, &siggy, NULL );
184 // if we're gong to scan on launch, it'll set things right up and then set up notifications.
185 // if no scan on launch, we need to set the notif's up now.
186 //if ( ! scanonlaunch ) {
187 setup_notifications();
194 // need to rediscover?
196 pnd_notify_rediscover_p ( nh ) )
198 createtime = time ( NULL ); // all 'new' .destops are created at or after this time; prev are old.
200 // if this was a forced scan, lets not do that next iteration
201 if ( scanonlaunch ) {
205 // by this point, the watched directories have notified us that something of relevent
206 // has occurred; we should be clever, but we're not, so just re-brute force the
207 // discovery and spit out .desktop files..
208 pnd_log ( pndn_rem, "------------------------------------------------------\n" );
209 pnd_log ( pndn_rem, "Changes within watched paths .. performing re-discover!\n" );
214 pnd_log ( pndn_rem, "Scanning desktop paths----------------------------\n" );
215 if ( ! perform_discoveries ( desktop_appspath, overridespath, desktop_dotdesktoppath, desktop_iconpath ) ) {
216 pnd_log ( pndn_rem, "No applications found in desktop search path\n" );
219 if ( menu_appspath && menu_dotdesktoppath && menu_iconpath ) {
220 pnd_log ( pndn_rem, "Scanning menu paths----------------------------\n" );
221 if ( ! perform_discoveries ( menu_appspath, overridespath, menu_dotdesktoppath, menu_iconpath ) ) {
222 pnd_log ( pndn_rem, "No applications found in menu search path\n" );
226 // if we've got a hup script located, lets invoke it
228 pnd_log ( pndn_rem, "Invoking hup script '%s'.\n", pndhup );
229 pnd_exec_no_wait_1 ( pndhup, NULL );
232 // since its entirely likely new directories have been found (ie: SD with a directory structure was inserted)
233 // we should re-apply watches to catch all these new directories; ie: user might use on-device browser to
234 // drop in new applications, or use the shell to juggle them around, or any number of activities.
235 //setup_notifications();
237 } // need to rediscover?
239 // lets not eat up all the CPU
240 // should use an alarm or select() or something -- I mean really, why aren't I putting interval_secs into
241 // the select() call above in pnd_notify_whatsitcalled()? -- but lets not break this right before release shall we
242 sleep ( interval_secs );
248 pnd_notify_shutdown ( nh );
253 void consume_configuration ( void ) {
255 // attempt to fetch a sensible default searchpath for configs
256 configpath = pnd_conf_query_searchpath();
258 // attempt to fetch the apps config to pick up a searchpath
259 pnd_conf_handle apph;
261 apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
265 overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
267 if ( ! overridespath ) {
268 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
271 notifypath = pnd_conf_get_as_char ( apph, PND_APPS_NOTIFY_KEY );
273 if ( ! notifypath ) {
274 notifypath = PND_APPS_NOTIFYPATH;
278 // couldn't find a useful app search path so use the default
279 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
280 notifypath = PND_APPS_NOTIFYPATH;
283 // attempt to figure out where to drop dotfiles .. now that we're going
284 // multi-target we see the limit of my rudimentary conf-file parser; should
285 // just parse to an array of targets, rather that hardcoding two, but
286 // on the other hand, don't likely see the need for more than two? (famous
288 pnd_conf_handle desktoph;
290 desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );
292 // for 'desktop' main applications
294 desktop_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_DOTDESKTOP_PATH_KEY );
295 desktop_iconpath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_ICONS_PATH_KEY );
296 desktop_appspath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_SEARCH_KEY );
299 if ( ! desktop_dotdesktoppath ) {
300 desktop_dotdesktoppath = PND_DESKTOP_DOTDESKTOP_PATH_DEFAULT;
303 if ( ! desktop_iconpath ) {
304 desktop_iconpath = PND_DESKTOP_ICONS_PATH_DEFAULT;
307 if ( ! desktop_appspath ) {
308 desktop_appspath = PND_DESKTOP_SEARCH_PATH_DEFAULT;
311 // for 'menu' applications
313 menu_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_MENU_DOTDESKTOP_PATH_KEY );
314 menu_iconpath = pnd_conf_get_as_char ( desktoph, PND_MENU_ICONS_PATH_KEY );
315 menu_appspath = pnd_conf_get_as_char ( desktoph, PND_MENU_SEARCH_KEY );
318 /* try to locate a runscript and optional hupscript
322 run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
323 run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
326 if ( ! run_searchpath ) {
327 run_searchpath = PND_APPS_SEARCHPATH;
328 run_script = PND_PNDRUN_FILENAME;
331 if ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) != PND_CONF_BADNUM ) {
332 pnd_log_set_filter ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) );
333 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
337 run_searchpath = NULL;
339 pndrun = PND_PNDRUN_DEFAULT;
343 pndrun = pnd_locate_filename ( run_searchpath, run_script );
346 pndrun = strdup ( pndrun ); // so we don't just use the built in buffer; next locate will overwrite it
348 pndrun = PND_PNDRUN_DEFAULT;
353 if ( desktoph && run_searchpath ) {
356 if ( ( t = pnd_conf_get_as_char ( desktoph, PND_PNDHUP_KEY ) ) ) {
357 pndhup = pnd_locate_filename ( run_searchpath, t );
360 pndhup = strdup ( pndhup ); // so we don't just use the built in buffer; next locate will overwrite it
363 #if 0 // don't enable this; if no key in config, we don't want to bother hupping
365 pndhup = pnd_locate_filename ( run_searchpath, PND_PNDHUP_FILENAME );
372 if ( run_searchpath ) pnd_log ( pndn_rem, "Locating pnd run in %s\n", run_searchpath );
373 if ( run_script ) pnd_log ( pndn_rem, "Locating pnd runscript as %s\n", run_script );
374 if ( pndrun ) pnd_log ( pndn_rem, "pndrun is %s\n", pndrun );
376 pnd_log ( pndn_rem, "pndhup is %s\n", pndhup );
378 pnd_log ( pndn_rem, "No pndhup found (which is fine.)\n" );
381 /* handle globbing or variable substitution
383 desktop_dotdesktoppath = pnd_expand_tilde ( strdup ( desktop_dotdesktoppath ) );
384 desktop_iconpath = pnd_expand_tilde ( strdup ( desktop_iconpath ) );
385 mkdir ( desktop_dotdesktoppath, 0777 );
386 mkdir ( desktop_iconpath, 0777 );
388 if ( menu_dotdesktoppath ) {
389 menu_dotdesktoppath = pnd_expand_tilde ( strdup ( menu_dotdesktoppath ) );
390 mkdir ( menu_dotdesktoppath, 0777 );
392 if ( menu_iconpath ) {
393 menu_iconpath = pnd_expand_tilde ( strdup ( menu_iconpath ) );
394 mkdir ( menu_iconpath, 0777 );
401 void setup_notifications ( void ) {
402 searchpath = notifypath;
404 // if this is first time through, we can just set it up; for subsequent times
405 // through, we need to close existing fd and re-open it, since we're too lame
406 // to store the list of watches and 'rm' them
409 pnd_notify_shutdown ( nh );
414 // set up a new set of notifies
416 nh = pnd_notify_init();
420 pnd_log ( pndn_rem, "INOTIFY failed to init.\n" );
425 pnd_log ( pndn_rem, "INOTIFY is up.\n" );
431 pnd_log ( pndn_rem, "Watching path '%s' and its descendents.\n", buffer );
432 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
440 void sighup_handler ( int n ) {
442 pnd_log ( pndn_rem, "---[ SIGHUP received ]---\n" );
444 // reparse config files
445 consume_configuration();
447 // re set up the notifier watches
448 setup_notifications();
453 // This very recently was inline code; just slight refactor to functionize it so that it can be
454 // reused in a couple of places. Simple code with simple design quickly became too large for
455 // its simple design; should revisit a lot of these little things..
456 void process_discoveries ( pnd_box_handle applist, char *emitdesktoppath, char *emiticonpath ) {
457 pnd_disco_t *d = pnd_box_get_head ( applist );
461 pnd_log ( pndn_rem, "Found app: %s\n", pnd_box_get_key ( d ) );
463 // check if icon already exists (from a previous extraction say); if so, we needn't
465 char existingpath [ FILENAME_MAX ];
466 sprintf ( existingpath, "%s/%s.png", emiticonpath, d -> unique_id );
469 if ( stat ( existingpath, &dirs ) == 0 ) {
470 // icon seems to exist, so just crib the location into the .desktop
472 pnd_log ( pndn_rem, " Found icon already existed, so reusing it! %s\n", existingpath );
477 d -> icon = strdup ( existingpath );
480 // icon seems unreadable or does not exist; lets try to create it..
482 pnd_log ( pndn_debug, " Icon not already present, so trying to write it! %s\n", existingpath );
484 // attempt to create icon files; if successful, alter the disco struct to contain new
485 // path, otherwise leave it alone (since it could be a generic icon reference..)
486 if ( pnd_emit_icon ( emiticonpath, d ) ) {
487 // success; fix up icon path to new one..
491 d -> icon = strdup ( existingpath );
493 pnd_log ( pndn_debug, " WARN: Couldn't write out icon %s\n", existingpath );
496 } // icon already exists?
498 // create the .desktop file
499 if ( pnd_emit_dotdesktop ( emitdesktoppath, pndrun, d ) ) {
500 // add a watch onto the newly created .desktop?
502 char buffer [ FILENAME_MAX ];
503 sprintf ( buffer, "%s/%s", emitdesktoppath, d -> unique_id );
504 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
507 pnd_log ( pndn_rem, "ERROR: Error creating .desktop file for app: %s\n", pnd_box_get_key ( d ) );
511 d = pnd_box_get_next ( d );
518 // returns true if any applications were found
519 unsigned char perform_discoveries ( char *appspath, char *overridespath, // args to do discovery
520 char *emitdesktoppath, char *emiticonpath ) // args to do emitting
522 pnd_box_handle applist;
524 // attempt to auto-discover applications in the given path
525 applist = pnd_disco_search ( appspath, overridespath );
531 process_discoveries ( applist, emitdesktoppath, emiticonpath );
533 // run a clean up, to remove any dotdesktop files that we didn't
534 // just now create (that seem to have been created by pndnotifyd
535 // previously.) This allows SD eject (or .pnd remove) to remove
536 // an app from the launcher
537 // NOTE: Could opendir and iterate across all .desktop files,
538 // removing any that have Source= something else, and that the
539 // app name is not in the list found in applist box above. But
540 // a cheesy simple way right now is to just remove .desktop files
541 // that have a last mod time prior to the time we stored above.
545 if ( ( dir = opendir ( emitdesktoppath ) ) ) {
546 struct dirent *dirent;
548 char buffer [ FILENAME_MAX ];
550 while ( ( dirent = readdir ( dir ) ) ) {
552 // file is a .desktop?
553 if ( strstr ( dirent -> d_name, ".desktop" ) == NULL ) {
557 // figure out full path
558 sprintf ( buffer, "%s/%s", emitdesktoppath, dirent -> d_name );
560 // file was previously created by libpnd; check Source= line
561 // logic: default to 'yes' (in case we can't open the file for some reason)
562 // if we can open the file, default to no and look for the source flag we added; if
563 // that matches then we know its libpnd created, otherwise assume not.
564 unsigned char source_libpnd = 1;
567 FILE *grep = fopen ( buffer, "r" );
570 while ( fgets ( line, 255, grep ) ) {
571 if ( strcasestr ( line, PND_DOTDESKTOP_SOURCE ) ) {
578 if ( source_libpnd ) {
581 "File '%s' appears to have been created by libpnd so candidate for delete: %u\n", buffer, source_libpnd );
585 pnd_log ( pndn_rem, "File '%s' appears NOT to have been created by libpnd, so leave it alone\n", buffer );
587 continue; // skip deleting it
591 if ( stat ( buffer, &dirs ) == 0 ) {
592 if ( dirs.st_mtime >= createtime ) {
594 pnd_log ( pndn_rem, "File '%s' seems 'new', so leave it alone.\n", buffer );
596 continue; // skip deleting it
600 // by this point, the .desktop file must be 'old' and created by pndnotifyd
601 // previously, so can remove it
602 pnd_log ( pndn_rem, "File '%s' seems nolonger relevent; removing it.\n", dirent -> d_name );
605 } // while getting filenames from dir
610 } // purge old .desktop files
612 //WARN: MEMORY LEAK HERE
613 pnd_log ( pndn_debug, "pndnotifyd - memory leak here - perform_discoveries()\n" );
614 pnd_box_delete ( applist ); // does not free the disco_t contents!