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()
15 #define __USE_GNU /* for strcasestr */
17 #include <time.h> // for time()
18 #include <ctype.h> // for isdigit()
19 #include <sys/types.h> // for umask
20 #include <sys/stat.h> // for umask
21 #include <dirent.h> // for opendir()
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"
30 #include "pnd_utility.h"
32 static unsigned char g_daemon_mode = 0;
34 int main ( int argc, char *argv[] ) {
41 char *searchpath = NULL;
42 char *dotdesktoppath = NULL;
44 unsigned char scanonlaunch = 1;
45 unsigned int interval_secs = 20;
53 /* iterate across args
55 for ( i = 1; i < argc; i++ ) {
57 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
58 //printf ( "Going daemon mode. Silent running.\n" );
60 } else if ( isdigit ( argv [ i ][ 0 ] ) ) {
61 interval_secs = atoi ( argv [ i ] );
62 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'n' ) {
65 printf ( "%s [-d] [##]\n", argv [ 0 ] );
66 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
67 printf ( "-n\tDo not scan on launch; default is to run a scan for apps when %s is invoked.\n", argv [ 0 ] );
68 printf ( "##\tA numeric value is interpreted as number of seconds between checking for filesystem changes. Default %u.\n",
75 if ( ! g_daemon_mode ) {
76 printf ( "Interval between checks is %u seconds\n", interval_secs );
79 // basic daemon set up
80 if ( g_daemon_mode ) {
82 // set a CWD somewhere else
87 // detach from terminal
88 if ( ( i = fork() ) < 0 ) {
89 printf ( "ERROR: Couldn't fork()\n" );
93 exit ( 0 ); // exit parent
98 umask ( 022 ); // emitted files can be rwxr-xr-x
105 // attempt to fetch a sensible default searchpath for configs
106 configpath = pnd_conf_query_searchpath();
108 // attempt to fetch the apps config to pick up a searchpath
109 pnd_conf_handle apph;
111 apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
114 appspath = pnd_conf_get_as_char ( apph, PND_APPS_KEY );
117 appspath = PND_APPS_SEARCHPATH;
120 overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
122 if ( ! overridespath ) {
123 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
127 // couldn't find a useful app search path so use the default
128 appspath = PND_APPS_SEARCHPATH;
129 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
132 // attempt to figure out where to drop dotfiles
133 pnd_conf_handle desktoph;
135 desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );
138 dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DOTDESKTOP_KEY );
140 if ( ! dotdesktoppath ) {
141 dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
145 dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
148 // try to locate a runscript
151 run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
152 run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
155 if ( ! run_searchpath ) {
156 run_searchpath = PND_APPS_SEARCHPATH;
157 run_script = PND_PNDRUN_FILENAME;
161 run_searchpath = NULL;
163 pndrun = PND_PNDRUN_DEFAULT;
167 pndrun = pnd_locate_filename ( run_searchpath, run_script );
170 pndrun = PND_PNDRUN_DEFAULT;
175 if ( ! g_daemon_mode ) {
176 if ( run_searchpath ) printf ( "Locating pnd run in %s\n", run_searchpath );
177 if ( run_script ) printf ( "Locating pnd runscript as %s\n", run_script );
178 if ( pndrun ) printf ( "Default pndrun is %s\n", pndrun );
181 /* handle globbing or variable substitution
183 dotdesktoppath = pnd_expand_tilde ( strdup ( dotdesktoppath ) );
187 mkdir ( dotdesktoppath, 0777 );
192 if ( ! g_daemon_mode ) {
193 printf ( "Apps searchpath is '%s'\n", appspath );
194 printf ( "PXML overrides searchpath is '%s'\n", overridespath );
195 printf ( ".desktop files emit to '%s'\n", dotdesktoppath );
200 searchpath = appspath;
202 nh = pnd_notify_init();
205 if ( ! g_daemon_mode ) {
206 printf ( "INOTIFY failed to init.\n" );
211 if ( ! g_daemon_mode ) {
212 printf ( "INOTIFY is up.\n" );
218 if ( ! g_daemon_mode ) {
219 printf ( "Watching path '%s' and its descendents.\n", buffer );
222 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
231 // need to rediscover?
233 pnd_notify_rediscover_p ( nh ) )
235 pnd_box_handle applist;
236 time_t createtime = time ( NULL ); // all 'new' .destops are created at or after this time; prev are old.
238 // if this was a forced scan, lets not do that next iteration
239 if ( scanonlaunch ) {
243 // by this point, the watched directories have notified us that something of relevent
244 // has occurred; we should be clever, but we're not, so just re-brute force the
245 // discovery and spit out .desktop files..
246 if ( ! g_daemon_mode ) {
247 printf ( "Changes within watched paths .. performing re-discover!\n" );
248 printf ( "Path to emit .desktop files to: '%s'\n", dotdesktoppath );
252 applist = pnd_disco_search ( appspath, overridespath );
254 // list the found apps (if any)
256 pnd_disco_t *d = pnd_box_get_head ( applist );
260 if ( ! g_daemon_mode ) {
261 printf ( "Found app: %s\n", pnd_box_get_key ( d ) );
264 // check if icon already exists (from a previous extraction say); if so, we needn't
266 char existingpath [ FILENAME_MAX ];
267 sprintf ( existingpath, "%s/%s.png", dotdesktoppath, d -> unique_id );
270 if ( stat ( existingpath, &dirs ) == 0 ) {
271 // icon seems to exist, so just crib the location into the .desktop
273 if ( ! g_daemon_mode ) {
274 printf ( " Found icon already existed, so reusing it! %s\n", existingpath );
280 d -> icon = strdup ( existingpath );
283 // icon seems unreadable or does not exist; lets try to create it..
285 if ( ! g_daemon_mode ) {
286 printf ( " Icon not already present, so trying to write it! %s\n", existingpath );
289 // attempt to create icon files; if successful, alter the disco struct to contain new
290 // path, otherwise leave it alone (since it could be a generic icon reference..)
291 if ( pnd_emit_icon ( dotdesktoppath, d ) ) {
292 // success; fix up icon path to new one..
296 d -> icon = strdup ( existingpath );
299 } // icon already exists?
301 // create the .desktop file
302 if ( pnd_emit_dotdesktop ( dotdesktoppath, pndrun, d ) ) {
303 // add a watch onto the newly created .desktop?
305 char buffer [ FILENAME_MAX ];
306 sprintf ( buffer, "%s/%s", dotdesktoppath, d -> unique_id );
307 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
310 if ( ! g_daemon_mode ) {
311 printf ( "ERROR: Error creating .desktop file for app: %s\n", pnd_box_get_key ( d ) );
316 d = pnd_box_get_next ( d );
322 if ( ! g_daemon_mode ) {
323 printf ( "No applications found in search path\n" );
328 // run a clean up, to remove any dotdesktop files that we didn't
329 // just now create (that seem to have been created by pndnotifyd
330 // previously.) This allows SD eject (or .pnd remove) to remove
331 // an app from the launcher
332 // NOTE: Could opendir and iterate across all .desktop files,
333 // removing any that have Source= something else, and that the
334 // app name is not in the list found in applist box above. But
335 // a cheesy simple way right now is to just remove .desktop files
336 // that have a last mod time prior to the time we stored above.
340 if ( ( dir = opendir ( dotdesktoppath ) ) ) {
341 struct dirent *dirent;
343 char buffer [ FILENAME_MAX ];
345 while ( ( dirent = readdir ( dir ) ) ) {
347 // file is a .desktop?
348 if ( strstr ( dirent -> d_name, ".desktop" ) == NULL ) {
352 // figure out full path
353 sprintf ( buffer, "%s/%s", dotdesktoppath, dirent -> d_name );
355 // file was previously created by libpnd; check Source= line
356 // logic: default to 'yes' (in case we can't open the file for some reason)
357 // if we can open the file, default to no and look for the source flag we added; if
358 // that matches then we know its libpnd created, otherwise assume not.
359 unsigned char source_libpnd = 1;
362 FILE *grep = fopen ( buffer, "r" );
365 while ( fgets ( line, 255, grep ) ) {
366 if ( strcasestr ( line, PND_DOTDESKTOP_SOURCE ) ) {
373 if ( source_libpnd ) {
374 if ( ! g_daemon_mode ) {
375 printf ( "File '%s' appears to have been created by libpnd so candidate for delete: %u\n", buffer, source_libpnd );
378 if ( ! g_daemon_mode ) {
379 printf ( "File '%s' appears NOT to have been created by libpnd, so leave it alone\n", buffer );
381 continue; // skip deleting it
385 if ( stat ( buffer, &dirs ) == 0 ) {
386 if ( dirs.st_mtime >= createtime ) {
387 if ( ! g_daemon_mode ) {
388 printf ( "File '%s' seems 'new', so leave it alone.\n", buffer );
390 continue; // skip deleting it
394 // by this point, the .desktop file must be 'old' and created by pndnotifyd
395 // previously, so can remove it
396 if ( ! g_daemon_mode ) {
397 printf ( "File '%s' seems to not belong; removing it.\n", dirent -> d_name );
401 } // while getting filenames from dir
406 } // purge old .desktop files
408 } // need to rediscover?
410 // lets not eat up all the CPU
411 // should use an alarm or select() or something
412 sleep ( interval_secs );
418 pnd_notify_shutdown ( nh );