2121#undef fputs
2222#undef fputc
2323#undef putw
24+ #undef putp
2425#undef fflush
2526#undef getenv
2627#undef setenv
@@ -215,7 +216,7 @@ static inline const pid_t ios_nextAvailablePid() {
215216inline void ios_storeThreadId (pthread_t thread ) {
216217 // To avoid issues when a command starts a command without forking,
217218 // we only store thread IDs for the first thread of the "process".
218- // fprintf(stderr, "Unlocking pid %x , storing thread %x current value: %x\n", current_pid, thread, thread_ids[current_pid]);
219+ // fprintf(stderr, "Unlocking pid %d , storing thread %x current value: %x\n", current_pid, thread, thread_ids[current_pid]);
219220 if (thread_ids [current_pid ] == -1 ) {
220221 thread_ids [current_pid ] = thread ;
221222 }
@@ -254,8 +255,8 @@ char* libc_getenv(const char* variableName) {
254255}
255256
256257extern void set_session_errno (int n );
257- int ios_setenv (const char * variableName , const char * value , int overwrite ) {
258- if (environment [current_pid ] != NULL ) {
258+ int ios_setenv_pid (const char * variableName , const char * value , int overwrite , int pid ) {
259+ if (environment [pid ] != NULL ) {
259260 if (variableName == NULL ) {
260261 set_session_errno (EINVAL );
261262 return -1 ;
@@ -269,9 +270,9 @@ int ios_setenv(const char* variableName, const char* value, int overwrite) {
269270 set_session_errno (EINVAL );
270271 return -1 ;
271272 }
272- char * * envp = environment [current_pid ];
273+ char * * envp = environment [pid ];
273274 unsigned long varNameLen = strlen (variableName );
274- for (int i = 0 ; i < numVariablesSet [current_pid ]; i ++ ) {
275+ for (int i = 0 ; i < numVariablesSet [pid ]; i ++ ) {
275276 if (envp [i ] == NULL ) { continue ; }
276277 if (strncmp (variableName , envp [i ], varNameLen ) == 0 ) {
277278 if (strlen (envp [i ]) > varNameLen ) {
@@ -286,18 +287,26 @@ int ios_setenv(const char* variableName, const char* value, int overwrite) {
286287 }
287288 }
288289 // Not found so far, add it to the list:
289- int pos = numVariablesSet [current_pid ];
290- environment [current_pid ] = realloc (envp , (numVariablesSet [current_pid ] + 2 ) * sizeof (char * ));
291- environment [current_pid ][pos ] = malloc (strlen (variableName ) + strlen (value ) + 2 );
292- environment [current_pid ][pos + 1 ] = NULL ;
293- sprintf (environment [current_pid ][pos ], "%s=%s" , variableName , value );
294- numVariablesSet [current_pid ] += 1 ;
290+ int pos = numVariablesSet [pid ];
291+ environment [pid ] = realloc (envp , (numVariablesSet [pid ] + 2 ) * sizeof (char * ));
292+ environment [pid ][pos ] = malloc (strlen (variableName ) + strlen (value ) + 2 );
293+ environment [pid ][pos + 1 ] = NULL ;
294+ sprintf (environment [pid ][pos ], "%s=%s" , variableName , value );
295+ numVariablesSet [pid ] += 1 ;
295296 return 0 ;
296297 } else {
297298 return setenv (variableName , value , overwrite );
298299 }
299300}
300301
302+ int ios_setenv_parent (const char * variableName , const char * value , int overwrite ) {
303+ return ios_setenv_pid (variableName , value , overwrite , previousPid [current_pid ]);
304+ }
305+
306+ int ios_setenv (const char * variableName , const char * value , int overwrite ) {
307+ return ios_setenv_pid (variableName , value , overwrite , current_pid );
308+ }
309+
301310int ios_putenv (char * string ) {
302311 if (environment [current_pid ] != NULL ) {
303312 unsigned length ;
@@ -336,10 +345,10 @@ int ios_putenv(char* string) {
336345 }
337346}
338347
339- int ios_unsetenv (const char * variableName ) {
348+ int ios_unsetenv_pid (const char * variableName , int pid ) {
340349 // Someone calls unsetenv once the process has been terminated.
341350 // Best thing to do is erase the environment and return
342- if (environment [current_pid ] != NULL ) {
351+ if (environment [pid ] != NULL ) {
343352 if (variableName == NULL ) {
344353 set_session_errno (EINVAL );
345354 return -1 ;
@@ -353,42 +362,50 @@ int ios_unsetenv(const char* variableName) {
353362 set_session_errno (EINVAL );
354363 return -1 ;
355364 }
356- char * * envp = environment [current_pid ];
365+ char * * envp = environment [pid ];
357366 unsigned long varNameLen = strlen (variableName );
358- for (int i = 0 ; i < numVariablesSet [current_pid ]; i ++ ) {
367+ for (int i = 0 ; i < numVariablesSet [pid ]; i ++ ) {
359368 if (envp [i ] == NULL ) { continue ; }
360369 if (strncmp (variableName , envp [i ], varNameLen ) == 0 ) {
361370 if (strlen (envp [i ]) > varNameLen ) {
362371 if (envp [i ][varNameLen ] == '=' ) {
363372 // This variable is defined in the current environment:
364373 free (envp [i ]);
365374 envp [i ] = NULL ;
366- if (i < numVariablesSet [current_pid ] - 1 ) {
367- for (int j = i ; j < numVariablesSet [current_pid ] - 1 ; j ++ ) {
375+ if (i < numVariablesSet [pid ] - 1 ) {
376+ for (int j = i ; j < numVariablesSet [pid ] - 1 ; j ++ ) {
368377 envp [j ] = envp [j + 1 ];
369378 }
370- envp [numVariablesSet [current_pid ] - 1 ] = NULL ;
379+ envp [numVariablesSet [pid ] - 1 ] = NULL ;
371380 }
372- numVariablesSet [current_pid ] -= 1 ;
373- environment [current_pid ] = realloc (envp , (numVariablesSet [current_pid ] + 1 ) * sizeof (char * ));
381+ numVariablesSet [pid ] -= 1 ;
382+ environment [pid ] = realloc (envp , (numVariablesSet [pid ] + 1 ) * sizeof (char * ));
374383 return 0 ;
375384 }
376385 }
377386 }
378387 }
379388 /*
380- for (int i = 0; i < numVariablesSet[current_pid ]; i++) {
381- char* position = strstr(envp[i],"=");
382- if (strncmp(variableName, envp[i], position - envp[i]) == 0) {
383- }
384- } */
389+ for (int i = 0; i < numVariablesSet[pid ]; i++) {
390+ char* position = strstr(envp[i],"=");
391+ if (strncmp(variableName, envp[i], position - envp[i]) == 0) {
392+ }
393+ } */
385394 // Not found:
386395 return 0 ;
387396 } else {
388397 return unsetenv (variableName );
389398 }
390399}
391400
401+ int ios_unsetenv_parent (const char * variableName ) {
402+ return ios_unsetenv_pid (variableName , previousPid [current_pid ]);
403+ }
404+
405+ int ios_unsetenv (const char * variableName ) {
406+ return ios_unsetenv_pid (variableName , current_pid );
407+ }
408+
392409
393410// store environment variables (called from execve)
394411// Copy the entire environment:
@@ -488,6 +505,8 @@ pid_t ios_currentPid() {
488505 return current_pid ;
489506}
490507
508+ // Note to self: do not redefine getpid() unless you have a way to make it consistent even when a "process" starts a new thread.
509+ // 0MQ and asyncio rely on this.
491510pid_t fork (void ) { return ios_nextAvailablePid (); } // increases current_pid by 1.
492511pid_t ios_fork (void ) { return ios_nextAvailablePid (); } // increases current_pid by 1.
493512pid_t vfork (void ) { return ios_nextAvailablePid (); }
@@ -570,31 +589,72 @@ void vwarnx(const char *fmt, va_list args)
570589}
571590// void err(int eval, const char *fmt, ...);
572591void err (int eval , const char * fmt , ...) {
573- va_list argptr ;
574- va_start (argptr , fmt );
575- vwarn (fmt , argptr );
576- va_end (argptr );
592+ if (fmt != NULL ) {
593+ va_list argptr ;
594+ va_start (argptr , fmt );
595+ vwarn (fmt , argptr );
596+ va_end (argptr );
597+ }
598+ ios_exit (eval );
599+ }
600+ // void errc(int eval, int errorcode, const char *fmt, ...);
601+ void errc (int eval , int errorcode , const char * fmt , ...) {
602+ if (thread_stderr == NULL ) thread_stderr = stderr ;
603+ if (fmt != NULL ) {
604+ va_list argptr ;
605+ va_start (argptr , fmt );
606+ fputs (ios_progname (), thread_stderr );
607+ fputs (": " , thread_stderr );
608+ vfprintf (thread_stderr , fmt , argptr );
609+ fputs (": " , thread_stderr );
610+ fputs (strerror (errorcode ), thread_stderr );
611+ putc ('\n' , thread_stderr );
612+ va_end (argptr );
613+ }
577614 ios_exit (eval );
578615}
579616// void errx(int eval, const char *fmt, ...);
580617void errx (int eval , const char * fmt , ...) {
581- va_list argptr ;
582- va_start (argptr , fmt );
583- vwarnx (fmt , argptr );
584- va_end (argptr );
618+ if (fmt != NULL ) {
619+ va_list argptr ;
620+ va_start (argptr , fmt );
621+ vwarnx (fmt , argptr );
622+ va_end (argptr );
623+ }
585624 ios_exit (eval );
586625}
587626// void warn(const char *fmt, ...);
588627void warn (const char * fmt , ...) {
589- va_list argptr ;
590- va_start (argptr , fmt );
591- vwarn (fmt , argptr );
592- va_end (argptr );
628+ if (fmt != NULL ) {
629+ va_list argptr ;
630+ va_start (argptr , fmt );
631+ vwarn (fmt , argptr );
632+ va_end (argptr );
633+ }
593634}
594635// void warnx(const char *fmt, ...);
595636void warnx (const char * fmt , ...) {
596- va_list argptr ;
597- va_start (argptr , fmt );
598- vwarnx (fmt , argptr );
599- va_end (argptr );
637+ if (fmt != NULL ) {
638+ va_list argptr ;
639+ va_start (argptr , fmt );
640+ vwarnx (fmt , argptr );
641+ va_end (argptr );
642+ }
643+ }
644+ // void warnc(int code, const char *fmt, ...);
645+ void warnc (int code , const char * fmt , ...) {
646+ if (thread_stderr == NULL ) thread_stderr = stderr ;
647+ fputs (ios_progname (), thread_stderr );
648+ if (fmt != NULL )
649+ {
650+ va_list argptr ;
651+ va_start (argptr , fmt );
652+ fputs (": " , thread_stderr );
653+ vfprintf (thread_stderr , fmt , argptr );
654+ vwarn (fmt , argptr );
655+ va_end (argptr );
656+ }
657+ fputs (": " , thread_stderr );
658+ fputs (strerror (code ), thread_stderr );
659+ putc ('\n' , thread_stderr );
600660}
0 commit comments