b5341530e089be0db2d05fd73e97f9381c514b12
[pandora-libraries.git] / apps / pndnotifyd.c
1
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
4  *
5  */
6
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
11
12 #include <stdio.h>     // for stdio
13 #include <unistd.h>    // for exit()
14 #include <stdlib.h>    // for exit()
15 #include <string.h>
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()
21
22 #include "pnd_conf.h"
23 #include "pnd_container.h"
24 #include "pnd_apps.h"
25 #include "pnd_notify.h"
26 #include "../lib/pnd_pathiter.h"
27 #include "pnd_discovery.h"
28
29 static unsigned char g_daemon_mode = 0;
30
31 int main ( int argc, char *argv[] ) {
32   pnd_notify_handle nh;
33   // like discotest
34   char *configpath;
35   char *appspath;
36   char *overridespath;
37   // daemon stuff
38   char *searchpath = NULL;
39   char *dotdesktoppath = NULL;
40   // behaviour
41   unsigned char scanonlaunch = 1;
42   unsigned int interval_secs = 20;
43   // misc
44   int i;
45
46   /* iterate across args
47    */
48   for ( i = 1; i < argc; i++ ) {
49
50     if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
51       //printf ( "Going daemon mode. Silent running.\n" );
52       g_daemon_mode = 1;
53     } else if ( isdigit ( argv [ i ][ 0 ] ) ) {
54       interval_secs = atoi ( argv [ i ] );
55     } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'n' ) {
56       scanonlaunch = 0;
57     } else {
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",
62                interval_secs );
63       exit ( 0 );
64     }
65
66   }
67
68   if ( ! g_daemon_mode ) {
69     printf ( "Interval between checks is %u seconds\n", interval_secs );
70   }
71
72   // basic daemon set up
73   if ( g_daemon_mode ) {
74
75     // set a CWD somewhere else
76 #if 0
77     chdir ( "/tmp" );
78 #endif
79
80     // detach from terminal
81     if ( ( i = fork() ) < 0 ) {
82       printf ( "ERROR: Couldn't fork()\n" );
83       exit ( i );
84     }
85     if ( i ) {
86       exit ( 0 ); // exit parent
87     }
88     setsid();
89
90     // umask
91     umask ( 022 ); // emitted files can be rwxr-xr-x
92     
93   } // set up daemon
94
95   /* parse configs
96    */
97
98   // attempt to fetch a sensible default searchpath for configs
99   configpath = pnd_conf_query_searchpath();
100
101   // attempt to fetch the apps config to pick up a searchpath
102   pnd_conf_handle apph;
103
104   apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
105
106   if ( apph ) {
107     appspath = pnd_conf_get_as_char ( apph, PND_APPS_KEY );
108
109     if ( ! appspath ) {
110       appspath = PND_APPS_SEARCHPATH;
111     }
112
113     overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
114
115     if ( ! overridespath ) {
116       overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
117     }
118
119   } else {
120     // couldn't find a useful app search path so use the default
121     appspath = PND_APPS_SEARCHPATH;
122     overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
123   }
124
125   // attempt to figure out where to drop dotfiles
126   pnd_conf_handle desktoph;
127
128   desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );
129
130   if ( desktoph ) {
131     dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DOTDESKTOP_KEY );
132
133     if ( ! dotdesktoppath ) {
134       dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
135     }
136
137   } else {
138     dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
139   }
140
141   /* startup
142    */
143
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 );
148   }
149
150   /* set up notifies
151    */
152   searchpath = appspath;
153
154   nh = pnd_notify_init();
155
156   if ( ! nh ) {
157     if ( ! g_daemon_mode ) {
158       printf ( "INOTIFY failed to init.\n" );
159     }
160     exit ( -1 );
161   }
162
163   if ( ! g_daemon_mode ) {
164     printf ( "INOTIFY is up.\n" );
165   }
166
167   SEARCHPATH_PRE
168   {
169
170     if ( ! g_daemon_mode ) {
171       printf ( "Watching path '%s' and its descendents.\n", buffer );
172     }
173
174     pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
175
176   }
177   SEARCHPATH_POST
178
179   /* daemon main loop
180    */
181   while ( 1 ) {
182
183     // need to rediscover?
184     if ( scanonlaunch ||
185          pnd_notify_rediscover_p ( nh ) )
186     {
187       pnd_box_handle applist;
188       time_t createtime = time ( NULL ); // all 'new' .destops are created at or after this time; prev are old.
189
190       // if this was a forced scan, lets not do that next iteration
191       if ( scanonlaunch ) {
192         scanonlaunch = 0;
193       }
194
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 );
201       }
202
203       // run the discovery
204       applist = pnd_disco_search ( appspath, overridespath );
205
206       // list the found apps (if any)
207       if ( applist ) {
208         pnd_disco_t *d = pnd_box_get_head ( applist );
209
210         while ( d ) {
211
212           if ( ! g_daemon_mode ) {
213             printf ( "Found app: %s\n", pnd_box_get_key ( d ) );
214           }
215
216           // create the .desktop file
217           if ( pnd_emit_dotdesktop ( dotdesktoppath, d ) ) {
218             // add a watch onto the newly created .desktop?
219 #if 0
220             char buffer [ FILENAME_MAX ];
221             sprintf ( buffer, "%s/%s", dotdesktoppath, d -> unique_id );
222             pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
223 #endif
224           } else {
225             if ( ! g_daemon_mode ) {
226               printf ( "ERROR: Error creating .desktop file for app: %s\n", pnd_box_get_key ( d ) );
227             }
228           }
229
230           // next!
231           d = pnd_box_get_next ( d );
232
233         } // while applist
234
235       } else {
236
237         if ( ! g_daemon_mode ) {
238           printf ( "No applications found in search path\n" );
239         }
240
241       } // got apps?
242
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.
252       {
253         DIR *dir;
254
255         if ( ( dir = opendir ( dotdesktoppath ) ) ) {
256           struct dirent *dirent;
257           struct stat dirs;
258           char buffer [ FILENAME_MAX ];
259
260           while ( ( dirent = readdir ( dir ) ) ) {
261
262             // file is a .desktop?
263             if ( strstr ( dirent -> d_name, ".desktop" ) == NULL ) {
264               continue;
265             }
266
267             // file was previously created by libpnd; check Source= line
268             // TBD
269
270             // file is 'new'?
271             sprintf ( buffer, "%s/%s", dotdesktoppath, dirent -> d_name );
272             if ( stat ( buffer, &dirs ) == 0 ) {
273               if ( dirs.st_mtime >= createtime ) {
274                 continue;
275               }
276             }
277
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 );
282             }
283             unlink ( buffer );
284
285           } // while getting filenames from dir
286
287           closedir ( dir );
288         }
289
290       } // purge old .desktop files
291
292     } // need to rediscover?
293
294     // lets not eat up all the CPU
295     // should use an alarm or select() or something
296     sleep ( interval_secs );
297
298   } // while
299
300   /* shutdown
301    */
302   pnd_notify_shutdown ( nh );
303
304   return ( 0 );
305 }