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
10 // TODO: Removing stale .desktop checks that .desktop was created by libpnd; see 'TBD' below
12 #include <stdio.h> // for stdio
13 #include <unistd.h> // for exit()
14 #include <stdlib.h> // for exit()
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()
23 #include "pnd_container.h"
25 #include "pnd_notify.h"
26 #include "../lib/pnd_pathiter.h"
27 #include "pnd_discovery.h"
28 #include "pnd_locate.h"
30 static unsigned char g_daemon_mode = 0;
32 int main ( int argc, char *argv[] ) {
39 char *searchpath = NULL;
40 char *dotdesktoppath = NULL;
42 unsigned char scanonlaunch = 1;
43 unsigned int interval_secs = 20;
51 /* iterate across args
53 for ( i = 1; i < argc; i++ ) {
55 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
56 //printf ( "Going daemon mode. Silent running.\n" );
58 } else if ( isdigit ( argv [ i ][ 0 ] ) ) {
59 interval_secs = atoi ( argv [ i ] );
60 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'n' ) {
63 printf ( "%s [-d] [##]\n", argv [ 0 ] );
64 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
65 printf ( "-n\tDo not scan on launch; default is to run a scan for apps when %s is invoked.\n", argv [ 0 ] );
66 printf ( "##\tA numeric value is interpreted as number of seconds between checking for filesystem changes. Default %u.\n",
73 if ( ! g_daemon_mode ) {
74 printf ( "Interval between checks is %u seconds\n", interval_secs );
77 // basic daemon set up
78 if ( g_daemon_mode ) {
80 // set a CWD somewhere else
85 // detach from terminal
86 if ( ( i = fork() ) < 0 ) {
87 printf ( "ERROR: Couldn't fork()\n" );
91 exit ( 0 ); // exit parent
96 umask ( 022 ); // emitted files can be rwxr-xr-x
103 // attempt to fetch a sensible default searchpath for configs
104 configpath = pnd_conf_query_searchpath();
106 // attempt to fetch the apps config to pick up a searchpath
107 pnd_conf_handle apph;
109 apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
112 appspath = pnd_conf_get_as_char ( apph, PND_APPS_KEY );
115 appspath = PND_APPS_SEARCHPATH;
118 overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
120 if ( ! overridespath ) {
121 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
125 // couldn't find a useful app search path so use the default
126 appspath = PND_APPS_SEARCHPATH;
127 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
130 // attempt to figure out where to drop dotfiles
131 pnd_conf_handle desktoph;
133 desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );
136 dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DOTDESKTOP_KEY );
138 if ( ! dotdesktoppath ) {
139 dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
143 dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
146 // try to locate a runscript
149 run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
150 run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
153 if ( ! run_searchpath ) {
154 run_searchpath = PND_APPS_SEARCHPATH;
155 run_script = PND_PNDRUN_FILENAME;
159 run_searchpath = NULL;
161 pndrun = PND_PNDRUN_DEFAULT;
165 pndrun = pnd_locate_filename ( run_searchpath, run_script );
168 pndrun = PND_PNDRUN_DEFAULT;
173 if ( ! g_daemon_mode ) {
174 if ( run_searchpath ) printf ( "Locating pnd run in %s\n", run_searchpath );
175 if ( run_script ) printf ( "Locating pnd runscript as %s\n", run_script );
176 if ( pndrun ) printf ( "Default pndrun is %s\n", pndrun );
182 if ( ! g_daemon_mode ) {
183 printf ( "Apps searchpath is '%s'\n", appspath );
184 printf ( "PXML overrides searchpath is '%s'\n", overridespath );
185 printf ( ".desktop files emit to '%s'\n", dotdesktoppath );
190 searchpath = appspath;
192 nh = pnd_notify_init();
195 if ( ! g_daemon_mode ) {
196 printf ( "INOTIFY failed to init.\n" );
201 if ( ! g_daemon_mode ) {
202 printf ( "INOTIFY is up.\n" );
208 if ( ! g_daemon_mode ) {
209 printf ( "Watching path '%s' and its descendents.\n", buffer );
212 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
221 // need to rediscover?
223 pnd_notify_rediscover_p ( nh ) )
225 pnd_box_handle applist;
226 time_t createtime = time ( NULL ); // all 'new' .destops are created at or after this time; prev are old.
228 // if this was a forced scan, lets not do that next iteration
229 if ( scanonlaunch ) {
233 // by this point, the watched directories have notified us that something of relevent
234 // has occurred; we should be clever, but we're not, so just re-brute force the
235 // discovery and spit out .desktop files..
236 if ( ! g_daemon_mode ) {
237 printf ( "Changes within watched paths .. performing re-discover!\n" );
238 printf ( "Path to emit .desktop files to: '%s'\n", dotdesktoppath );
242 applist = pnd_disco_search ( appspath, overridespath );
244 // list the found apps (if any)
246 pnd_disco_t *d = pnd_box_get_head ( applist );
250 if ( ! g_daemon_mode ) {
251 printf ( "Found app: %s\n", pnd_box_get_key ( d ) );
254 // create the .desktop file
255 if ( pnd_emit_dotdesktop ( dotdesktoppath, pndrun, d ) ) {
256 // add a watch onto the newly created .desktop?
258 char buffer [ FILENAME_MAX ];
259 sprintf ( buffer, "%s/%s", dotdesktoppath, d -> unique_id );
260 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
263 if ( ! g_daemon_mode ) {
264 printf ( "ERROR: Error creating .desktop file for app: %s\n", pnd_box_get_key ( d ) );
269 d = pnd_box_get_next ( d );
275 if ( ! g_daemon_mode ) {
276 printf ( "No applications found in search path\n" );
281 // run a clean up, to remove any dotdesktop files that we didn't
282 // just now create (that seem to have been created by pndnotifyd
283 // previously.) This allows SD eject (or .pnd remove) to remove
284 // an app from the launcher
285 // NOTE: Could opendir and iterate across all .desktop files,
286 // removing any that have Source= something else, and that the
287 // app name is not in the list found in applist box above. But
288 // a cheesy simple way right now is to just remove .desktop files
289 // that have a last mod time prior to the time we stored above.
293 if ( ( dir = opendir ( dotdesktoppath ) ) ) {
294 struct dirent *dirent;
296 char buffer [ FILENAME_MAX ];
298 while ( ( dirent = readdir ( dir ) ) ) {
300 // file is a .desktop?
301 if ( strstr ( dirent -> d_name, ".desktop" ) == NULL ) {
305 // file was previously created by libpnd; check Source= line
309 sprintf ( buffer, "%s/%s", dotdesktoppath, dirent -> d_name );
310 if ( stat ( buffer, &dirs ) == 0 ) {
311 if ( dirs.st_mtime >= createtime ) {
316 // by this point, the .desktop file must be 'old' and created by pndnotifyd
317 // previously, so can remove it
318 if ( ! g_daemon_mode ) {
319 printf ( "File '%s' seems to not belong; removing it.\n", dirent -> d_name );
323 } // while getting filenames from dir
328 } // purge old .desktop files
330 } // need to rediscover?
332 // lets not eat up all the CPU
333 // should use an alarm or select() or something
334 sleep ( interval_secs );
340 pnd_notify_shutdown ( nh );