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
20 #include "pnd_container.h"
22 #include "pnd_notify.h"
23 #include "../lib/pnd_pathiter.h"
24 #include "pnd_discovery.h"
25 #include "pnd_locate.h"
27 #include "pnd_utility.h"
28 #include "pnd_desktop.h"
29 #include "pnd_logger.h"
30 #include "pnd_dbusnotify.h"
31 #include "pnd_pndfiles.h"
33 // this piece of code was simpler once; but need to grow it a bit and in a rush
34 // moving all these to globals rather than refactor the code a bit; tsk tsk..
36 // op mode; emitting stdout or no?
37 static unsigned char g_daemon_mode = 0;
41 pndn_rem, // will set default log level to here, so 'debug' is omitted
45 } pndnotify_loglevels_e;
51 char *searchpath = NULL;
52 char *notifypath = NULL;
53 time_t createtime = 0; // all 'new' .destops are created at or after this time; prev are old.
54 // dotfiles; this used to be a single pai .. now two pairs, a little unwieldy; pnd_box it up?
55 char *desktop_dotdesktoppath = NULL;
56 char *desktop_iconpath = NULL;
57 char *desktop_appspath = NULL;
58 char *menu_dotdesktoppath = NULL;
59 char *menu_iconpath = NULL;
60 char *menu_appspath = NULL;
61 char *info_dotdesktoppath = NULL;
63 char *run_searchpath; // searchpath to find pnd_run.sh
64 char *run_script; // name of pnd_run.sh script from config
65 char *pndrun; // full path to located pnd_run.sh
66 char *pndhup = NULL; // full path to located pnd_hup.sh
68 char g_username [ 128 ]; // since we have to wait for login (!!), store username here
70 pnd_notify_handle nh = 0;
71 pnd_dbusnotify_handle dbh = 0;
72 unsigned char g_info_p = 0; // spit out info .desktops
75 #define PNDNOTIFYD_LOGLEVEL "pndnotifyd.loglevel"
76 #define PNDLOCKNAME "pndnotifyd-disco.lock"
79 void consume_configuration ( void );
80 void setup_notifications ( void );
81 void sigint_handler ( int n );
82 void sighup_handler ( int n );
83 void process_discoveries ( pnd_box_handle applist, char *emitdesktoppath, char *emiticonpath );
84 unsigned char perform_discoveries ( char *appspath, char *overridespath,
85 char *emitdesktoppath, char *emiticonpath );
87 int main ( int argc, char *argv[] ) {
89 unsigned char scanonlaunch = 1;
90 unsigned int interval_secs = 5;
91 int logall = -1; // -1 means normal logging rules; >=0 means log all!
95 /* iterate across args
97 for ( i = 1; i < argc; i++ ) {
99 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
100 //printf ( "Going daemon mode. Silent running.\n" );
102 } else if ( isdigit ( argv [ i ][ 0 ] ) ) {
103 interval_secs = atoi ( argv [ i ] );
104 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'n' ) {
106 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
108 if ( isdigit ( argv [ i ][ 2 ] ) ) {
109 unsigned char x = atoi ( argv [ i ] + 2 );
120 printf ( "%s [-d] [-l] [##]\n", argv [ 0 ] );
121 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
122 printf ( "-n\tDo not scan on launch; default is to run a scan for apps when %s is invoked.\n", argv [ 0 ] );
123 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" );
124 printf ( "##\tA numeric value is interpreted as number of seconds between checking for filesystem changes. Default %u.\n",
126 printf ( "Signal: HUP the process to force reload of configuration and reset the notifier watch paths\n" );
134 pnd_log_set_pretext ( "pndnotifyd" );
135 pnd_log_set_flush ( 1 );
137 if ( logall == -1 ) {
138 // standard logging; non-daemon versus daemon
140 if ( g_daemon_mode ) {
143 pnd_log_set_filter ( pndn_rem );
150 f = fopen ( "/tmp/pndnotifyd.log", "w" );
153 pnd_log_set_filter ( logall );
154 pnd_log_to_stream ( f );
155 pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndnotifyd.log\n" );
158 if ( logall == pndn_debug ) {
159 pnd_log_set_buried_logging ( 1 ); // log the shit out of it
160 pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
165 pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
167 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
169 pnd_log ( pndn_rem, "Interval between checks is %u seconds\n", interval_secs );
171 // check if inotify is awake yet; if not, try waiting for awhile to see if it does
172 pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );
174 if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
175 pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
179 pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );
181 // basic daemon set up
182 if ( g_daemon_mode ) {
184 // set a CWD somewhere else
189 // detach from terminal
190 if ( ( i = fork() ) < 0 ) {
191 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
195 exit ( 0 ); // exit parent
200 umask ( 022 ); // emitted files can be rwxr-xr-x
204 // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
205 // log-out and back in again, with SDs popping in and out between..
206 pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
208 if ( pnd_check_login ( g_username, 127 ) ) {
211 pnd_log ( pndn_debug, " No one logged in yet .. spinning.\n" );
214 pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", g_username );
219 consume_configuration();
224 pnd_log ( pndn_rem, "PXML overrides searchpath is '%s'\n", overridespath );
225 pnd_log ( pndn_rem, "Notify searchpath is '%s'\n", notifypath );
227 pnd_log ( pndn_rem, "Desktop apps ---------------------------------\n" );
228 pnd_log ( pndn_rem, "Apps searchpath is '%s'\n", desktop_appspath );
229 pnd_log ( pndn_rem, ".desktop files emit to '%s'\n", desktop_dotdesktoppath );
230 pnd_log ( pndn_rem, ".desktop icon files emit to '%s'\n", desktop_iconpath );
232 pnd_log ( pndn_rem, "Menu apps ---------------------------------\n" );
233 pnd_log ( pndn_rem, "Apps searchpath is '%s'\n", menu_appspath );
234 pnd_log ( pndn_rem, ".desktop files emit to '%s'\n", menu_dotdesktoppath );
235 pnd_log ( pndn_rem, ".desktop icon files emit to '%s'\n", menu_iconpath );
236 pnd_log ( pndn_rem, ".desktop info files emit to '%s'\n", info_dotdesktoppath ? info_dotdesktoppath : "n/a" );
238 /* set up signal handler
243 struct sigaction siggy;
244 siggy.sa_handler = sighup_handler;
245 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
246 siggy.sa_flags = 0; /* don't need anything */
248 sigaction ( SIGHUP, &siggy, NULL );
250 siggy.sa_handler = sigint_handler;
251 sigaction ( SIGINT, &siggy, NULL );
256 // if we're gong to scan on launch, it'll set things right up and then set up notifications.
257 // if no scan on launch, we need to set the notif's up now.
258 //if ( ! scanonlaunch ) {
259 setup_notifications();
262 dbh = pnd_dbusnotify_init();
266 unsigned char watch_inotify, watch_dbus;
273 watch_dbus = pnd_dbusnotify_rediscover_p ( dbh );
276 if ( ! watch_dbus && nh ) {
277 watch_inotify = pnd_notify_rediscover_p ( nh );
280 // need to rediscover?
281 if ( scanonlaunch || watch_inotify || watch_dbus ) {
283 // by this point, the watched directories have notified us that something of relevent
284 // has occurred; we should be clever, but we're not, so just re-brute force the
285 // discovery and spit out .desktop files..
286 pnd_log ( pndn_rem, "------------------------------------------------------\n" );
288 pnd_log ( pndn_rem, "System changes detected in dbus or watched paths .. performing re-discover!\n" );
290 // if this was a forced scan, lets not do that next iteration
291 if ( scanonlaunch ) {
292 pnd_log ( pndn_rem, "scan-on-first-launch detected an event\n" );
296 if ( watch_inotify ) {
297 pnd_log ( pndn_rem, "inotify detected an event\n" );
301 pnd_log ( pndn_rem, "dbusnotify detected an event\n" );
302 pnd_notify_shutdown ( nh );
306 if ( time ( NULL ) - createtime <= 2 ) {
307 pnd_log ( pndn_rem, "Rediscovery request comes to soon after previous discovery; skipping.\n" );
308 sleep ( interval_secs );
312 pnd_log ( pndn_rem, "------------------------------------------------------\n" );
314 createtime = time ( NULL ); // all 'new' .destops are created at or after this time; prev are old.
320 pnd_log ( pndn_rem, "creating lockfile %s", PNDLOCKNAME );
321 if ( ! pnd_lock ( PNDLOCKNAME ) ) {
322 // problem .. well, too bad, we need to do this .. proceed!
326 pnd_log ( pndn_rem, " Scanning desktop paths----------------------------\n" );
327 if ( ! perform_discoveries ( desktop_appspath, overridespath, desktop_dotdesktoppath, desktop_iconpath ) ) {
328 pnd_log ( pndn_rem, " No applications found in desktop search path\n" );
331 if ( menu_appspath && menu_dotdesktoppath && menu_iconpath ) {
332 pnd_log ( pndn_rem, " Scanning menu paths----------------------------\n" );
333 if ( ! perform_discoveries ( menu_appspath, overridespath, menu_dotdesktoppath, menu_iconpath ) ) {
334 pnd_log ( pndn_rem, " No applications found in menu search path\n" );
339 pnd_log ( pndn_rem, "clearing lockfile %s", PNDLOCKNAME );
340 pnd_unlock ( PNDLOCKNAME );
342 // if we've got a hup script located, lets invoke it
344 pnd_log ( pndn_rem, "Invoking hup script '%s'.\n", pndhup );
345 pnd_exec_no_wait_1 ( pndhup, NULL );
348 // since its entirely likely new directories have been found (ie: SD with a directory structure was inserted)
349 // we should re-apply watches to catch all these new directories; ie: user might use on-device browser to
350 // drop in new applications, or use the shell to juggle them around, or any number of activities.
352 setup_notifications();
355 } // need to rediscover?
357 // lets not eat up all the CPU
358 // should use an alarm or select() or something -- I mean really, why aren't I putting interval_secs into
359 // the select() call above in pnd_notify_whatsitcalled()? -- but lets not break this right before release shall we
360 // NOTE: Oh right, I remember now -- inotify will spam when a card is inserted, and it will not be instantaneoous..
361 // the events will dribble in over a second. So this sleep is a lame trick that generally works. I really should
362 // do select(), and then when it returns just spin for a couple seconds slurping up events until no more and a thresh-hold
363 // time is hit, but this will do for now. I suck.
364 sleep ( interval_secs );
370 pnd_notify_shutdown ( nh );
371 pnd_dbusnotify_shutdown ( dbh );
376 void consume_configuration ( void ) {
378 // attempt to fetch a sensible default searchpath for configs
379 configpath = pnd_conf_query_searchpath();
381 // attempt to fetch the apps config to pick up a searchpath
382 pnd_conf_handle apph;
384 apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
388 overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
390 if ( ! overridespath ) {
391 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
394 notifypath = pnd_conf_get_as_char ( apph, PND_APPS_NOTIFY_KEY );
396 if ( ! notifypath ) {
397 notifypath = PND_APPS_NOTIFYPATH;
401 // couldn't find a useful app search path so use the default
402 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
403 notifypath = PND_APPS_NOTIFYPATH;
406 // attempt to figure out where to drop dotfiles .. now that we're going
407 // multi-target we see the limit of my rudimentary conf-file parser; should
408 // just parse to an array of targets, rather that hardcoding two, but
409 // on the other hand, don't likely see the need for more than two? (famous
411 pnd_conf_handle desktoph;
413 desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );
415 // for 'desktop' main applications
417 desktop_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_DOTDESKTOP_PATH_KEY );
418 desktop_iconpath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_ICONS_PATH_KEY );
419 desktop_appspath = pnd_conf_get_as_char ( desktoph, PND_DESKTOP_SEARCH_KEY );
420 info_dotdesktoppath = pnd_conf_get_as_char ( desktoph, "info.dotdesktoppath" );
423 if ( ! desktop_dotdesktoppath ) {
424 desktop_dotdesktoppath = PND_DESKTOP_DOTDESKTOP_PATH_DEFAULT;
427 if ( ! desktop_iconpath ) {
428 desktop_iconpath = PND_DESKTOP_ICONS_PATH_DEFAULT;
431 if ( ! desktop_appspath ) {
432 desktop_appspath = PND_DESKTOP_SEARCH_PATH_DEFAULT;
435 // for 'menu' applications
437 menu_dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_MENU_DOTDESKTOP_PATH_KEY );
438 menu_iconpath = pnd_conf_get_as_char ( desktoph, PND_MENU_ICONS_PATH_KEY );
439 menu_appspath = pnd_conf_get_as_char ( desktoph, PND_MENU_SEARCH_KEY );
444 g_info_p = pnd_conf_get_as_int_d ( desktoph, "info.emit_info", 0 );
447 /* try to locate a runscript and optional hupscript
451 run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
452 run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
455 if ( ! run_searchpath ) {
456 run_searchpath = PND_APPS_SEARCHPATH;
457 run_script = PND_PNDRUN_FILENAME;
460 if ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) != PND_CONF_BADNUM ) {
461 if ( pnd_log_do_buried_logging() == 0 ) {
462 pnd_log_set_filter ( pnd_conf_get_as_int ( apph, PNDNOTIFYD_LOGLEVEL ) );
463 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
465 pnd_log ( pndn_rem, "-l command line suppresses log level change in config file\n" );
470 run_searchpath = NULL;
472 pndrun = PND_PNDRUN_DEFAULT;
476 pndrun = pnd_locate_filename ( run_searchpath, run_script );
479 pndrun = strdup ( pndrun ); // so we don't just use the built in buffer; next locate will overwrite it
481 pndrun = PND_PNDRUN_DEFAULT;
486 if ( desktoph && run_searchpath ) {
489 if ( ( t = pnd_conf_get_as_char ( desktoph, PND_PNDHUP_KEY ) ) ) {
490 pndhup = pnd_locate_filename ( run_searchpath, t );
493 pndhup = strdup ( pndhup ); // so we don't just use the built in buffer; next locate will overwrite it
496 #if 0 // don't enable this; if no key in config, we don't want to bother hupping
498 pndhup = pnd_locate_filename ( run_searchpath, PND_PNDHUP_FILENAME );
505 if ( run_searchpath ) pnd_log ( pndn_rem, "Locating pnd run in %s\n", run_searchpath );
506 if ( run_script ) pnd_log ( pndn_rem, "Locating pnd runscript as %s\n", run_script );
507 if ( pndrun ) pnd_log ( pndn_rem, "pndrun is %s\n", pndrun );
509 pnd_log ( pndn_rem, "pndhup is %s\n", pndhup );
511 pnd_log ( pndn_rem, "No pndhup found (which is fine.)\n" );
514 /* cheap hack, maybe it'll help when pndnotifyd is coming up before the rest of the system and before
515 * the user is formally logged in
517 pnd_log ( pndn_rem, "Setting a default $HOME to non-root user\n" );
519 // first, try to see if known-username maps to a homedir; if so, just use that!
520 // otherwise, pick first non-root homedir and assume .. or we start blaring .desktops
521 // out to all users like idiots
522 unsigned char got_user_homedir = 0;
524 if ( g_username [ 0 ] ) {
526 char path [ PATH_MAX ];
528 sprintf ( path, "/home/%s", g_username );
530 // does this made up path exist?
531 if ( stat ( path, &homedir ) == 0 ) {
534 if ( S_ISDIR(homedir.st_mode) ) {
535 pnd_log ( pndn_rem, " User [%s] matches path [%s], going with '%s'\n", g_username, path, path );
536 setenv ( "HOME", path, 1 /* overwrite */ );
537 got_user_homedir = 1;
540 } // guessing a homedirname..
544 // if guessing a path was no good, just try finding one
545 if ( got_user_homedir == 0 ) {
548 if ( ( dir = opendir ( "/home" ) ) ) {
549 struct dirent *dirent;
551 while ( ( dirent = readdir ( dir ) ) ) {
552 pnd_log ( pndn_rem, " Scanning user homedir '%s'\n", dirent -> d_name );
554 // file is a .desktop?
555 if ( dirent -> d_name [ 0 ] == '.' ) {
557 } else if ( strcmp ( dirent -> d_name, "root" ) == 0 ) {
561 // a non-root user is found
563 sprintf ( buffer, "/home/%s", dirent -> d_name );
564 pnd_log ( pndn_rem, " Going with '%s'\n", buffer );
565 setenv ( "HOME", buffer, 1 /* overwrite */ );
568 } // while iterating through dir listing
575 /* handle globbing or variable substitution
577 desktop_dotdesktoppath = pnd_expand_tilde ( strdup ( desktop_dotdesktoppath ) );
578 desktop_iconpath = pnd_expand_tilde ( strdup ( desktop_iconpath ) );
579 mkdir ( desktop_dotdesktoppath, 0777 );
580 mkdir ( desktop_iconpath, 0777 );
582 if ( info_dotdesktoppath ) {
583 info_dotdesktoppath = pnd_expand_tilde ( strdup ( info_dotdesktoppath ) );
584 mkdir ( info_dotdesktoppath, 0777 );
587 if ( menu_dotdesktoppath ) {
588 menu_dotdesktoppath = pnd_expand_tilde ( strdup ( menu_dotdesktoppath ) );
589 mkdir ( menu_dotdesktoppath, 0777 );
591 if ( menu_iconpath ) {
592 menu_iconpath = pnd_expand_tilde ( strdup ( menu_iconpath ) );
593 mkdir ( menu_iconpath, 0777 );
600 void setup_notifications ( void ) {
601 searchpath = notifypath;
603 // if this is first time through, we can just set it up; for subsequent times
604 // through, we need to close existing fd and re-open it, since we're too lame
605 // to store the list of watches and 'rm' them
608 pnd_notify_shutdown ( nh );
613 // set up a new set of notifies
615 nh = pnd_notify_init();
619 pnd_log ( pndn_rem, "INOTIFY failed to init.\n" );
624 pnd_log ( pndn_rem, "INOTIFY is up.\n" );
630 pnd_log ( pndn_rem, "Watching path '%s' and its descendents.\n", buffer );
631 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
633 //pnd_notify_watch_path ( nh, buffer, 0 /* no recurse */ );
639 sleep ( 1 ); // wait for events to trigger?
641 // clear out any notifies we just created
642 while ( pnd_notify_rediscover_p ( nh ) ) {
643 usleep ( 100 ); // spin
650 void sighup_handler ( int n ) {
652 pnd_log ( pndn_rem, "---[ SIGHUP received ]---\n" );
654 // reparse config files
655 consume_configuration();
657 // re set up the notifier watches
658 setup_notifications();
663 void sigint_handler ( int n ) {
665 pnd_log ( pndn_rem, "---[ SIGINT received ]---\n" );
668 pnd_dbusnotify_shutdown ( dbh );
672 pnd_notify_shutdown ( nh );
678 // This very recently was inline code; just slight refactor to functionize it so that it can be
679 // reused in a couple of places. Simple code with simple design quickly became too large for
680 // its simple design; should revisit a lot of these little things..
681 void process_discoveries ( pnd_box_handle applist, char *emitdesktoppath, char *emiticonpath ) {
682 pnd_disco_t *d = pnd_box_get_head ( applist );
686 pnd_log ( pndn_rem, "Found app: %s\n", pnd_box_get_key ( d ) );
688 // check if icon already exists (from a previous extraction say); if so, we needn't
690 char existingpath [ FILENAME_MAX ];
691 sprintf ( existingpath, "%s/%s.png", emiticonpath, d -> unique_id /*, d -> subapp_number */ );
694 if ( stat ( existingpath, &dirs ) == 0 ) {
695 // icon seems to exist, so just crib the location into the .desktop
697 pnd_log ( pndn_rem, " Found icon already existed, so reusing it! %s\n", existingpath );
702 d -> icon = strdup ( existingpath );
705 // icon seems unreadable or does not exist; lets try to create it..
707 pnd_log ( pndn_debug, " Icon not already present, so trying to write it! %s\n", existingpath );
709 // handle same-path icon override for davec :)
710 char ovrfile [ PATH_MAX ];
712 sprintf ( ovrfile, "%s/%s", d -> object_path, d -> object_filename );
713 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
715 strcpy ( fixpxml, ".png" );
718 if ( stat ( ovrfile, &statbuf ) == 0 ) {
719 d -> icon = strdup ( ovrfile );
720 fixpxml = ovrfile; // !NULL will be the trigger to skip emittinf desktop from .pnd
724 // attempt to create icon files; if successful, alter the disco struct to contain new
725 // path, otherwise leave it alone (since it could be a generic icon reference..)
726 if ( fixpxml == NULL ) {
727 // don't have an same-path override icon, so go fetch something from pnd file
729 if ( pnd_emit_icon ( emiticonpath, d ) ) {
730 // success; fix up icon path to new one..
734 d -> icon = strdup ( existingpath );
736 pnd_log ( pndn_debug, " WARN: Couldn't write out icon %s\n", existingpath );
739 } // got ovr icon already?
741 } // icon already exists?
743 // create the .desktop file
744 if ( pnd_emit_dotdesktop ( emitdesktoppath, pndrun, d ) ) {
745 // add a watch onto the newly created .desktop?
747 char buffer [ FILENAME_MAX ];
748 sprintf ( buffer, "%s/%s", emitdesktoppath, d -> unique_id );
749 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
752 pnd_log ( pndn_rem, "ERROR: Error creating .desktop file for app: %s\n", pnd_box_get_key ( d ) );
756 if ( g_info_p && info_dotdesktoppath ) {
757 if ( ! pnd_emit_dotinfo ( info_dotdesktoppath, pndrun, d ) ) {
758 pnd_log ( pndn_rem, "ERROR: Error creating info .desktop file for app: %s\n", pnd_box_get_key ( d ) );
762 // file association requests
763 if ( ! pnd_emit_dotassoc ( info_dotdesktoppath, pndrun, d ) ) {
764 pnd_log ( pndn_rem, "ERROR: Error creating file association .desktop file for app: %s\n", pnd_box_get_key ( d ) );
767 // does this object request any mkdir's?
768 if ( d -> mkdir_sp ) {
770 // it would appear it does! but we have to carefully validate these suckers
771 pnd_log ( pndn_rem, " App %s requests mkdir: %s\n", d -> object_path, d -> mkdir_sp );
773 // for each mkdir requested path, do it...
774 char *searchpath = d -> mkdir_sp;
778 /* "buffer" now holds each chunk of the searchpath, expanded */
780 // WARN: This whole concept could be flawed; what if they represent '..' in some other obscure way (unicode?)
781 // and we end up allowing mkdir's all over the place? The risk really is limited -- once the pnd is here,
782 // if the user _runs it_, it can go nuts, so creating a few dirs isn't all that dangerous...
784 // Perhaps I should have a config setting for pndnotifyd to suppress this whole mkdir behaviour?
786 // if not containing ".." we allow it
787 if ( strstr ( buffer, ".." ) == NULL ) {
789 // determine mountpoint for the file
790 // - we could deduce this from the path (somewhat risky if we assume leading /media/mmcblk1p1 type notation .. could
791 // be other distributions entirely
792 // - better to scan through mount-list and figure it out.. *sucks*
793 char mountpoint [ PATH_MAX ];
794 if ( pnd_determine_mountpoint ( d -> object_path, mountpoint, PATH_MAX - strlen ( buffer ) - 1 ) == 1 ) {
796 strcat ( mountpoint, "/" );
797 strcat ( mountpoint, buffer );
800 if ( stat ( mountpoint, &t ) == 0 ) {
801 pnd_log ( pndn_rem, " Skipping existing mkdir: %s\n", mountpoint );
803 pnd_log ( pndn_rem, " Attempting create of non-existant path: %s\n", mountpoint );
804 mkdir ( mountpoint, 0777 );
807 } // if figured out the mountpoint
817 d = pnd_box_get_next ( d );
824 // returns true if any applications were found
825 unsigned char perform_discoveries ( char *appspath, char *overridespath, // args to do discovery
826 char *emitdesktoppath, char *emiticonpath ) // args to do emitting
828 pnd_box_handle applist;
830 pnd_log ( pndn_rem, "perform discovery - apps: %s, overrides: %s\n", appspath, overridespath );
831 pnd_log ( pndn_rem, " - emit desktop: %s, icons: %s\n", emitdesktoppath, emiticonpath );
833 // attempt to auto-discover applications in the given path
834 applist = pnd_disco_search ( appspath, overridespath );
837 process_discoveries ( applist, emitdesktoppath, emiticonpath );
840 // run a clean up, to remove any dotdesktop files that we didn't
841 // just now create (that seem to have been created by pndnotifyd
842 // previously.) This allows SD eject (or .pnd remove) to remove
843 // an app from the launcher
844 // NOTE: Could opendir and iterate across all .desktop files,
845 // removing any that have Source= something else, and that the
846 // app name is not in the list found in applist box above. But
847 // a cheesy simple way right now is to just remove .desktop files
848 // that have a last mod time prior to the time we stored above.
852 if ( ( dir = opendir ( emitdesktoppath ) ) ) {
853 struct dirent *dirent;
855 char buffer [ FILENAME_MAX ];
857 while ( ( dirent = readdir ( dir ) ) ) {
859 // file is a .desktop?
860 if ( strstr ( dirent -> d_name, ".desktop" ) == NULL ) {
864 // figure out full path
865 sprintf ( buffer, "%s/%s", emitdesktoppath, dirent -> d_name );
867 // file was previously created by libpnd; check Source= line
868 // logic: default to 'yes' (in case we can't open the file for some reason)
869 // if we can open the file, default to no and look for the source flag we added; if
870 // that matches then we know its libpnd created, otherwise assume not.
871 unsigned char source_libpnd = 1;
874 FILE *grep = fopen ( buffer, "r" );
877 while ( fgets ( line, 255, grep ) ) {
878 if ( strcasestr ( line, PND_DOTDESKTOP_SOURCE ) ) {
885 if ( source_libpnd ) {
887 pnd_log ( pndn_debug,
888 "File '%s' appears to have been created by libpnd so candidate for delete: %u\n", buffer, source_libpnd );
892 pnd_log ( pndn_debug, "File '%s' appears NOT to have been created by libpnd, so leave it alone\n", buffer );
894 continue; // skip deleting it
898 if ( stat ( buffer, &dirs ) == 0 ) {
899 if ( dirs.st_mtime >= createtime ) {
901 pnd_log ( pndn_debug, "File '%s' seems 'new', so leave it alone.\n", buffer );
903 continue; // skip deleting it
907 // by this point, the .desktop file must be 'old' and created by pndnotifyd
908 // previously, so can remove it
909 pnd_log ( pndn_rem, "File '%s' seems nolonger relevent; removing it.\n", dirent -> d_name );
912 } // while getting filenames from dir
917 } // purge old .desktop files
919 //WARN: MEMORY LEAK HERE
920 pnd_log ( pndn_debug, "pndnotifyd - memory leak here - perform_discoveries()\n" );
922 pnd_box_delete ( applist ); // does not free the disco_t contents!