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 */
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 */
15 #include "pnd_container.h"
16 #include "pnd_utility.h"
17 #include "pnd_pndfiles.h"
18 #include "pnd_discovery.h"
20 unsigned char pnd_check_login ( char *r_username, unsigned int maxlen ) {
25 f = fopen ( "/var/run/utmp", "r" );
29 // spin until a non-root user comes along
30 while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) {
32 if ( ( b.ut_type == USER_PROCESS ) &&
33 ( strcmp ( b.ut_user, "root" ) != 0 ) )
36 // ut_user contains the username ..
37 // now we need to find the path to that account.
38 while ( ( pw = getpwent() ) ) {
40 if ( strcmp ( pw -> pw_name, b.ut_user ) == 0 ) {
43 strncpy ( r_username, b.ut_user, maxlen );
48 } // passwd entry matches the utmp entry
50 } // while iteratin across passwd entries
53 } // utmp entry is for a user login
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 ) {
67 char *s = freeable_buffer;
68 char *home = getenv ( "HOME" );
70 printf ( "DEBUG: expand tilde IN: '%s'\n", freeable_buffer );
71 printf ( "DEBUG: $HOME was %s\n", home );
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..
83 static char *florp = NULL;
86 f = fopen ( "/var/run/utmp", "r" );
90 while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) {
92 if ( b.ut_type == USER_PROCESS ) {
94 // ut_user contains the username ..
95 // now we need to find the path to that account.
96 while ( ( pw = getpwent() ) ) {
98 if ( strcmp ( pw -> pw_name, b.ut_user ) == 0 ) {
100 // aight, we've got a logged in user and have matched it to
101 // passwd entry, so can construct the appropriate path
106 florp = strdup ( pw -> pw_dir );
109 printf ( " DEBUG: home (for %s) is %s (from %u)\n", b.ut_user, home, b.ut_type );
111 } // passwd entry matches the utmp entry
113 } // while iteratin across passwd entries
116 } // utmp entry is for a user login
126 return ( s ); // can't succeed
129 printf ( "DEBUG: entering while (%s) with home (%s)\n", s, home );
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 );
138 strcat ( temp, home );
139 // copy stuff after tilde in
140 strcat ( temp, p + 1 );
144 } // while finding matches
146 printf ( "DEBUG: expand tilde OUT: '%s'\n", s );
151 void pnd_exec_no_wait_1 ( char *fullpath, char *arg1 ) {
154 if ( ( i = fork() ) < 0 ) {
155 printf ( "ERROR: Couldn't fork()\n" );
160 return; // parent process, don't care
163 // child process, do something
165 execl ( fullpath, fullpath, arg1, (char*) NULL );
167 execl ( fullpath, fullpath, (char*) NULL );
170 // getting here is an error
171 //printf ( "Error attempting to run %s\n", fullpath );
176 pnd_pxml_handle pnd_pxml_get_by_path ( char *fullpath ) {
177 unsigned char valid = pnd_object_type_unknown;
178 pnd_pxml_handle pxmlh = 0;
180 // WARN: this is way too close to callback in pnd_disco .. should be refactored!
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;
188 // if not a file of interest, just keep looking until we run out
193 // potentially a valid application
194 if ( valid == pnd_object_type_directory ) {
195 pxmlh = pnd_pxml_fetch ( (char*) fullpath );
197 } else if ( valid == pnd_object_type_pnd ) {
199 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
202 f = fopen ( fullpath, "r" );
204 // try to locate the PXML portion
205 if ( ! pnd_pnd_seek_pxml ( f ) ) {
207 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
210 // accrue it into a buffer
211 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
216 // by now, we have <PXML> .. </PXML>, try to parse..
217 pxmlh = pnd_pxml_fetch_buffer ( (char*) fullpath, pxmlbuf );
229 unsigned char pnd_determine_mountpoint ( char *fullpath, char *r_mountpoint, unsigned char mountpoint_len ) {
231 // just cheap it, and call df like an idiot.
233 // Filesystem 1K-blocks Used Available Use% Mounted on
235 char cmd [ PATH_MAX ];
237 char inbuf [ PATH_MAX ];
239 sprintf ( cmd, "/bin/df %s 2>/dev/null", fullpath );
241 if ( ( p = popen ( cmd, "r" ) ) ) {
243 // ignore title line; we really shoudl analyze it to figure out which column, but we make assumptions..
244 fgets ( inbuf, PATH_MAX, p );
246 if ( ! fgets ( inbuf, PATH_MAX, p ) ) {
254 char crap [ PATH_MAX ];
255 char mount [ PATH_MAX ];
256 if ( sscanf ( inbuf, "%s %s %s %s %s %s", crap, crap, crap, crap, crap, mount ) != 6 ) {
260 if ( strlen ( mount ) < mountpoint_len ) {
261 strcpy ( r_mountpoint, mount );
272 // can we even stat this file?
273 if ( stat ( fullpath, &fooby ) == 0 ) {
274 //dev_t st_dev; /* ID of device containing file */
275 //dev_t st_rdev; /* device ID (if special file) */
277 dev_t mount = fooby.st_dev;
279 DIR *d = opendir ( "/dev" );
283 char path [ FILENAME_MAX ];
285 while ( de = readdir ( d ) ) {
286 sprintf ( path, "/dev/%s", de -> d_name );
288 if ( stat ( path, &fooby ) == 0 ) {
290 // 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
291 if ( fooby.st_rdev == mount ) {
292 printf ( "Device: %s\n", path );