UNTESTED on device!
[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 ( ! pndrun ) {
24     return ( 0 );
25   }
26
27   if ( ! fullpath ) {
28     return ( 0 );
29   }
30
31   if ( ! unique_id ) {
32     return ( 0 );
33   }
34
35   if ( ! rel_exec ) {
36     return ( 0 );
37   }
38
39 #if 0
40   printf ( "  runscript: %s\n", pndrun );
41   printf ( "  path: %s\n", fullpath );
42   printf ( "  id: %s\n", unique_id );
43   printf ( "  exec: %s\n", rel_exec );
44   printf ( "  cwd: %s\n", rel_startdir );
45   printf ( "  clock: %u\n", clockspeed );
46 #endif
47
48   memset ( argv, '\0', sizeof(char*) * 20 );
49
50   f = 0;
51   argv [ f++ ] = pndrun;
52   argv [ f++ ] = "-p";
53   argv [ f++ ] = fullpath;
54   argv [ f++ ] = "-e";
55   argv [ f++ ] = rel_exec;
56   if ( rel_startdir ) {
57     argv [ f++ ] = "-s";
58     argv [ f++ ] = rel_startdir;
59   }
60   argv [ f++ ] = "-b";
61   argv [ f++ ] = unique_id;
62
63   // skip -a (arguments) for now
64
65   if ( options & PND_EXEC_OPTION_NOUNION ) {
66     argv [ f++ ] = "-n"; // no union for now
67   }
68
69   if ( options & PND_EXEC_OPTION_NOX11 ) {
70     argv [ f++ ] = "-x"; // no union for now
71   }
72
73   // finish
74   argv [ f++ ] = NULL; // for execv
75
76   // debug
77 #if 0
78   int i;
79   for ( i = 0; i < f; i++ ) {
80     printf ( "exec's argv %u [ %s ]\n", i, argv [ i ] );
81   }
82 #endif
83
84   // invoke it!
85
86   if ( ( f = fork() ) < 0 ) {
87     // error forking
88   } else if ( f > 0 ) {
89     // parent
90   } else {
91     // child, do it
92     execv ( pndrun, argv );
93   } 
94
95   // by definition, either error occurred or we are the original application.
96
97   // do we wish to wait until the child process completes? (we don't
98   // care if it crashed, was killed, was suspended, whatever.)
99   if ( options & PND_EXEC_OPTION_BLOCK ) {
100     int status = 0;
101     //waitpid ( f, &status. 0 /* no options */ );
102     wait ( &status );
103   }
104
105   // printf ( "Exiting pnd_apps_exec\n" );
106
107   return ( 1 );
108 }
109
110 void pnd_get_ro_mountpoint ( char *fullpath, char *unique_id, char *r_mountpoint, unsigned int mountpoint_len ) {
111
112   if ( ! r_mountpoint ) {
113     return; // sillyness
114   }
115
116   snprintf ( r_mountpoint, mountpoint_len, "%s/%s/", PND_MOUNT_PATH, unique_id );
117
118   return;
119 }
120
121 unsigned char pnd_get_appdata_path ( char *fullpath, char *unique_id, char *r_path, unsigned int path_len ) {
122   // you know, determining the 'mount point' used is sort of a pain in the rear
123   // use df <file>, skip header line, grab first token. Cheap and should work.
124   // (Rather than just assume first two path chunks in path, say, which would be pretty darned
125   // accurate, but whose to say they're not using NFS or crazy mountpoints?)
126   FILE *df;
127   char cmdbuf [ 1024 ];
128
129   snprintf ( cmdbuf, 1023, "/bin/df %s", fullpath );
130
131   df = popen ( cmdbuf, "r" );
132
133   if ( ! df ) {
134     return ( 0 ); // tunefs: you can tune a filesystem but you can't tune a fish
135   }
136
137   // fetch and discard header
138   if ( ! fgets ( cmdbuf, 1023, df ) ) {
139     pclose ( df );
140     return ( 0 );
141   }
142
143   // grab df result line
144   if ( ! fgets ( cmdbuf, 1023, df ) ) {
145     pclose ( df );
146     return ( 0 );
147   }
148
149   pclose ( df );
150
151   // parse
152   char *ws = strchr ( cmdbuf, ' ' );
153
154   if ( ! ws ) {
155     return ( 0 );
156   }
157
158   *ws = '\0';
159
160   if ( r_path && path_len ) {
161     snprintf ( r_path, path_len, "%s/appdata/%s/", cmdbuf, unique_id );
162   }
163
164   return ( 1 );
165 }