Fix for About screen instant-vanish
[pandora-libraries.git] / lib / pnd_utility.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #define __USE_GNU /* for strcasestr */
5 #include <string.h> /* for making ftw.h happy */
6 #include <unistd.h> /* for fork exec */
7
8 #include <utmp.h> /* for expand-tilde below; see commentary for this about-turn */
9 #include <sys/types.h> /* ditto */
10 #include <pwd.h> /* ditto */
11 #include <sys/stat.h> /* for fstat */
12 #include <dirent.h> /* for opendir */
13
14 #include "pnd_pxml.h"
15 #include "pnd_container.h"
16 #include "pnd_utility.h"
17 #include "pnd_pndfiles.h"
18 #include "pnd_discovery.h"
19
20 unsigned char pnd_check_login ( char *r_username, unsigned int maxlen ) {
21   FILE *f;
22   struct utmp b;
23   struct passwd *pw;
24
25   f = fopen ( "/var/run/utmp", "r" );
26
27   if ( f ) {
28
29     // spin until a non-root user comes along
30     while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) {
31
32       if ( ( b.ut_type == USER_PROCESS ) &&
33            ( strcmp ( b.ut_user, "root" ) != 0 ) )
34       {
35
36         // ut_user contains the username ..
37         // now we need to find the path to that account.
38         while ( ( pw = getpwent() ) ) {
39
40           if ( strcmp ( pw -> pw_name, b.ut_user ) == 0 ) {
41
42             if ( r_username ) {
43               strncpy ( r_username, b.ut_user, maxlen );
44             }
45
46             fclose ( f );
47             return ( 1 );
48           } // passwd entry matches the utmp entry
49
50         } // while iteratin across passwd entries
51         endpwent();
52
53       } // utmp entry is for a user login
54
55     } // while
56
57     fclose ( f );
58   } // opened?
59
60   return ( 0 );
61 }
62
63 // a generalized variable-substitution routine might be nice; for now we need a quick tilde one,
64 // so here goes. Brute force ftw!
65 char *pnd_expand_tilde ( char *freeable_buffer ) {
66   char *p;
67   char *s = freeable_buffer;
68   char *home = getenv ( "HOME" );
69
70   //printf ( "DEBUG: expand tilde IN: '%s'\n", freeable_buffer );
71   //printf ( "DEBUG:  $HOME was %s\n", home );
72
73   // well, as pndnotifyd (etc) may be running as _root_, while the user is logged in
74   // as 'pandora' or god knows what, this could be problematic. Other parts of the lib
75   // use wordexp() for shell-like expansion, but this funciton is (at least) used by
76   // pndnotifyd for determination of the .desktop emit path, so we need ~ to expand
77   // to the _actual user_ rather than root's homedir. Rather than run 'users' or 'who'
78   // or the like, we'll just cut to the chase..
79   ///////
80   {
81     FILE *f;
82     struct utmp b;
83     static char *florp = NULL;
84     struct passwd *pw;
85
86     f = fopen ( "/var/run/utmp", "r" );
87
88     if ( f ) {
89
90       while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) {
91
92         if ( b.ut_type == USER_PROCESS ) {
93
94           // ut_user contains the username ..
95           // now we need to find the path to that account.
96           while ( ( pw = getpwent() ) ) {
97
98             if ( strcmp ( pw -> pw_name, b.ut_user ) == 0 ) {
99
100               // aight, we've got a logged in user and have matched it to
101               // passwd entry, so can construct the appropriate path
102
103               if ( florp ) {
104                 free ( florp );
105               }
106               florp = strdup ( pw -> pw_dir );
107
108               home = florp;
109               //printf ( "  DEBUG: home (for %s) is %s (from %u)\n", b.ut_user, home, b.ut_type );
110
111             } // passwd entry matches the utmp entry
112
113           } // while iteratin across passwd entries
114           endpwent();
115
116         } // utmp entry is for a user login
117
118       } // while
119       fclose ( f );
120     } // opened?
121
122   }
123   ///////
124
125   if ( ! home ) {
126     return ( s ); // can't succeed
127   }
128
129   //printf ( "DEBUG: entering while (%s) with home (%s)\n", s, home );
130
131   while ( ( p = strchr ( s, '~' ) ) ) {
132     //printf ( "DEBUG: within while (%s)\n", s );
133     char *temp = malloc ( strlen ( s ) + strlen ( home ) + 1 );
134     memset ( temp, '\0', strlen ( s ) + strlen ( home ) + 1 );
135     // copy in stuff prior to ~
136     strncpy ( temp, s, p - s );
137     // copy tilde in
138     strcat ( temp, home );
139     // copy stuff after tilde in
140     strcat ( temp, p + 1 );
141     // swap ptrs
142     free ( s );
143     s = temp;
144   } // while finding matches
145
146   //printf ( "DEBUG: expand tilde OUT: '%s'\n", s );
147
148   return ( s );
149 }
150
151 void pnd_exec_no_wait_1 ( char *fullpath, char *arg1 ) {
152   int i;
153
154   if ( ( i = fork() ) < 0 ) {
155     printf ( "ERROR: Couldn't fork()\n" );
156     return;
157   }
158
159   if ( i ) {
160     return; // parent process, don't care
161   }
162
163   // child process, do something
164   if ( arg1 ) {
165     execl ( fullpath, fullpath, arg1, (char*) NULL );
166   } else {
167     execl ( fullpath, fullpath, (char*) NULL );
168   }
169
170   // getting here is an error
171   //printf ( "Error attempting to run %s\n", fullpath );
172
173   return;
174 }
175
176 pnd_pxml_handle *pnd_pxml_get_by_path ( char *fullpath ) {
177   unsigned char valid = pnd_object_type_unknown;
178   pnd_pxml_handle *pxmlapps = 0;
179
180   // WARN: this is way too close to callback in pnd_disco .. should be refactored!
181
182   if ( strcasestr ( fullpath, PXML_FILENAME ) ) {
183     valid = pnd_object_type_directory;
184   } else if ( strcasestr ( fullpath, PND_PACKAGE_FILEEXT "\0" ) ) {
185     valid = pnd_object_type_pnd;
186   }
187
188   // if not a file of interest, just keep looking until we run out
189   if ( ! valid ) {
190     return ( 0 );
191   }
192
193   // potentially a valid application
194   if ( valid == pnd_object_type_directory ) {
195     pxmlapps = pnd_pxml_fetch ( (char*) fullpath );
196
197   } else if ( valid == pnd_object_type_pnd ) {
198     FILE *f;
199     char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
200
201     // open it up..
202     f = fopen ( fullpath, "r" );
203
204     // try to locate the PXML portion
205     if ( ! pnd_pnd_seek_pxml ( f ) ) {
206       fclose ( f );
207       return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
208     }
209
210     // accrue it into a buffer
211     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
212       fclose ( f );
213       return ( 0 );
214     }
215
216     // by now, we have <PXML> .. </PXML>, try to parse..
217     pxmlapps = pnd_pxml_fetch_buffer ( (char*) fullpath, pxmlbuf );
218
219     // done with file
220     fclose ( f );
221
222   }
223
224   // ..
225
226   return ( pxmlapps );
227 }
228
229 unsigned char pnd_determine_mountpoint ( char *fullpath, char *r_mountpoint, unsigned int mountpoint_len ) {
230
231   // just cheap it, and call df like an idiot.
232
233   // Filesystem           1K-blocks      Used Available Use% Mounted on
234
235   char cmd [ PATH_MAX ];
236   FILE *p;
237   char inbuf [ PATH_MAX ];
238
239   sprintf ( cmd, "/bin/df %s 2>/dev/null", fullpath );
240
241   if ( ( p = popen ( cmd, "r" ) ) ) {
242
243     // ignore title line; we really shoudl analyze it to figure out which column, but we make assumptions..
244     fgets ( inbuf, PATH_MAX, p );
245
246     if ( ! fgets ( inbuf, PATH_MAX, p ) ) {
247       pclose ( p );
248       return ( 0 );
249     }
250
251     pclose ( p );
252
253     // by now, good
254     char mount [ PATH_MAX ];
255     if ( sscanf ( inbuf, "%*s %*s %*s %*s %*s %s", mount ) != 1 ) {
256       return ( 0 );
257     }
258
259     if ( strlen ( mount ) < mountpoint_len ) {
260       strcpy ( r_mountpoint, mount );
261       return ( 1 );
262     }
263
264   } // if popen
265
266   return ( 0 );
267
268 #if 0
269   struct stat fooby;
270
271   // can we even stat this file?
272   if ( stat ( fullpath, &fooby ) == 0 ) {
273     //dev_t     st_dev;     /* ID of device containing file */
274     //dev_t     st_rdev;    /* device ID (if special file) */
275
276     dev_t mount = fooby.st_dev;
277
278     DIR *d = opendir ( "/dev" );
279
280     if ( d ) {
281       struct dirent *de;
282       char path [ FILENAME_MAX ];
283
284       while ( de = readdir ( d ) ) {
285         sprintf ( path, "/dev/%s", de -> d_name );
286
287         if ( stat ( path, &fooby ) == 0 ) {
288
289           // finally, if we find the same major/minor pair in /dev, as we found for the target file, it means we found the right device
290           if ( fooby.st_rdev == mount ) {
291             printf ( "Device: %s\n", path );
292           }
293
294         } // if
295
296       } // while
297
298     } // opened /dev?
299
300   } // stat
301 #endif
302
303   return ( 0 );
304 }
305
306 unsigned char pnd_filecopy ( char *sourcepath, char *targetpath ) {
307 #define BITLEN (64*1024)
308   FILE *pnd, *target; // pnd == from, since I cribbed the code from pnd_desktop.c :/
309   unsigned char bits [ BITLEN ];
310   unsigned int bitlen;
311
312   pnd = fopen ( sourcepath, "rb" );
313
314   if ( ! pnd ) {
315     return ( 0 );
316   }
317
318   unsigned int len;
319
320   target = fopen ( targetpath, "wb" );
321
322   if ( ! target ) {
323     fclose ( pnd );
324     return ( 0 );
325   }
326
327   fseek ( pnd, 0, SEEK_END );
328   len = ftell ( pnd );
329   fseek ( pnd, 0, SEEK_SET );
330
331   while ( len ) {
332
333     if ( len > (BITLEN) ) {
334       bitlen = (BITLEN);
335     } else {
336       bitlen = len;
337     }
338
339     if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
340       fclose ( pnd );
341       fclose ( target );
342       unlink ( targetpath );
343       return ( 0 );
344     }
345
346     if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
347       fclose ( pnd );
348       fclose ( target );
349       unlink ( targetpath );
350       return ( 0 );
351     }
352
353     len -= bitlen;
354   } // while
355
356   fclose ( pnd );
357   fclose ( target );
358
359   return ( 1 );
360 }