Added a 'flags' arg to pnd_apps_exec
[pandora-libraries.git] / lib / pnd_apps.c
1
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 */
6
7 #include <sys/types.h> /* for wait */
8 #include <sys/wait.h> /* for wait */
9
10 #include "pnd_container.h"
11 #include "pnd_pxml.h"
12 #include "pnd_apps.h"
13
14 unsigned char pnd_apps_exec ( char *pndrun, char *fullpath, char *unique_id,
15                               char *rel_exec, char *rel_startdir,
16                               unsigned int clockspeed, unsigned int options )
17 {
18   char *argv [ 20 ];
19   int f;
20
21   //printf ( "Entering pnd_apps_exec\n" );
22
23 #if 0
24   printf ( "  runscript: %s\n", pndrun );
25   printf ( "  path: %s\n", fullpath );
26   printf ( "  id: %s\n", unique_id );
27   printf ( "  exec: %s\n", rel_exec );
28   printf ( "  cwd: %s\n", rel_startdir );
29   printf ( "  clock: %u\n", clockspeed );
30 #endif
31
32   memset ( argv, '\0', sizeof(char*) * 20 );
33
34   f = 0;
35   argv [ f++ ] = pndrun;
36   argv [ f++ ] = "-p";
37   argv [ f++ ] = fullpath;
38   argv [ f++ ] = "-e";
39   argv [ f++ ] = rel_exec;
40   argv [ f++ ] = "-s";
41   argv [ f++ ] = rel_startdir;
42   // skip -a (arguments) for now
43
44   //argv [ f++ ] = "-b";
45   //argv [ f++ ] = baename;
46
47   argv [ f++ ] = "-u"; // no union for now
48   argv [ f++ ] = NULL; // for execv
49
50   // debug
51 #if 0
52   int i;
53   for ( i = 0; i < f; i++ ) {
54     printf ( "exec's argv %u [ %s ]\n", i, argv [ i ] );
55   }
56 #endif
57
58   // invoke it!
59
60   if ( ( f = fork() ) < 0 ) {
61     // error forking
62   } else if ( f > 0 ) {
63     // parent
64   } else {
65     // child, do it
66     execv ( pndrun, argv );
67   } 
68
69   // by definition, either error occurred or we are the original application.
70
71   // do we wish to wait until the child process completes? (we don't
72   // care if it crashed, was killed, was suspended, whatever.)
73   if ( options & PND_EXEC_OPTION_BLOCK ) {
74     int status = 0;
75     //waitpid ( f, &status. 0 /* no options */ );
76     wait ( &status );
77   }
78
79   // printf ( "Exiting pnd_apps_exec\n" );
80
81   return ( 1 );
82 }