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 */
13 #include "pnd_container.h"
14 #include "pnd_utility.h"
15 #include "pnd_pndfiles.h"
16 #include "pnd_discovery.h"
18 // a generalized variable-substitution routine might be nice; for now we need a quick tilde one,
19 // so here goes. Brute force ftw!
20 char *pnd_expand_tilde ( char *freeable_buffer ) {
22 char *s = freeable_buffer;
23 char *home = getenv ( "HOME" );
25 //printf ( "expand tilde IN: '%s'\n", freeable_buffer );
26 //printf ( " home was %s\n", home );
28 // well, as pndnotifyd (etc) may be running as _root_, while the user is logged in
29 // as 'pandora' or god knows what, this could be problematic. Other parts of the lib
30 // use wordexp() for shell-like expansion, but this funciton is (at least) used by
31 // pndnotifyd for determination of the .desktop emit path, so we need ~ to expand
32 // to the _actual user_ rather than root's homedir. Rather than run 'users' or 'who'
33 // or the like, we'll just cut to the chase..
38 static char *florp = NULL;
41 f = fopen ( "/var/run/utmp", "r" );
45 while ( fread ( &b, sizeof(struct utmp), 1, f ) == 1 ) {
47 if ( b.ut_type == USER_PROCESS ) {
49 // ut_user contains the username ..
50 // now we need to find the path to that account.
51 while ( ( pw = getpwent() ) ) {
53 if ( strcmp ( pw -> pw_name, b.ut_user ) == 0 ) {
55 // aight, we've got a logged in user and have matched it to
56 // passwd entry, so can construct the appropriate path
61 florp = strdup ( pw -> pw_dir );
64 //printf ( " home is %s (from %u)\n", home, b.ut_type );
66 } // passwd entry matches the utmp entry
68 } // while iteratin across passwd entries
71 } // utmp entry is for a user login
81 return ( s ); // can't succeed
84 while ( ( p = strchr ( s, '~' ) ) ) {
85 char *temp = malloc ( strlen ( s ) + strlen ( home ) + 1 );
86 memset ( temp, '\0', strlen ( s ) + strlen ( home ) + 1 );
87 // copy in stuff prior to ~
88 strncpy ( temp, s, p - s );
90 strcat ( temp, home );
91 // copy stuff after tilde in
92 strcat ( temp, p + 1 );
96 } // while finding matches
98 //printf ( "expand tilde OUT: '%s'\n", s );
103 void pnd_exec_no_wait_1 ( char *fullpath, char *arg1 ) {
106 if ( ( i = fork() ) < 0 ) {
107 printf ( "ERROR: Couldn't fork()\n" );
112 return; // parent process, don't care
115 // child process, do something
117 execl ( fullpath, fullpath, arg1, (char*) NULL );
119 execl ( fullpath, fullpath, (char*) NULL );
122 // getting here is an error
123 //printf ( "Error attempting to run %s\n", fullpath );
128 pnd_pxml_handle pnd_pxml_get_by_path ( char *fullpath ) {
129 unsigned char valid = pnd_object_type_unknown;
130 pnd_pxml_handle pxmlh = 0;
132 // WARN: this is way too close to callback in pnd_disco .. should be refactored!
134 if ( strcasestr ( fullpath, PXML_FILENAME ) ) {
135 valid = pnd_object_type_directory;
136 } else if ( strcasestr ( fullpath, PND_PACKAGE_FILEEXT "\0" ) ) {
137 valid = pnd_object_type_pnd;
140 // if not a file of interest, just keep looking until we run out
145 // potentially a valid application
146 if ( valid == pnd_object_type_directory ) {
147 pxmlh = pnd_pxml_fetch ( (char*) fullpath );
149 } else if ( valid == pnd_object_type_pnd ) {
151 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
154 f = fopen ( fullpath, "r" );
156 // try to locate the PXML portion
157 if ( ! pnd_pnd_seek_pxml ( f ) ) {
159 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
162 // accrue it into a buffer
163 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
168 // by now, we have <PXML> .. </PXML>, try to parse..
169 pxmlh = pnd_pxml_fetch_buffer ( (char*) fullpath, pxmlbuf );