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 #include <stdio.h> // for stdio
8 #include <unistd.h> // for exit()
9 #include <stdlib.h> // for exit()
10 #define __USE_GNU /* for strcasestr */
12 #include <time.h> // for time()
13 #include <ctype.h> // for isdigit()
14 #include <sys/types.h> // for umask
15 #include <sys/stat.h> // for umask
16 #include <dirent.h> // for opendir()
17 #include <signal.h> // for sigaction
18 #include <errno.h> // for errno
21 #include "pnd_container.h"
23 #include "pnd_notify.h"
24 #include "../lib/pnd_pathiter.h"
25 #include "pnd_discovery.h"
26 #include "pnd_locate.h"
28 #include "pnd_utility.h"
29 #include "pnd_desktop.h"
30 #include "pnd_logger.h"
31 #include "pnd_dbusnotify.h"
32 #include "pnd_pndfiles.h"
34 // this piece of code was simpler once; but need to grow it a bit and in a rush
35 // moving all these to globals rather than refactor the code a bit; tsk tsk..
37 // op mode; emitting stdout or no?
38 static unsigned char g_daemon_mode = 0;
42 pndn_rem, // will set default log level to here, so 'debug' is omitted
46 } pndnotify_loglevels_e;
52 char *searchpath = NULL;
53 char *notifypath = NULL;
54 time_t createtime = 0; // all 'new' .destops are created at or after this time; prev are old.
55 // dotfiles; this used to be a single pai .. now two pairs, a little unwieldy; pnd_box it up?
56 char *desktop_dotdesktoppath = NULL;
57 char *desktop_iconpath = NULL;
58 char *desktop_appspath = NULL;
59 char *menu_dotdesktoppath = NULL;
60 char *menu_iconpath = NULL;
61 char *menu_appspath = NULL;
62 char *info_dotdesktoppath = 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 char g_username [ 128 ]; // since we have to wait for login (!!), store username here
71 pnd_notify_handle nh = 0;
72 pnd_dbusnotify_handle dbh = 0;
73 unsigned char g_info_p = 0; // spit out info .desktops
76 #define PNDNOTIFYD_LOGLEVEL "pndnotifyd.loglevel"
77 #define PNDLOCKNAME "pndnotifyd-disco.lock"
80 void consume_configuration ( void );
81 void setup_notifications ( void );
82 void sigint_handler ( int n );
83 void sighup_handler ( int n );
84 void process_discoveries ( pnd_box_handle applist, char *emitdesktoppath, char *emiticonpath );
85 unsigned char perform_discoveries ( char *appspath, char *overridespath,
86 char *emitdesktoppath, char *emiticonpath );
88 int main ( int argc, char *argv[] ) {
90 unsigned char scanonlaunch = 1;
91 unsigned int interval_secs = 5;
92 int logall = -1; // -1 means normal logging rules; >=0 means log all!
96 /* iterate across args
98 for ( i = 1; i < argc; i++ ) {
100 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
101 //printf ( "Going daemon mode. Silent running.\n" );
103 } else if ( isdigit ( argv [ i ][ 0 ] ) ) {
104 interval_secs = atoi ( argv [ i ] );
105 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'n' ) {
107 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
109 if ( isdigit ( argv [ i ][ 2 ] ) ) {
110 unsigned char x = atoi ( argv [ i ] + 2 );
121 printf ( "%s [-d] [-l] [##]\n", argv [ 0 ] );
122 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
123 printf ( "-n\tDo not scan on launch; default is to run a scan for apps when %s is invoked.\n", argv [ 0 ] );
124 printf ( "-l#\tLog-it; -l is 0-and-up (or all), and -l2 means 2-and-up (not all); l[0-3] for now. Log goes to /tmp/pndnotifyd.log\n" );
125 printf ( "##\tA numeric value is interpreted as number of seconds between checking for filesystem changes. Default %u.\n",
127 printf ( "Signal: HUP the process to force reload of configuration and reset the notifier watch paths\n" );
135 pnd_log_set_pretext ( "pndnotifyd" );
136 pnd_log_set_flush ( 1 );
138 if ( logall == -1 ) {
139 // standard logging; non-daemon versus daemon
141 if ( g_daemon_mode ) {
144 pnd_log_set_filter ( pndn_rem );
151 f = fopen ( "/tmp/pndnotifyd.log", "w" );
154 pnd_log_set_filter ( logall );
155 pnd_log_to_stream ( f );
156 pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndnotifyd.log\n" );
159 if ( logall == pndn_debug ) {
160 pnd_log_set_buried_logging ( 1 ); // log the shit out of it
161 pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
166 pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
168 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
170 pnd_log ( pndn_rem, "Interval between checks is %u seconds\n", interval_secs );
172 // check if inotify is awake yet; if not, try waiting for awhile to see if it does
173 pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );
175 if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
176 pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
180 pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );
182 // basic daemon set up
183 if ( g_daemon_mode ) {
185 // set a CWD somewhere else
190 // detach from terminal
191 if ( ( i = fork() ) < 0 ) {
192 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
196 exit ( 0 ); // exit parent
201 umask ( 022 ); // emitted files can be rwxr-xr-x
205 // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
206 // log-out and back in again, with SDs popping in and out between..
207 pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
209 if ( pnd_check_login ( g_username, 127 ) ) {
212 pnd_log ( pndn_debug, " No one logged in yet .. spinning.\n" );
215 pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", g_username );
220 consume_configuration();
225 pnd_log ( pndn_rem, "PXML overrides searchpath is '%s'\n", overridespath );
226 pnd_log ( pndn_rem, "Notify searchpath is '%s'\n", notifypath );
228 pnd_log ( pndn_rem, "Desktop apps ---------------------------------\n" );
229 pnd_log ( pndn_rem, "Apps searchpath is '%s'\n", desktop_appspath );
230 pnd_log ( pndn_rem, ".desktop files emit to '%s'\n", desktop_dotdesktoppath );
231 pnd_log ( pndn_rem, ".desktop icon files emit to '%s'\n", desktop_iconpath );
233 pnd_log ( pndn_rem, "Menu apps ---------------------------------\n" );
234 pnd_log ( pndn_rem, "Apps searchpath is '%s'\n", menu_appspath );
235 pnd_log ( pndn_rem, ".desktop files emit to '%s'\n", menu_dotdesktoppath );
236 pnd_log ( pndn_rem, ".desktop icon files emit to '%s'\n", menu_iconpath );
237 pnd_log ( pndn_rem, ".desktop info files emit to '%s'\n", info_dotdesktoppath ? info_dotdesktoppath : "n/a" );
239 /* set up signal handler
244 struct sigaction siggy;
245 siggy.sa_handler = sighup_handler;
246 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
247 siggy.sa_flags = 0; /* don't need anything */
249 sigaction ( SIGHUP, &siggy, NULL );
251 siggy.sa_handler = sigint_handler;
252 sigaction ( SIGINT, &siggy, NULL );
257 // if we're gong to scan on launch, it'll set things right up and then set up notifications.
258 // if no scan on launch, we need to set the notif's up now.
259 //if ( ! scanonlaunch ) {
260 setup_notifications();
263 dbh = pnd_dbusnotify_init();
267 unsigned char watch_inotify = 0, watch_dbus = 0;
268 unsigned char idle = 0;
272 // collect all events to avoid multiple PND rescans
274 struct timeval timeout_1s = { 1, 0};
275 int fd_inotify = -1, fd_dbus = -1;
276 struct timeval *timeout = NULL;
281 fd_dbus = pnd_dbusnotify_fd ( dbh );
283 fd_inotify = pnd_notify_fd ( nh );
288 if ( fd_dbus != -1 ) {
289 FD_SET ( fd_dbus, &rfds );
290 FD_SET ( fd_dbus, &efds );
291 if ( fd_dbus > fd_max )
295 if ( fd_inotify != -1 ) {
296 FD_SET ( fd_inotify, &rfds );
297 FD_SET ( fd_inotify, &efds );
298 if ( fd_inotify > fd_max )
302 if ( fd_max == -1 ) {
303 pnd_log ( pndn_error, "ERROR: notify: nothing to watch\n" );
308 // the idle timeout is needed for inotify as some file updates produce several events
309 // with a delay in between (length depends on things like file size and system load), like
310 // - IN_CREATE, IN_CLOSE_WRITE (cp, mv from other fs, etc)
311 // - IN_MOVED_TO, IN_CLOSE_WRITE (PNDManager)
312 // - multiple file copies, deletes, etc
313 timeout = idle ? NULL : &timeout_1s;
315 retcode = select ( fd_max + 1, &rfds, NULL, &efds, timeout );
318 pnd_log ( pndn_error, "ERROR: notify: select failed: %d\n", errno );
319 if ( errno == EINTR )
324 idle = ( retcode == 0 );
326 if ( dbh && FD_ISSET ( fd_dbus, &efds ) ) {
327 pnd_log ( pndn_error, "ERROR: notify: dbus fd error\n" );
328 pnd_dbusnotify_shutdown ( dbh );
332 if ( nh && FD_ISSET ( fd_inotify, &efds ) ) {
333 pnd_log ( pndn_error, "ERROR: notify: inotify fd error\n" );
334 pnd_notify_shutdown ( nh );
338 if ( dbh && FD_ISSET ( fd_dbus, &rfds ) ) {
339 watch_dbus |= pnd_dbusnotify_rediscover_p ( dbh );
340 if ( watch_dbus && nh ) {
341 // will restart inotify (see below), drop all events
342 pnd_notify_shutdown ( nh );
347 if ( nh && FD_ISSET ( fd_inotify, &rfds ) ) {
348 watch_inotify |= pnd_notify_rediscover_p ( nh );
357 // need to rediscover?
358 if ( scanonlaunch || watch_inotify || watch_dbus ) {
360 // by this point, the watched directories have notified us that something of relevent
361 // has occurred; we should be clever, but we're not, so just re-brute force the
362 // discovery and spit out .desktop files..
363 pnd_log ( pndn_rem, "------------------------------------------------------\n" );
365 pnd_log ( pndn_rem, "System changes detected in dbus or watched paths .. performing re-discover!\n" );
367 // if this was a forced scan, lets not do that next iteration
368 if ( scanonlaunch ) {
369 pnd_log ( pndn_rem, "scan-on-first-launch detected an event\n" );
373 if ( watch_inotify ) {
374 pnd_log ( pndn_rem, "inotify detected an event\n" );
378 pnd_log ( pndn_rem, "dbusnotify detected an event\n" );
381 if ( time ( NULL ) - createtime < interval_secs ) {
382 pnd_log ( pndn_rem, "Rediscovery request comes to soon after previous discovery; delaying.\n" );
383 sleep ( interval_secs );
388 pnd_log ( pndn_rem, "------------------------------------------------------\n" );
390 createtime = time ( NULL ); // all 'new' .destops are created at or after this time; prev are old.
396 pnd_log ( pndn_rem, "creating lockfile %s", PNDLOCKNAME );
397 if ( ! pnd_lock ( PNDLOCKNAME ) ) {
398 // problem .. well, too bad, we need to do this .. proceed!
402 pnd_log ( pndn_rem, " Scanning desktop paths----------------------------\n" );
403 if ( ! perform_discoveries ( desktop_appspath, overridespath, desktop_dotdesktoppath, desktop_iconpath ) ) {
404 pnd_log ( pndn_rem, " No applications found in desktop search path\n" );
407 if ( menu_appspath && menu_dotdesktoppath && menu_iconpath ) {
408 pnd_log ( pndn_rem, " Scanning menu paths----------------------------\n" );
409 if ( ! perform_discoveries ( menu_appspath, overridespath, menu_dotdesktoppath, menu_iconpath ) ) {
410 pnd_log ( pndn_rem, " No applications found in menu search path\n" );
415 pnd_log ( pndn_rem, "clearing lockfile %s", PNDLOCKNAME );
416 pnd_unlock ( PNDLOCKNAME );
418 // if we've got a hup script located, lets invoke it
420 pnd_log ( pndn_rem, "Invoking hup script '%s'.\n", pndhup );
421 pnd_exec_no_wait_1 ( pndhup, NULL );
424 // since its entirely likely new directories have been found (ie: SD with a directory structure was inserted)
425 // we should re-apply watches to catch all these new directories; ie: user might use on-device browser to
426 // drop in new applications, or use the shell to juggle them around, or any number of activities.
428 setup_notifications();
431 watch_inotify = watch_dbus = 0;
433 } // need to rediscover?
439 pnd_notify_shutdown ( nh );
440 pnd_dbusnotify_shutdown ( dbh );
445 void consume_configuration ( void ) {
447 // attempt to fetch a sensible default searchpath for configs
448 configpath = pnd_conf_query_searchpath();
450 // attempt to fetch the apps config to pick up a searchpath
451 pnd_conf_handle apph;
453 apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
457 overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
459 if ( ! overridespath ) {
460 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
463 notifypath = pnd_conf_get_as_char ( apph, PND_APPS_NOTIFY_KEY );
465 if ( ! notifypath ) {
466 notifypath = PND_APPS_NOTIFYPATH;
470 // couldn't find a useful app search path so use the default
471 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
472 notifypath = PND_APPS_NOTIFYPATH;
475 // attempt to figure out where to drop dotfiles .. now that we're going
476 // multi-target we see the limit of my rudimentary conf-file parser; should
477 // just parse to an array of targets, rather that hardcoding two, but
478 // on the other hand, don't likely see the need for more than two? (famous
480 pnd_conf_handle desktoph;
482 desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );
484 // for 'desktop' main applications
486 desktop_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_DOTDESKTOP_PATH_KEY );
487 desktop_iconpath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_ICONS_PATH_KEY );
488 desktop_appspath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_SEARCH_KEY );
489 info_dotdesktoppath = pnd_conf_get_as_char ( desktoph, "info.dotdesktoppath" );
492 if ( ! desktop_dotdesktoppath ) {
493 desktop_dotdesktoppath = PND_DESKTOP_DOTDESKTOP_PATH_DEFAULT;
496 if ( ! desktop_iconpath ) {
497 desktop_iconpath = PND_DESKTOP_ICONS_PATH_DEFAULT;
500 if ( ! desktop_appspath ) {
501 desktop_appspath = PND_DESKTOP_SEARCH_PATH_DEFAULT;
504 // for 'menu' applications
506 menu_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_MENU_DOTDESKTOP_PATH_KEY );
507 menu_iconpath = pnd_conf_get_as_char ( desktoph, PND_MENU_ICONS_PATH_KEY );
508 menu_appspath = pnd_conf_get_as_char ( desktoph, PND_MENU_SEARCH_KEY );
513 g_info_p = pnd_conf_get_as_int_d ( desktoph, "info.emit_info", 0 );
516 /* try to locate a runscript and optional hupscript
520 run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
521 run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
524 if ( ! run_searchpath ) {
525 run_searchpath = PND_APPS_SEARCHPATH;
526 run_script = PND_PNDRUN_FILENAME;
529 if ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) != PND_CONF_BADNUM ) {
530 if ( pnd_log_do_buried_logging() == 0 ) {
531 pnd_log_set_filter ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) );
532 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
534 pnd_log ( pndn_rem, "-l command line suppresses log level change in config file\n" );
539 run_searchpath = NULL;
541 pndrun = PND_PNDRUN_DEFAULT;
545 pndrun = pnd_locate_filename ( run_searchpath, run_script );
548 pndrun = strdup ( pndrun ); // so we don't just use the built in buffer; next locate will overwrite it
550 pndrun = PND_PNDRUN_DEFAULT;
555 if ( desktoph && run_searchpath ) {
558 if ( ( t = pnd_conf_get_as_char ( desktoph, PND_PNDHUP_KEY ) ) ) {
559 pndhup = pnd_locate_filename ( run_searchpath, t );
562 pndhup = strdup ( pndhup ); // so we don't just use the built in buffer; next locate will overwrite it
565 #if 0 // don't enable this; if no key in config, we don't want to bother hupping
567 pndhup = pnd_locate_filename ( run_searchpath, PND_PNDHUP_FILENAME );
574 if ( run_searchpath ) pnd_log ( pndn_rem, "Locating pnd run in %s\n", run_searchpath );
575 if ( run_script ) pnd_log ( pndn_rem, "Locating pnd runscript as %s\n", run_script );
576 if ( pndrun ) pnd_log ( pndn_rem, "pndrun is %s\n", pndrun );
578 pnd_log ( pndn_rem, "pndhup is %s\n", pndhup );
580 pnd_log ( pndn_rem, "No pndhup found (which is fine.)\n" );
583 /* cheap hack, maybe it'll help when pndnotifyd is coming up before the rest of the system and before
584 * the user is formally logged in
586 pnd_log ( pndn_rem, "Setting a default $HOME to non-root user\n" );
588 // first, try to see if known-username maps to a homedir; if so, just use that!
589 // otherwise, pick first non-root homedir and assume .. or we start blaring .desktops
590 // out to all users like idiots
591 unsigned char got_user_homedir = 0;
593 if ( g_username [ 0 ] ) {
595 char path [ PATH_MAX ];
597 sprintf ( path, "/home/%s", g_username );
599 // does this made up path exist?
600 if ( stat ( path, &homedir ) == 0 ) {
603 if ( S_ISDIR(homedir.st_mode) ) {
604 pnd_log ( pndn_rem, " User [%s] matches path [%s], going with '%s'\n", g_username, path, path );
605 setenv ( "HOME", path, 1 /* overwrite */ );
606 got_user_homedir = 1;
609 } // guessing a homedirname..
613 // if guessing a path was no good, just try finding one
614 if ( got_user_homedir == 0 ) {
617 if ( ( dir = opendir ( "/home" ) ) ) {
618 struct dirent *dirent;
620 while ( ( dirent = readdir ( dir ) ) ) {
621 pnd_log ( pndn_rem, " Scanning user homedir '%s'\n", dirent -> d_name );
623 // file is a .desktop?
624 if ( dirent -> d_name [ 0 ] == '.' ) {
626 } else if ( strcmp ( dirent -> d_name, "root" ) == 0 ) {
630 // a non-root user is found
632 sprintf ( buffer, "/home/%s", dirent -> d_name );
633 pnd_log ( pndn_rem, " Going with '%s'\n", buffer );
634 setenv ( "HOME", buffer, 1 /* overwrite */ );
637 } // while iterating through dir listing
644 /* handle globbing or variable substitution
646 desktop_dotdesktoppath = pnd_expand_tilde ( strdup ( desktop_dotdesktoppath ) );
647 desktop_iconpath = pnd_expand_tilde ( strdup ( desktop_iconpath ) );
648 mkdir ( desktop_dotdesktoppath, 0777 );
649 mkdir ( desktop_iconpath, 0777 );
651 if ( info_dotdesktoppath ) {
652 info_dotdesktoppath = pnd_expand_tilde ( strdup ( info_dotdesktoppath ) );
653 mkdir ( info_dotdesktoppath, 0777 );
656 if ( menu_dotdesktoppath ) {
657 menu_dotdesktoppath = pnd_expand_tilde ( strdup ( menu_dotdesktoppath ) );
658 mkdir ( menu_dotdesktoppath, 0777 );
660 if ( menu_iconpath ) {
661 menu_iconpath = pnd_expand_tilde ( strdup ( menu_iconpath ) );
662 mkdir ( menu_iconpath, 0777 );
669 void setup_notifications ( void ) {
670 searchpath = notifypath;
672 // if this is first time through, we can just set it up; for subsequent times
673 // through, we need to close existing fd and re-open it, since we're too lame
674 // to store the list of watches and 'rm' them
677 pnd_notify_shutdown ( nh );
682 // set up a new set of notifies
684 nh = pnd_notify_init();
688 pnd_log ( pndn_rem, "INOTIFY failed to init.\n" );
693 pnd_log ( pndn_rem, "INOTIFY is up.\n" );
699 pnd_log ( pndn_rem, "Watching path '%s' and its descendents.\n", buffer );
700 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
702 //pnd_notify_watch_path ( nh, buffer, 0 /* no recurse */ );
708 sleep ( 1 ); // wait for events to trigger?
710 // clear out any notifies we just created
711 while ( pnd_notify_rediscover_p ( nh ) ) {
712 usleep ( 100 ); // spin
719 void sighup_handler ( int n ) {
721 pnd_log ( pndn_rem, "---[ SIGHUP received ]---\n" );
723 // reparse config files
724 consume_configuration();
726 // re set up the notifier watches
727 setup_notifications();
732 void sigint_handler ( int n ) {
734 pnd_log ( pndn_rem, "---[ SIGINT received ]---\n" );
737 pnd_dbusnotify_shutdown ( dbh );
741 pnd_notify_shutdown ( nh );
747 // This very recently was inline code; just slight refactor to functionize it so that it can be
748 // reused in a couple of places. Simple code with simple design quickly became too large for
749 // its simple design; should revisit a lot of these little things..
750 void process_discoveries ( pnd_box_handle applist, char *emitdesktoppath, char *emiticonpath ) {
751 pnd_disco_t *d = pnd_box_get_head ( applist );
755 pnd_log ( pndn_rem, "Found app: %s\n", pnd_box_get_key ( d ) );
757 // check if icon already exists (from a previous extraction say); if so, we needn't
759 char existingpath [ FILENAME_MAX ];
760 sprintf ( existingpath, "%s/%s.png", emiticonpath, d -> unique_id /*, d -> subapp_number */ );
763 if ( stat ( existingpath, &dirs ) == 0 ) {
764 // icon seems to exist, so just crib the location into the .desktop
766 pnd_log ( pndn_rem, " Found icon already existed, so reusing it! %s\n", existingpath );
771 d -> icon = strdup ( existingpath );
774 // icon seems unreadable or does not exist; lets try to create it..
776 pnd_log ( pndn_debug, " Icon not already present, so trying to write it! %s\n", existingpath );
778 // handle same-path icon override for davec :)
779 char ovrfile [ PATH_MAX ];
781 sprintf ( ovrfile, "%s/%s", d -> object_path, d -> object_filename );
782 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
784 strcpy ( fixpxml, ".png" );
787 if ( stat ( ovrfile, &statbuf ) == 0 ) {
788 d -> icon = strdup ( ovrfile );
789 fixpxml = ovrfile; // !NULL will be the trigger to skip emittinf desktop from .pnd
793 // attempt to create icon files; if successful, alter the disco struct to contain new
794 // path, otherwise leave it alone (since it could be a generic icon reference..)
795 if ( fixpxml == NULL ) {
796 // don't have an same-path override icon, so go fetch something from pnd file
798 if ( pnd_emit_icon ( emiticonpath, d ) ) {
799 // success; fix up icon path to new one..
803 d -> icon = strdup ( existingpath );
805 pnd_log ( pndn_debug, " WARN: Couldn't write out icon %s\n", existingpath );
808 } // got ovr icon already?
810 } // icon already exists?
812 // create the .desktop file
813 if ( pnd_emit_dotdesktop ( emitdesktoppath, pndrun, d ) ) {
814 // add a watch onto the newly created .desktop?
816 char buffer [ FILENAME_MAX ];
817 sprintf ( buffer, "%s/%s", emitdesktoppath, d -> unique_id );
818 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
821 pnd_log ( pndn_rem, "ERROR: Error creating .desktop file for app: %s\n", pnd_box_get_key ( d ) );
825 if ( g_info_p && info_dotdesktoppath ) {
826 if ( ! pnd_emit_dotinfo ( info_dotdesktoppath, pndrun, d ) ) {
827 pnd_log ( pndn_rem, "ERROR: Error creating info .desktop file for app: %s\n", pnd_box_get_key ( d ) );
831 // does this object request any mkdir's?
832 if ( d -> mkdir_sp ) {
834 // it would appear it does! but we have to carefully validate these suckers
835 pnd_log ( pndn_rem, " App %s requests mkdir: %s\n", d -> object_path, d -> mkdir_sp );
837 // for each mkdir requested path, do it...
838 char *searchpath = d -> mkdir_sp;
842 /* "buffer" now holds each chunk of the searchpath, expanded */
844 // WARN: This whole concept could be flawed; what if they represent '..' in some other obscure way (unicode?)
845 // and we end up allowing mkdir's all over the place? The risk really is limited -- once the pnd is here,
846 // if the user _runs it_, it can go nuts, so creating a few dirs isn't all that dangerous...
848 // Perhaps I should have a config setting for pndnotifyd to suppress this whole mkdir behaviour?
850 // if not containing ".." we allow it
851 if ( strstr ( buffer, ".." ) == NULL ) {
853 // determine mountpoint for the file
854 // - we could deduce this from the path (somewhat risky if we assume leading /media/mmcblk1p1 type notation .. could
855 // be other distributions entirely
856 // - better to scan through mount-list and figure it out.. *sucks*
857 char mountpoint [ PATH_MAX ];
858 if ( pnd_determine_mountpoint ( d -> object_path, mountpoint, PATH_MAX - strlen ( buffer ) - 1 ) == 1 ) {
860 strcat ( mountpoint, "/" );
861 strcat ( mountpoint, buffer );
864 if ( stat ( mountpoint, &t ) == 0 ) {
865 pnd_log ( pndn_rem, " Skipping existing mkdir: %s\n", mountpoint );
867 pnd_log ( pndn_rem, " Attempting create of non-existant path: %s\n", mountpoint );
868 mkdir ( mountpoint, 0777 );
871 } // if figured out the mountpoint
881 d = pnd_box_get_next ( d );
888 // returns true if any applications were found
889 unsigned char perform_discoveries ( char *appspath, char *overridespath, // args to do discovery
890 char *emitdesktoppath, char *emiticonpath ) // args to do emitting
892 pnd_box_handle applist;
894 pnd_log ( pndn_rem, "perform discovery - apps: %s, overrides: %s\n", appspath, overridespath );
895 pnd_log ( pndn_rem, " - emit desktop: %s, icons: %s\n", emitdesktoppath, emiticonpath );
897 // attempt to auto-discover applications in the given path
898 applist = pnd_disco_search ( appspath, overridespath );
901 process_discoveries ( applist, emitdesktoppath, emiticonpath );
904 // run a clean up, to remove any dotdesktop files that we didn't
905 // just now create (that seem to have been created by pndnotifyd
906 // previously.) This allows SD eject (or .pnd remove) to remove
907 // an app from the launcher
908 // NOTE: Could opendir and iterate across all .desktop files,
909 // removing any that have Source= something else, and that the
910 // app name is not in the list found in applist box above. But
911 // a cheesy simple way right now is to just remove .desktop files
912 // that have a last mod time prior to the time we stored above.
916 if ( ( dir = opendir ( emitdesktoppath ) ) ) {
917 struct dirent *dirent;
919 char buffer [ FILENAME_MAX ];
921 while ( ( dirent = readdir ( dir ) ) ) {
923 // file is a .desktop?
924 if ( strstr ( dirent -> d_name, ".desktop" ) == NULL ) {
928 // figure out full path
929 sprintf ( buffer, "%s/%s", emitdesktoppath, dirent -> d_name );
931 // file was previously created by libpnd; check Source= line
932 // logic: default to 'yes' (in case we can't open the file for some reason)
933 // if we can open the file, default to no and look for the source flag we added; if
934 // that matches then we know its libpnd created, otherwise assume not.
935 unsigned char source_libpnd = 1;
938 FILE *grep = fopen ( buffer, "r" );
941 while ( fgets ( line, 255, grep ) ) {
942 if ( strcasestr ( line, PND_DOTDESKTOP_SOURCE ) ) {
949 if ( source_libpnd ) {
951 pnd_log ( pndn_debug,
952 "File '%s' appears to have been created by libpnd so candidate for delete: %u\n", buffer, source_libpnd );
956 pnd_log ( pndn_debug, "File '%s' appears NOT to have been created by libpnd, so leave it alone\n", buffer );
958 continue; // skip deleting it
962 if ( stat ( buffer, &dirs ) == 0 ) {
963 if ( dirs.st_mtime >= createtime ) {
965 pnd_log ( pndn_debug, "File '%s' seems 'new', so leave it alone.\n", buffer );
967 continue; // skip deleting it
971 // by this point, the .desktop file must be 'old' and created by pndnotifyd
972 // previously, so can remove it
973 pnd_log ( pndn_rem, "File '%s' seems nolonger relevent; removing it.\n", dirent -> d_name );
976 } // while getting filenames from dir
981 } // purge old .desktop files
983 //WARN: MEMORY LEAK HERE
984 pnd_log ( pndn_debug, "pndnotifyd - memory leak here - perform_discoveries()\n" );
986 pnd_box_delete ( applist ); // does not free the disco_t contents!