2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <string.h> /* for memset */
5 #include <unistd.h> /* for fork/exec */
8 #include <sys/types.h> /* for wait */
9 #include <sys/wait.h> /* for wait */
11 #include "pnd_container.h"
14 #include "pnd_logger.h"
16 static char apps_exec_runline [ 1024 ];
18 char *pnd_apps_exec_runline ( void ) {
19 return ( apps_exec_runline );
22 unsigned char pnd_apps_exec_disco ( char *pndrun, pnd_disco_t *app,
23 unsigned int options, void *reserved )
26 char fullpath [ PATH_MAX ] = "";
29 //printf ( "Entering pnd_apps_exec\n" );
35 if ( ! app -> unique_id ) {
39 if ( ! app -> exec ) {
43 // determine path to pnd-file
44 sprintf ( fullpath, "%s/%s", app -> object_path, app -> object_filename );
46 // nail down argv for the app
48 memset ( argv, '\0', sizeof(char*) * 20 );
51 argv [ f++ ] = pndrun;
53 argv [ f++ ] = fullpath;
55 argv [ f++ ] = app -> exec;
56 if ( app -> startdir ) {
58 argv [ f++ ] = app -> startdir;
60 if ( app -> execargs ) {
62 argv [ f++ ] = app -> execargs;
64 if ( app -> appdata_dirname ) {
66 argv [ f++ ] = app -> appdata_dirname;
69 argv [ f++ ] = app -> unique_id;
71 if ( app -> clockspeed ) {
73 argv [ f++ ] = app -> clockspeed;
76 // skip -a (arguments) for now
78 if ( options & PND_EXEC_OPTION_NOUNION ) {
79 argv [ f++ ] = "-n"; // no union for now
82 if ( options & PND_EXEC_OPTION_NOX11 ) {
83 argv [ f++ ] = "-x"; // shut down X!
87 argv [ f++ ] = NULL; // for execv
90 if ( options & PND_EXEC_OPTION_NORUN ) {
92 bzero ( apps_exec_runline, 1024 );
93 //pnd_log ( PND_LOG_DEFAULT, "Norun %u\n", f );
94 unsigned char quotenext = 0;
95 for ( i = 0; i < ( f - 1 ); i++ ) {
96 //pnd_log ( PND_LOG_DEFAULT, "Norun %u: %s\n", i, argv [ i ] );
98 // add spacing between args
100 strncat ( apps_exec_runline, " ", 1000 );
105 strncat ( apps_exec_runline, "\"", 1000 );
109 strncat ( apps_exec_runline, argv [ i ], 1000 );
113 strncat ( apps_exec_runline, "\"", 1000 );
120 // if this is for -a, we need to wrap with quotes
121 if ( strcmp ( argv [ i ], "-a" ) == 0 ) {
133 for ( i = 0; i < f; i++ ) {
134 printf ( "exec's argv %u [ %s ]\n", i, argv [ i ] );
140 if ( ( f = fork() ) < 0 ) {
142 } else if ( f > 0 ) {
146 execv ( pndrun, argv );
149 // by definition, either error occurred or we are the original application.
151 // do we wish to wait until the child process completes? (we don't
152 // care if it crashed, was killed, was suspended, whatever.)
153 if ( options & PND_EXEC_OPTION_BLOCK ) {
155 waitpid ( f, &status, 0 /* no options */ );
159 // printf ( "Exiting pnd_apps_exec\n" );
164 unsigned char pnd_apps_exec ( char *pndrun, char *fullpath, char *unique_id,
165 char *rel_exec, char *rel_startdir,
167 unsigned int clockspeed, unsigned int options )
170 bzero ( &d, sizeof(pnd_disco_t) );
172 char cpuspeed [ 10 ];
173 sprintf ( cpuspeed, "%u", clockspeed );
175 char hackpath [ PATH_MAX ];
176 strncpy ( hackpath, fullpath, PATH_MAX );
177 char *c = strrchr ( hackpath, '/' );
180 d.object_path = hackpath;
181 d.object_filename = c + 1;
183 d.object_path = fullpath;
186 d.unique_id = unique_id;
188 d.startdir = rel_startdir;
191 d.clockspeed = cpuspeed;
196 return ( pnd_apps_exec_disco ( pndrun, &d, options, NULL ) );
199 void pnd_get_ro_mountpoint ( char *fullpath, char *unique_id, char *r_mountpoint, unsigned int mountpoint_len ) {
201 if ( ! r_mountpoint ) {
205 snprintf ( r_mountpoint, mountpoint_len, "%s/%s/", PND_MOUNT_PATH, unique_id );
210 unsigned char pnd_get_appdata_path ( char *fullpath, char *unique_id, char *r_path, unsigned int path_len ) {
211 // you know, determining the 'mount point' used is sort of a pain in the rear
212 // use df <file>, skip header line, grab first token. Cheap and should work.
213 // (Rather than just assume first two path chunks in path, say, which would be pretty darned
214 // accurate, but whose to say they're not using NFS or crazy mountpoints?)
216 char cmdbuf [ 1024 ];
218 snprintf ( cmdbuf, 1023, "/bin/df %s", fullpath );
220 df = popen ( cmdbuf, "r" );
223 return ( 0 ); // tunefs: you can tune a filesystem but you can't tune a fish
226 // fetch and discard header
227 if ( ! fgets ( cmdbuf, 1023, df ) ) {
232 // grab df result line
233 if ( ! fgets ( cmdbuf, 1023, df ) ) {
241 char *ws = strchr ( cmdbuf, ' ' );
249 if ( r_path && path_len ) {
250 snprintf ( r_path, path_len, "%s/pandora/appdata/%s/", cmdbuf, unique_id );