21
21
#undef fputs
22
22
#undef fputc
23
23
#undef putw
24
+ #undef putp
24
25
#undef fflush
25
26
#undef getenv
26
27
#undef setenv
@@ -215,7 +216,7 @@ static inline const pid_t ios_nextAvailablePid() {
215
216
inline void ios_storeThreadId (pthread_t thread ) {
216
217
// To avoid issues when a command starts a command without forking,
217
218
// 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]);
219
220
if (thread_ids [current_pid ] == -1 ) {
220
221
thread_ids [current_pid ] = thread ;
221
222
}
@@ -254,8 +255,8 @@ char* libc_getenv(const char* variableName) {
254
255
}
255
256
256
257
extern 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 ) {
259
260
if (variableName == NULL ) {
260
261
set_session_errno (EINVAL );
261
262
return -1 ;
@@ -269,9 +270,9 @@ int ios_setenv(const char* variableName, const char* value, int overwrite) {
269
270
set_session_errno (EINVAL );
270
271
return -1 ;
271
272
}
272
- char * * envp = environment [current_pid ];
273
+ char * * envp = environment [pid ];
273
274
unsigned long varNameLen = strlen (variableName );
274
- for (int i = 0 ; i < numVariablesSet [current_pid ]; i ++ ) {
275
+ for (int i = 0 ; i < numVariablesSet [pid ]; i ++ ) {
275
276
if (envp [i ] == NULL ) { continue ; }
276
277
if (strncmp (variableName , envp [i ], varNameLen ) == 0 ) {
277
278
if (strlen (envp [i ]) > varNameLen ) {
@@ -286,18 +287,26 @@ int ios_setenv(const char* variableName, const char* value, int overwrite) {
286
287
}
287
288
}
288
289
// 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 ;
295
296
return 0 ;
296
297
} else {
297
298
return setenv (variableName , value , overwrite );
298
299
}
299
300
}
300
301
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
+
301
310
int ios_putenv (char * string ) {
302
311
if (environment [current_pid ] != NULL ) {
303
312
unsigned length ;
@@ -336,10 +345,10 @@ int ios_putenv(char* string) {
336
345
}
337
346
}
338
347
339
- int ios_unsetenv (const char * variableName ) {
348
+ int ios_unsetenv_pid (const char * variableName , int pid ) {
340
349
// Someone calls unsetenv once the process has been terminated.
341
350
// Best thing to do is erase the environment and return
342
- if (environment [current_pid ] != NULL ) {
351
+ if (environment [pid ] != NULL ) {
343
352
if (variableName == NULL ) {
344
353
set_session_errno (EINVAL );
345
354
return -1 ;
@@ -353,42 +362,50 @@ int ios_unsetenv(const char* variableName) {
353
362
set_session_errno (EINVAL );
354
363
return -1 ;
355
364
}
356
- char * * envp = environment [current_pid ];
365
+ char * * envp = environment [pid ];
357
366
unsigned long varNameLen = strlen (variableName );
358
- for (int i = 0 ; i < numVariablesSet [current_pid ]; i ++ ) {
367
+ for (int i = 0 ; i < numVariablesSet [pid ]; i ++ ) {
359
368
if (envp [i ] == NULL ) { continue ; }
360
369
if (strncmp (variableName , envp [i ], varNameLen ) == 0 ) {
361
370
if (strlen (envp [i ]) > varNameLen ) {
362
371
if (envp [i ][varNameLen ] == '=' ) {
363
372
// This variable is defined in the current environment:
364
373
free (envp [i ]);
365
374
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 ++ ) {
368
377
envp [j ] = envp [j + 1 ];
369
378
}
370
- envp [numVariablesSet [current_pid ] - 1 ] = NULL ;
379
+ envp [numVariablesSet [pid ] - 1 ] = NULL ;
371
380
}
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 * ));
374
383
return 0 ;
375
384
}
376
385
}
377
386
}
378
387
}
379
388
/*
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
+ } */
385
394
// Not found:
386
395
return 0 ;
387
396
} else {
388
397
return unsetenv (variableName );
389
398
}
390
399
}
391
400
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
+
392
409
393
410
// store environment variables (called from execve)
394
411
// Copy the entire environment:
@@ -488,6 +505,8 @@ pid_t ios_currentPid() {
488
505
return current_pid ;
489
506
}
490
507
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.
491
510
pid_t fork (void ) { return ios_nextAvailablePid (); } // increases current_pid by 1.
492
511
pid_t ios_fork (void ) { return ios_nextAvailablePid (); } // increases current_pid by 1.
493
512
pid_t vfork (void ) { return ios_nextAvailablePid (); }
@@ -570,31 +589,72 @@ void vwarnx(const char *fmt, va_list args)
570
589
}
571
590
// void err(int eval, const char *fmt, ...);
572
591
void 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
+ }
577
614
ios_exit (eval );
578
615
}
579
616
// void errx(int eval, const char *fmt, ...);
580
617
void 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
+ }
585
624
ios_exit (eval );
586
625
}
587
626
// void warn(const char *fmt, ...);
588
627
void 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
+ }
593
634
}
594
635
// void warnx(const char *fmt, ...);
595
636
void 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 );
600
660
}
0 commit comments