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 if ( options & PND_EXEC_OPTION_INFO ) {
56 argv [ f++ ] = ((pnd_apps_exec_info_t*)reserved) -> viewer;
58 argv [ f++ ] = app -> exec;
60 if ( app -> startdir ) {
62 argv [ f++ ] = app -> startdir;
64 if ( options & PND_EXEC_OPTION_INFO ) {
65 if ( ((pnd_apps_exec_info_t*)reserved) -> args ) {
67 argv [ f++ ] = ((pnd_apps_exec_info_t*)reserved) -> args;
70 if ( app -> execargs ) {
72 argv [ f++ ] = app -> execargs;
75 if ( app -> appdata_dirname ) {
77 argv [ f++ ] = app -> appdata_dirname;
80 argv [ f++ ] = app -> unique_id;
82 if ( app -> clockspeed ) {
84 argv [ f++ ] = app -> clockspeed;
87 // skip -a (arguments) for now
89 if ( options & PND_EXEC_OPTION_NOUNION ) {
90 argv [ f++ ] = "-n"; // no union for now
93 if ( options & PND_EXEC_OPTION_NOX11 ) {
94 argv [ f++ ] = "-x"; // shut down X!
98 argv [ f++ ] = NULL; // for execv
101 if ( options & PND_EXEC_OPTION_NORUN ) {
103 bzero ( apps_exec_runline, 1024 );
104 //pnd_log ( PND_LOG_DEFAULT, "Norun %u\n", f );
105 unsigned char quotenext = 0;
106 for ( i = 0; i < ( f - 1 ); i++ ) {
107 //pnd_log ( PND_LOG_DEFAULT, "Norun %u: %s\n", i, argv [ i ] );
109 // add spacing between args
111 strncat ( apps_exec_runline, " ", 1000 );
116 strncat ( apps_exec_runline, "\"", 1000 );
120 strncat ( apps_exec_runline, argv [ i ], 1000 );
124 strncat ( apps_exec_runline, "\"", 1000 );
131 // deprecated; need to handle spaces in some additional args
132 // if ( strcmp ( argv [ i ], "-a" ) == 0 ) {
133 // if this is for -a, we need to wrap with quotes
135 // to allow spaces in filenames we have to add quotes around most terms!
136 // terms with quotes:
137 // -a additional arguments
138 // -p fullpath to pnd
139 // -e name of execuatable inside the pnd
141 // -b name for the appdir
143 if ( ( strcmp ( argv [ i ], "-a" ) == 0 ) ||
144 ( strcmp ( argv [ i ], "-p" ) == 0 ) ||
145 ( strcmp ( argv [ i ], "-e" ) == 0 ) ||
146 ( strcmp ( argv [ i ], "-s" ) == 0 ) ||
147 ( strcmp ( argv [ i ], "-b" ) == 0 ) )
161 for ( i = 0; i < f; i++ ) {
162 printf ( "exec's argv %u [ %s ]\n", i, argv [ i ] );
168 if ( ( f = fork() ) < 0 ) {
170 } else if ( f > 0 ) {
174 execv ( pndrun, argv );
177 // by definition, either error occurred or we are the original application.
179 // do we wish to wait until the child process completes? (we don't
180 // care if it crashed, was killed, was suspended, whatever.)
181 if ( options & PND_EXEC_OPTION_BLOCK ) {
183 waitpid ( f, &status, 0 /* no options */ );
187 // printf ( "Exiting pnd_apps_exec\n" );
192 unsigned char pnd_apps_exec ( char *pndrun, char *fullpath, char *unique_id,
193 char *rel_exec, char *rel_startdir,
195 unsigned int clockspeed, unsigned int options )
198 bzero ( &d, sizeof(pnd_disco_t) );
200 char cpuspeed [ 10 ];
201 sprintf ( cpuspeed, "%u", clockspeed );
203 char hackpath [ PATH_MAX ];
204 strncpy ( hackpath, fullpath, PATH_MAX );
205 char *c = strrchr ( hackpath, '/' );
208 d.object_path = hackpath;
209 d.object_filename = c + 1;
211 d.object_path = fullpath;
214 d.unique_id = unique_id;
216 d.startdir = rel_startdir;
219 d.clockspeed = cpuspeed;
224 return ( pnd_apps_exec_disco ( pndrun, &d, options, NULL ) );
227 void pnd_get_ro_mountpoint ( char *fullpath, char *unique_id, char *r_mountpoint, unsigned int mountpoint_len ) {
229 if ( ! r_mountpoint ) {
233 snprintf ( r_mountpoint, mountpoint_len, "%s/%s/", PND_MOUNT_PATH, unique_id );
238 unsigned char pnd_get_appdata_path ( char *fullpath, char *unique_id, char *r_path, unsigned int path_len ) {
239 // you know, determining the 'mount point' used is sort of a pain in the rear
240 // use df <file>, skip header line, grab first token. Cheap and should work.
241 // (Rather than just assume first two path chunks in path, say, which would be pretty darned
242 // accurate, but whose to say they're not using NFS or crazy mountpoints?)
244 char cmdbuf [ 1024 ];
246 snprintf ( cmdbuf, 1023, "/bin/df %s", fullpath );
248 df = popen ( cmdbuf, "r" );
251 return ( 0 ); // tunefs: you can tune a filesystem but you can't tune a fish
254 // fetch and discard header
255 if ( ! fgets ( cmdbuf, 1023, df ) ) {
260 // grab df result line
261 if ( ! fgets ( cmdbuf, 1023, df ) ) {
269 char *ws = strchr ( cmdbuf, ' ' );
277 if ( r_path && path_len ) {
278 snprintf ( r_path, path_len, "%s/pandora/appdata/%s/", cmdbuf, unique_id );