unix - shell closes file descriptor num 19 -


debugging application have found strange behaviour of shell interpreter(/bin/sh on solaris, /bin/dash in debian). while fork()ing in shell file descriptor number 19(dec) closed shell. in case leads closing of communication socket pair between processes.

looking @ shell sources have found one , this:

for brevity:

/* used input , output of shell */ #define     inio        19 

and

if (input > 0) {     ldup(input, inio);     input = inio; }  ...  static void ldup(int fa, int fb) {     if (fa >= 0) {         if (fa != fb) {             close(fb);             fcntl(fa, 0, fb); /* normal dup */             close(fa);         }         fcntl(fb, 2, 1);    /* autoclose fb */     } } 

so netto closing fd number inio(19);

simple test reproducing:

$ exec 19>&1 $ echo aaa >&19 aaa $ bash -c 'echo aaa >&19' aaa $ dash -c 'echo aaa >&19' dash: 1: syntax error: bad fd number $ ksh -c 'echo aaa >&19' aaa 

the questions are:

  • what reasons strange behavior?
  • what wrong file descriptor 19 ?

19 special because (long ago), maximum number of open files 20, e.g.,

#define _nfile  20 

in stdio.h

in posix, may see other symbols such open_max via sysconf interface.

  • file descriptors count 0, and
  • are assigned in increasing order
  • so "last possible" file descriptor have been 19.
  • if there unused file descriptor, making last "work".

both solaris sh (in particular through solaris 10) , dash date while, , detail noticed not breaking legacy shell scripts mattered (much).


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -