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"
29 static unsigned char g_daemon_mode = 0;
31 int main ( int argc, char *argv[] ) {
38 char *searchpath = NULL;
39 char *dotdesktoppath = NULL;
41 unsigned char scanonlaunch = 1;
42 unsigned int interval_secs = 20;
46 /* iterate across args
48 for ( i = 1; i < argc; i++ ) {
50 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
51 //printf ( "Going daemon mode. Silent running.\n" );
53 } else if ( isdigit ( argv [ i ][ 0 ] ) ) {
54 interval_secs = atoi ( argv [ i ] );
55 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'n' ) {
58 printf ( "%s [-d] [##]\n", argv [ 0 ] );
59 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
60 printf ( "-n\tDo not scan on launch; default is to run a scan for apps when %s is invoked.\n", argv [ 0 ] );
61 printf ( "##\tA numeric value is interpreted as number of seconds between checking for filesystem changes. Default %u.\n",
68 if ( ! g_daemon_mode ) {
69 printf ( "Interval between checks is %u seconds\n", interval_secs );
72 // basic daemon set up
73 if ( g_daemon_mode ) {
75 // set a CWD somewhere else
80 // detach from terminal
81 if ( ( i = fork() ) < 0 ) {
82 printf ( "ERROR: Couldn't fork()\n" );
86 exit ( 0 ); // exit parent
91 umask ( 022 ); // emitted files can be rwxr-xr-x
98 // attempt to fetch a sensible default searchpath for configs
99 configpath = pnd_conf_query_searchpath();
101 // attempt to fetch the apps config to pick up a searchpath
102 pnd_conf_handle apph;
104 apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
107 appspath = pnd_conf_get_as_char ( apph, PND_APPS_KEY );
110 appspath = PND_APPS_SEARCHPATH;
113 overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
115 if ( ! overridespath ) {
116 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
120 // couldn't find a useful app search path so use the default
121 appspath = PND_APPS_SEARCHPATH;
122 overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
125 // attempt to figure out where to drop dotfiles
126 pnd_conf_handle desktoph;
128 desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );
131 dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DOTDESKTOP_KEY );
133 if ( ! dotdesktoppath ) {
134 dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
138 dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
144 if ( ! g_daemon_mode ) {
145 printf ( "Apps searchpath is '%s'\n", appspath );
146 printf ( "PXML overrides searchpath is '%s'\n", overridespath );
147 printf ( ".desktop files emit to '%s'\n", dotdesktoppath );
152 searchpath = appspath;
154 nh = pnd_notify_init();
157 if ( ! g_daemon_mode ) {
158 printf ( "INOTIFY failed to init.\n" );
163 if ( ! g_daemon_mode ) {
164 printf ( "INOTIFY is up.\n" );
170 if ( ! g_daemon_mode ) {
171 printf ( "Watching path '%s' and its descendents.\n", buffer );
174 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
183 // need to rediscover?
185 pnd_notify_rediscover_p ( nh ) )
187 pnd_box_handle applist;
188 time_t createtime = time ( NULL ); // all 'new' .destops are created at or after this time; prev are old.
190 // if this was a forced scan, lets not do that next iteration
191 if ( scanonlaunch ) {
195 // by this point, the watched directories have notified us that something of relevent
196 // has occurred; we should be clever, but we're not, so just re-brute force the
197 // discovery and spit out .desktop files..
198 if ( ! g_daemon_mode ) {
199 printf ( "Changes within watched paths .. performing re-discover!\n" );
200 printf ( "Path to emit .desktop files to: '%s'\n", dotdesktoppath );
204 applist = pnd_disco_search ( appspath, overridespath );
206 // list the found apps (if any)
208 pnd_disco_t *d = pnd_box_get_head ( applist );
212 if ( ! g_daemon_mode ) {
213 printf ( "Found app: %s\n", pnd_box_get_key ( d ) );
216 // create the .desktop file
217 if ( pnd_emit_dotdesktop ( dotdesktoppath, d ) ) {
218 // add a watch onto the newly created .desktop?
220 char buffer [ FILENAME_MAX ];
221 sprintf ( buffer, "%s/%s", dotdesktoppath, d -> unique_id );
222 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
225 if ( ! g_daemon_mode ) {
226 printf ( "ERROR: Error creating .desktop file for app: %s\n", pnd_box_get_key ( d ) );
231 d = pnd_box_get_next ( d );
237 if ( ! g_daemon_mode ) {
238 printf ( "No applications found in search path\n" );
243 // run a clean up, to remove any dotdesktop files that we didn't
244 // just now create (that seem to have been created by pndnotifyd
245 // previously.) This allows SD eject (or .pnd remove) to remove
246 // an app from the launcher
247 // NOTE: Could opendir and iterate across all .desktop files,
248 // removing any that have Source= something else, and that the
249 // app name is not in the list found in applist box above. But
250 // a cheesy simple way right now is to just remove .desktop files
251 // that have a last mod time prior to the time we stored above.
255 if ( ( dir = opendir ( dotdesktoppath ) ) ) {
256 struct dirent *dirent;
258 char buffer [ FILENAME_MAX ];
260 while ( ( dirent = readdir ( dir ) ) ) {
262 // file is a .desktop?
263 if ( strstr ( dirent -> d_name, ".desktop" ) == NULL ) {
267 // file was previously created by libpnd; check Source= line
271 sprintf ( buffer, "%s/%s", dotdesktoppath, dirent -> d_name );
272 if ( stat ( buffer, &dirs ) == 0 ) {
273 if ( dirs.st_mtime >= createtime ) {
278 // by this point, the .desktop file must be 'old' and created by pndnotifyd
279 // previously, so can remove it
280 if ( ! g_daemon_mode ) {
281 printf ( "File '%s' seems to not belong; removing it.\n", dirent -> d_name );
285 } // while getting filenames from dir
290 } // purge old .desktop files
292 } // need to rediscover?
294 // lets not eat up all the CPU
295 // should use an alarm or select() or something
296 sleep ( interval_secs );
302 pnd_notify_shutdown ( nh );