w32proc.c 64.2 KB
Newer Older
1
/* Process support for GNU Emacs on the Microsoft W32 API.
2
   Copyright (C) 1992, 1995, 1999, 2000, 2001 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
20 21 22 23 24 25 26 27 28

   Drew Bliss                   Oct 14, 1993
     Adapted from alarm.c by Tim Fleehart
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <io.h>
29
#include <fcntl.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
30 31
#include <signal.h>

32 33 34 35 36 37 38 39
/* must include CRT headers *before* config.h */
#include "config.h"
#undef signal
#undef wait
#undef spawnve
#undef select
#undef kill

Richard M. Stallman's avatar
Richard M. Stallman committed
40
#include <windows.h>
41 42 43 44
#ifdef __GNUC__
/* This definition is missing from mingw32 headers. */
extern BOOL WINAPI IsValidLocale(LCID, DWORD);
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
45 46

#include "lisp.h"
Geoff Voelker's avatar
Geoff Voelker committed
47
#include "w32.h"
Geoff Voelker's avatar
Geoff Voelker committed
48
#include "w32heap.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
49
#include "systime.h"
50 51
#include "syswait.h"
#include "process.h"
52
#include "syssignal.h"
53
#include "w32term.h"
54

55 56 57 58
/* Control whether spawnve quotes arguments as necessary to ensure
   correct parsing by child process.  Because not all uses of spawnve
   are careful about constructing argv arrays, we make this behaviour
   conditional (off by default). */
59
Lisp_Object Vw32_quote_process_args;
60

61 62
/* Control whether create_child causes the process' window to be
   hidden.  The default is nil. */
63
Lisp_Object Vw32_start_process_show_window;
64

Geoff Voelker's avatar
Geoff Voelker committed
65 66 67 68 69 70
/* Control whether create_child causes the process to inherit Emacs'
   console window, or be given a new one of its own.  The default is
   nil, to allow multiple DOS programs to run on Win95.  Having separate
   consoles also allows Emacs to cleanly terminate process groups.  */
Lisp_Object Vw32_start_process_share_console;

71 72 73 74 75
/* Control whether create_child cause the process to inherit Emacs'
   error mode setting.  The default is t, to minimize the possibility of
   subprocesses blocking when accessing unmounted drives.  */
Lisp_Object Vw32_start_process_inherit_error_mode;

76 77 78
/* Time to sleep before reading from a subprocess output pipe - this
   avoids the inefficiency of frequently reading small amounts of data.
   This is primarily necessary for handling DOS processes on Windows 95,
79
   but is useful for W32 processes on both Windows 95 and NT as well.  */
80
Lisp_Object Vw32_pipe_read_delay;
81

82 83
/* Control conversion of upper case file names to lower case.
   nil means no, t means yes. */
84
Lisp_Object Vw32_downcase_file_names;
85

Geoff Voelker's avatar
Geoff Voelker committed
86 87 88 89 90 91 92 93 94 95 96 97
/* Control whether stat() attempts to generate fake but hopefully
   "accurate" inode values, by hashing the absolute truenames of files.
   This should detect aliasing between long and short names, but still
   allows the possibility of hash collisions.  */
Lisp_Object Vw32_generate_fake_inodes;

/* Control whether stat() attempts to determine file type and link count
   exactly, at the expense of slower operation.  Since true hard links
   are supported on NTFS volumes, this is only relevant on NT.  */
Lisp_Object Vw32_get_true_file_attributes;

Lisp_Object Qhigh, Qlow;
98

Richard M. Stallman's avatar
Richard M. Stallman committed
99
#ifdef EMACSDEBUG
100
void _DebPrint (const char *fmt, ...)
Richard M. Stallman's avatar
Richard M. Stallman committed
101
{
102
  char buf[1024];
Richard M. Stallman's avatar
Richard M. Stallman committed
103 104 105 106 107 108 109 110 111
  va_list args;

  va_start (args, fmt);
  vsprintf (buf, fmt, args);
  va_end (args);
  OutputDebugString (buf);
}
#endif

112
typedef void (_CALLBACK_ *signal_handler)(int);
Richard M. Stallman's avatar
Richard M. Stallman committed
113 114 115 116 117 118

/* Signal handlers...SIG_DFL == 0 so this is initialized correctly.  */
static signal_handler sig_handlers[NSIG];

/* Fake signal implementation to record the SIGCHLD handler.  */
signal_handler 
119
sys_signal (int sig, signal_handler handler)
Richard M. Stallman's avatar
Richard M. Stallman committed
120 121 122 123 124 125 126 127 128 129 130 131 132
{
  signal_handler old;
  
  if (sig != SIGCHLD)
    {
      errno = EINVAL;
      return SIG_ERR;
    }
  old = sig_handlers[sig];
  sig_handlers[sig] = handler;
  return old;
}

133 134 135 136 137 138 139 140 141 142
/* Defined in <process.h> which conflicts with the local copy */
#define _P_NOWAIT 1

/* Child process management list.  */
int child_proc_count = 0;
child_process child_procs[ MAX_CHILDREN ];
child_process *dead_child = NULL;

DWORD WINAPI reader_thread (void *arg);

Richard M. Stallman's avatar
Richard M. Stallman committed
143
/* Find an unused process slot.  */
144
child_process *
Richard M. Stallman's avatar
Richard M. Stallman committed
145 146 147
new_child (void)
{
  child_process *cp;
148
  DWORD id;
Richard M. Stallman's avatar
Richard M. Stallman committed
149 150 151
  
  for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
    if (!CHILD_ACTIVE (cp))
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
      goto Initialise;
  if (child_proc_count == MAX_CHILDREN)
    return NULL;
  cp = &child_procs[child_proc_count++];

 Initialise:
  memset (cp, 0, sizeof(*cp));
  cp->fd = -1;
  cp->pid = -1;
  cp->procinfo.hProcess = NULL;
  cp->status = STATUS_READ_ERROR;

  /* use manual reset event so that select() will function properly */
  cp->char_avail = CreateEvent (NULL, TRUE, FALSE, NULL);
  if (cp->char_avail)
    {
      cp->char_consumed = CreateEvent (NULL, FALSE, FALSE, NULL);
      if (cp->char_consumed)
        {
	  cp->thrd = CreateThread (NULL, 1024, reader_thread, cp, 0, &id);
	  if (cp->thrd)
	    return cp;
	}
    }
  delete_child (cp);
  return NULL;
}

void 
delete_child (child_process *cp)
{
  int i;

  /* Should not be deleting a child that is still needed. */
  for (i = 0; i < MAXDESC; i++)
    if (fd_info[i].cp == cp)
      abort ();

  if (!CHILD_ACTIVE (cp))
    return;

  /* reap thread if necessary */
  if (cp->thrd)
    {
      DWORD rc;

      if (GetExitCodeThread (cp->thrd, &rc) && rc == STILL_ACTIVE)
        {
	  /* let the thread exit cleanly if possible */
	  cp->status = STATUS_READ_ERROR;
	  SetEvent (cp->char_consumed);
	  if (WaitForSingleObject (cp->thrd, 1000) != WAIT_OBJECT_0)
	    {
	      DebPrint (("delete_child.WaitForSingleObject (thread) failed "
			 "with %lu for fd %ld\n", GetLastError (), cp->fd));
	      TerminateThread (cp->thrd, 0);
	    }
	}
      CloseHandle (cp->thrd);
      cp->thrd = NULL;
    }
  if (cp->char_avail)
    {
      CloseHandle (cp->char_avail);
      cp->char_avail = NULL;
    }
  if (cp->char_consumed)
    {
      CloseHandle (cp->char_consumed);
      cp->char_consumed = NULL;
    }

  /* update child_proc_count (highest numbered slot in use plus one) */
  if (cp == child_procs + child_proc_count - 1)
    {
      for (i = child_proc_count-1; i >= 0; i--)
	if (CHILD_ACTIVE (&child_procs[i]))
	  {
	    child_proc_count = i + 1;
	    break;
	  }
    }
  if (i < 0)
    child_proc_count = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
236 237 238 239 240 241 242
}

/* Find a child by pid.  */
static child_process *
find_child_pid (DWORD pid)
{
  child_process *cp;
243

Richard M. Stallman's avatar
Richard M. Stallman committed
244 245 246 247 248 249 250
  for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
    if (CHILD_ACTIVE (cp) && pid == cp->pid)
      return cp;
  return NULL;
}


251 252 253 254
/* Thread proc for child process and socket reader threads. Each thread
   is normally blocked until woken by select() to check for input by
   reading one char.  When the read completes, char_avail is signalled
   to wake up the select emulator and the thread blocks itself again. */
Richard M. Stallman's avatar
Richard M. Stallman committed
255 256 257 258 259 260 261 262 263
DWORD WINAPI 
reader_thread (void *arg)
{
  child_process *cp;
  
  /* Our identity */
  cp = (child_process *)arg;
  
  /* We have to wait for the go-ahead before we can start */
Geoff Voelker's avatar
Geoff Voelker committed
264 265
  if (cp == NULL
      || WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
266 267
    return 1;

Richard M. Stallman's avatar
Richard M. Stallman committed
268 269
  for (;;)
    {
270 271 272 273 274 275
      int rc;

      rc = _sys_read_ahead (cp->fd);

      /* The name char_avail is a misnomer - it really just means the
	 read-ahead has completed, whether successfully or not. */
Richard M. Stallman's avatar
Richard M. Stallman committed
276 277 278 279
      if (!SetEvent (cp->char_avail))
        {
	  DebPrint (("reader_thread.SetEvent failed with %lu for fd %ld\n",
		     GetLastError (), cp->fd));
280 281 282 283 284
	  return 1;
	}

      if (rc == STATUS_READ_ERROR)
	return 1;
Richard M. Stallman's avatar
Richard M. Stallman committed
285 286
        
      /* If the read died, the child has died so let the thread die */
287
      if (rc == STATUS_READ_FAILED)
Richard M. Stallman's avatar
Richard M. Stallman committed
288 289 290 291 292 293 294 295 296 297 298 299 300
	break;
        
      /* Wait until our input is acknowledged before reading again */
      if (WaitForSingleObject (cp->char_consumed, INFINITE) != WAIT_OBJECT_0)
        {
	  DebPrint (("reader_thread.WaitForSingleObject failed with "
		     "%lu for fd %ld\n", GetLastError (), cp->fd));
	  break;
        }
    }
  return 0;
}

Geoff Voelker's avatar
Geoff Voelker committed
301 302 303 304 305
/* To avoid Emacs changing directory, we just record here the directory
   the new process should start in.  This is set just before calling
   sys_spawnve, and is not generally valid at any other time.  */
static char * process_dir;

Richard M. Stallman's avatar
Richard M. Stallman committed
306
static BOOL 
307
create_child (char *exe, char *cmdline, char *env, int is_gui_app,
308
	      int * pPid, child_process *cp)
Richard M. Stallman's avatar
Richard M. Stallman committed
309 310 311
{
  STARTUPINFO start;
  SECURITY_ATTRIBUTES sec_attrs;
312
#if 0
Richard M. Stallman's avatar
Richard M. Stallman committed
313
  SECURITY_DESCRIPTOR sec_desc;
314
#endif
315
  DWORD flags;
Geoff Voelker's avatar
Geoff Voelker committed
316
  char dir[ MAXPATHLEN ];
Richard M. Stallman's avatar
Richard M. Stallman committed
317
  
318
  if (cp == NULL) abort ();
Richard M. Stallman's avatar
Richard M. Stallman committed
319 320 321 322
  
  memset (&start, 0, sizeof (start));
  start.cb = sizeof (start);
  
323
#ifdef HAVE_NTGUI
324
  if (NILP (Vw32_start_process_show_window) && !is_gui_app)
325 326 327
    start.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  else
    start.dwFlags = STARTF_USESTDHANDLES;
328 329 330 331 332 333 334
  start.wShowWindow = SW_HIDE;

  start.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
  start.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
  start.hStdError = GetStdHandle (STD_ERROR_HANDLE);
#endif /* HAVE_NTGUI */

335
#if 0
Richard M. Stallman's avatar
Richard M. Stallman committed
336 337
  /* Explicitly specify no security */
  if (!InitializeSecurityDescriptor (&sec_desc, SECURITY_DESCRIPTOR_REVISION))
338
    goto EH_Fail;
Richard M. Stallman's avatar
Richard M. Stallman committed
339
  if (!SetSecurityDescriptorDacl (&sec_desc, TRUE, NULL, FALSE))
340
    goto EH_Fail;
341
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
342
  sec_attrs.nLength = sizeof (sec_attrs);
343
  sec_attrs.lpSecurityDescriptor = NULL /* &sec_desc */;
Richard M. Stallman's avatar
Richard M. Stallman committed
344 345
  sec_attrs.bInheritHandle = FALSE;
  
Geoff Voelker's avatar
Geoff Voelker committed
346 347
  strcpy (dir, process_dir);
  unixtodos_filename (dir);
348 349 350 351 352 353

  flags = (!NILP (Vw32_start_process_share_console)
	   ? CREATE_NEW_PROCESS_GROUP
	   : CREATE_NEW_CONSOLE);
  if (NILP (Vw32_start_process_inherit_error_mode))
    flags |= CREATE_DEFAULT_ERROR_MODE;
Richard M. Stallman's avatar
Richard M. Stallman committed
354
  if (!CreateProcess (exe, cmdline, &sec_attrs, NULL, TRUE,
355
		      flags, env, dir, &start, &cp->procinfo))
356 357 358 359 360 361 362 363 364 365 366 367
    goto EH_Fail;

  cp->pid = (int) cp->procinfo.dwProcessId;

  /* Hack for Windows 95, which assigns large (ie negative) pids */
  if (cp->pid < 0)
    cp->pid = -cp->pid;

  /* pid must fit in a Lisp_Int */
  cp->pid = (cp->pid & VALMASK);

  *pPid = cp->pid;
Geoff Voelker's avatar
Geoff Voelker committed
368

Richard M. Stallman's avatar
Richard M. Stallman committed
369
  return TRUE;
Geoff Voelker's avatar
Geoff Voelker committed
370

Richard M. Stallman's avatar
Richard M. Stallman committed
371
 EH_Fail:
372
  DebPrint (("create_child.CreateProcess failed: %ld\n", GetLastError()););
Richard M. Stallman's avatar
Richard M. Stallman committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  return FALSE;
}

/* create_child doesn't know what emacs' file handle will be for waiting
   on output from the child, so we need to make this additional call
   to register the handle with the process
   This way the select emulator knows how to match file handles with
   entries in child_procs.  */
void 
register_child (int pid, int fd)
{
  child_process *cp;
  
  cp = find_child_pid (pid);
  if (cp == NULL)
    {
      DebPrint (("register_child unable to find pid %lu\n", pid));
      return;
    }
  
#ifdef FULL_DEBUG
  DebPrint (("register_child registered fd %d with pid %lu\n", fd, pid));
#endif
  
  cp->fd = fd;

399 400 401 402 403 404
  /* thread is initially blocked until select is called; set status so
     that select will release thread */
  cp->status = STATUS_READ_ACKNOWLEDGED;

  /* attach child_process to fd_info */
  if (fd_info[fd].cp != NULL)
Richard M. Stallman's avatar
Richard M. Stallman committed
405
    {
406 407
      DebPrint (("register_child: fd_info[%d] apparently in use!\n", fd));
      abort ();
Richard M. Stallman's avatar
Richard M. Stallman committed
408
    }
409 410

  fd_info[fd].cp = cp;
Richard M. Stallman's avatar
Richard M. Stallman committed
411 412 413 414 415 416 417
}

/* When a process dies its pipe will break so the reader thread will
   signal failure to the select emulator.
   The select emulator then calls this routine to clean up.
   Since the thread signaled failure we can assume it is exiting.  */
static void 
418
reap_subprocess (child_process *cp)
Richard M. Stallman's avatar
Richard M. Stallman committed
419
{
420
  if (cp->procinfo.hProcess)
Richard M. Stallman's avatar
Richard M. Stallman committed
421
    {
422
      /* Reap the process */
Geoff Voelker's avatar
Geoff Voelker committed
423 424 425 426 427
#ifdef FULL_DEBUG
      /* Process should have already died before we are called.  */
      if (WaitForSingleObject (cp->procinfo.hProcess, 0) != WAIT_OBJECT_0)
	DebPrint (("reap_subprocess: child fpr fd %d has not died yet!", cp->fd));
#endif
428 429 430 431
      CloseHandle (cp->procinfo.hProcess);
      cp->procinfo.hProcess = NULL;
      CloseHandle (cp->procinfo.hThread);
      cp->procinfo.hThread = NULL;
Richard M. Stallman's avatar
Richard M. Stallman committed
432
    }
433 434 435 436 437 438 439

  /* For asynchronous children, the child_proc resources will be freed
     when the last pipe read descriptor is closed; for synchronous
     children, we must explicitly free the resources now because
     register_child has not been called. */
  if (cp->fd == -1)
    delete_child (cp);
Richard M. Stallman's avatar
Richard M. Stallman committed
440 441 442 443 444
}

/* Wait for any of our existing child processes to die
   When it does, close its handle
   Return the pid and fill in the status if non-NULL.  */
445

Richard M. Stallman's avatar
Richard M. Stallman committed
446
int 
447
sys_wait (int *status)
Richard M. Stallman's avatar
Richard M. Stallman committed
448 449 450
{
  DWORD active, retval;
  int nh;
451
  int pid;
Richard M. Stallman's avatar
Richard M. Stallman committed
452 453 454 455 456 457 458
  child_process *cp, *cps[MAX_CHILDREN];
  HANDLE wait_hnd[MAX_CHILDREN];
  
  nh = 0;
  if (dead_child != NULL)
    {
      /* We want to wait for a specific child */
459
      wait_hnd[nh] = dead_child->procinfo.hProcess;
Richard M. Stallman's avatar
Richard M. Stallman committed
460
      cps[nh] = dead_child;
461
      if (!wait_hnd[nh]) abort ();
Richard M. Stallman's avatar
Richard M. Stallman committed
462
      nh++;
Geoff Voelker's avatar
Geoff Voelker committed
463 464
      active = 0;
      goto get_result;
Richard M. Stallman's avatar
Richard M. Stallman committed
465 466 467 468
    }
  else
    {
      for (cp = child_procs+(child_proc_count-1); cp >= child_procs; cp--)
469 470
	/* some child_procs might be sockets; ignore them */
	if (CHILD_ACTIVE (cp) && cp->procinfo.hProcess)
Richard M. Stallman's avatar
Richard M. Stallman committed
471
	  {
472
	    wait_hnd[nh] = cp->procinfo.hProcess;
Richard M. Stallman's avatar
Richard M. Stallman committed
473 474 475 476 477 478 479 480 481 482 483
	    cps[nh] = cp;
	    nh++;
	  }
    }
  
  if (nh == 0)
    {
      /* Nothing to wait on, so fail */
      errno = ECHILD;
      return -1;
    }
Geoff Voelker's avatar
Geoff Voelker committed
484 485 486 487 488 489 490 491

  do
    {
      /* Check for quit about once a second. */
      QUIT;
      active = WaitForMultipleObjects (nh, wait_hnd, FALSE, 1000);
    } while (active == WAIT_TIMEOUT);

Richard M. Stallman's avatar
Richard M. Stallman committed
492 493 494 495 496
  if (active == WAIT_FAILED)
    {
      errno = EBADF;
      return -1;
    }
Geoff Voelker's avatar
Geoff Voelker committed
497 498
  else if (active >= WAIT_OBJECT_0
	   && active < WAIT_OBJECT_0+MAXIMUM_WAIT_OBJECTS)
Richard M. Stallman's avatar
Richard M. Stallman committed
499 500 501
    {
      active -= WAIT_OBJECT_0;
    }
Geoff Voelker's avatar
Geoff Voelker committed
502 503
  else if (active >= WAIT_ABANDONED_0
	   && active < WAIT_ABANDONED_0+MAXIMUM_WAIT_OBJECTS)
Richard M. Stallman's avatar
Richard M. Stallman committed
504 505 506
    {
      active -= WAIT_ABANDONED_0;
    }
Geoff Voelker's avatar
Geoff Voelker committed
507 508 509 510
  else
    abort ();

get_result:
Richard M. Stallman's avatar
Richard M. Stallman committed
511 512 513 514 515 516 517 518 519 520 521 522 523
  if (!GetExitCodeProcess (wait_hnd[active], &retval))
    {
      DebPrint (("Wait.GetExitCodeProcess failed with %lu\n",
		 GetLastError ()));
      retval = 1;
    }
  if (retval == STILL_ACTIVE)
    {
      /* Should never happen */
      DebPrint (("Wait.WaitForMultipleObjects returned an active process\n"));
      errno = EINVAL;
      return -1;
    }
524 525

  /* Massage the exit code from the process to match the format expected
Karl Heuer's avatar
Karl Heuer committed
526
     by the WIFSTOPPED et al macros in syswait.h.  Only WIFSIGNALED and
527 528 529 530 531 532
     WIFEXITED are supported; WIFSTOPPED doesn't make sense under NT.  */

  if (retval == STATUS_CONTROL_C_EXIT)
    retval = SIGINT;
  else
    retval <<= 8;
533
  
Richard M. Stallman's avatar
Richard M. Stallman committed
534
  cp = cps[active];
535 536 537 538
  pid = cp->pid;
#ifdef FULL_DEBUG
  DebPrint (("Wait signaled with process pid %d\n", cp->pid));
#endif
539

Richard M. Stallman's avatar
Richard M. Stallman committed
540 541
  if (status)
    {
542 543 544 545 546 547
      *status = retval;
    }
  else if (synch_process_alive)
    {
      synch_process_alive = 0;

548 549 550 551 552 553
      /* Report the status of the synchronous process.  */
      if (WIFEXITED (retval))
	synch_process_retcode = WRETCODE (retval);
      else if (WIFSIGNALED (retval))
	{
	  int code = WTERMSIG (retval);
554 555
	  char *signame;

556
	  synchronize_system_messages_locale ();
557 558
	  signame = strsignal (code);

559 560 561 562 563
	  if (signame == 0)
	    signame = "unknown";

	  synch_process_death = signame;
	}
564 565

      reap_subprocess (cp);
Richard M. Stallman's avatar
Richard M. Stallman committed
566
    }
Geoff Voelker's avatar
Geoff Voelker committed
567 568

  reap_subprocess (cp);
Richard M. Stallman's avatar
Richard M. Stallman committed
569
  
570
  return pid;
Richard M. Stallman's avatar
Richard M. Stallman committed
571 572
}

Geoff Voelker's avatar
Geoff Voelker committed
573
void
574
w32_executable_type (char * filename, int * is_dos_app, int * is_cygnus_app, int * is_gui_app)
575
{
Geoff Voelker's avatar
Geoff Voelker committed
576 577 578 579 580 581
  file_data executable;
  char * p;
  
  /* Default values in case we can't tell for sure.  */
  *is_dos_app = FALSE;
  *is_cygnus_app = FALSE;
582
  *is_gui_app = FALSE;
Geoff Voelker's avatar
Geoff Voelker committed
583 584 585

  if (!open_input_file (&executable, filename))
    return;
586

Geoff Voelker's avatar
Geoff Voelker committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
  p = strrchr (filename, '.');
  
  /* We can only identify DOS .com programs from the extension. */
  if (p && stricmp (p, ".com") == 0)
    *is_dos_app = TRUE;
  else if (p && (stricmp (p, ".bat") == 0
		 || stricmp (p, ".cmd") == 0))
    {
      /* A DOS shell script - it appears that CreateProcess is happy to
	 accept this (somewhat surprisingly); presumably it looks at
	 COMSPEC to determine what executable to actually invoke.
	 Therefore, we have to do the same here as well. */
      /* Actually, I think it uses the program association for that
	 extension, which is defined in the registry.  */
      p = egetenv ("COMSPEC");
      if (p)
603
	w32_executable_type (p, is_dos_app, is_cygnus_app, is_gui_app);
Geoff Voelker's avatar
Geoff Voelker committed
604 605
    }
  else
606
    {
Geoff Voelker's avatar
Geoff Voelker committed
607 608 609 610
      /* Look for DOS .exe signature - if found, we must also check that
	 it isn't really a 16- or 32-bit Windows exe, since both formats
	 start with a DOS program stub.  Note that 16-bit Windows
	 executables use the OS/2 1.x format. */
611

Geoff Voelker's avatar
Geoff Voelker committed
612 613 614 615 616 617 618 619 620
      IMAGE_DOS_HEADER * dos_header;
      IMAGE_NT_HEADERS * nt_header;

      dos_header = (PIMAGE_DOS_HEADER) executable.file_base;
      if (dos_header->e_magic != IMAGE_DOS_SIGNATURE)
	goto unwind;

      nt_header = (PIMAGE_NT_HEADERS) ((char *) dos_header + dos_header->e_lfanew);

621
      if ((char *) nt_header > (char *) dos_header + executable.size) 
622
	{
Geoff Voelker's avatar
Geoff Voelker committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	  /* Some dos headers (pkunzip) have bogus e_lfanew fields.  */
	  *is_dos_app = TRUE;
	} 
      else if (nt_header->Signature != IMAGE_NT_SIGNATURE
	       && LOWORD (nt_header->Signature) != IMAGE_OS2_SIGNATURE)
  	{
	  *is_dos_app = TRUE;
  	}
      else if (nt_header->Signature == IMAGE_NT_SIGNATURE)
  	{
	  /* Look for cygwin.dll in DLL import list. */
	  IMAGE_DATA_DIRECTORY import_dir =
	    nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
	  IMAGE_IMPORT_DESCRIPTOR * imports;
	  IMAGE_SECTION_HEADER * section;

	  section = rva_to_section (import_dir.VirtualAddress, nt_header);
	  imports = RVA_TO_PTR (import_dir.VirtualAddress, section, executable);

	  for ( ; imports->Name; imports++)
  	    {
	      char * dllname = RVA_TO_PTR (imports->Name, section, executable);

646 647 648 649
	      /* The exact name of the cygwin dll has changed with
	         various releases, but hopefully this will be reasonably
	         future proof.  */
	      if (strncmp (dllname, "cygwin", 6) == 0)
Geoff Voelker's avatar
Geoff Voelker committed
650 651 652 653 654
		{
		  *is_cygnus_app = TRUE;
		  break;
		}
  	    }
655 656 657 658 659

	  /* Check whether app is marked as a console or windowed (aka
             GUI) app.  Accept Posix and OS2 subsytem apps as console
             apps.  */
	  *is_gui_app = (nt_header->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI);
Geoff Voelker's avatar
Geoff Voelker committed
660
  	}
661
    }
Geoff Voelker's avatar
Geoff Voelker committed
662 663 664
  
unwind:
  close_file_data (&executable);
665 666
}

667
int
668
compare_env (const void *strp1, const void *strp2)
669
{
670
  const char *str1 = *(const char **)strp1, *str2 = *(const char **)strp2;
671 672 673

  while (*str1 && *str2 && *str1 != '=' && *str2 != '=')
    {
674 675 676
      /* Sort order in command.com/cmd.exe is based on uppercasing
         names, so do the same here.  */
      if (toupper (*str1) > toupper (*str2))
677
	return 1;
678
      else if (toupper (*str1) < toupper (*str2))
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	return -1;
      str1++, str2++;
    }

  if (*str1 == '=' && *str2 == '=')
    return 0;
  else if (*str1 == '=')
    return -1;
  else
    return 1;
}

void
merge_and_sort_env (char **envp1, char **envp2, char **new_envp)
{
  char **optr, **nptr;
  int num;

  nptr = new_envp;
  optr = envp1;
  while (*optr)
    *nptr++ = *optr++;
  num = optr - envp1;

  optr = envp2;
  while (*optr)
    *nptr++ = *optr++;
  num += optr - envp2;

  qsort (new_envp, num, sizeof (char *), compare_env);

  *nptr = NULL;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
712 713 714 715

/* When a new child process is created we need to register it in our list,
   so intercept spawn requests.  */
int 
716
sys_spawnve (int mode, char *cmdname, char **argv, char **envp)
Richard M. Stallman's avatar
Richard M. Stallman committed
717
{
718
  Lisp_Object program, full;
Richard M. Stallman's avatar
Richard M. Stallman committed
719
  char *cmdline, *env, *parg, **targ;
720
  int arglen, numenv;
721 722
  int pid;
  child_process *cp;
723
  int is_dos_app, is_cygnus_app, is_gui_app;
Geoff Voelker's avatar
Geoff Voelker committed
724 725
  int do_quoting = 0;
  char escape_char;
726 727 728 729
  /* We pass our process ID to our children by setting up an environment
     variable in their environment.  */
  char ppid_env_var_buffer[64];
  char *extra_env[] = {ppid_env_var_buffer, NULL};
730
  char *sepchars = " \t";
731

732 733 734 735 736 737
  /* We don't care about the other modes */
  if (mode != _P_NOWAIT)
    {
      errno = EINVAL;
      return -1;
    }
738 739 740 741 742 743 744 745 746

  /* Handle executable names without an executable suffix.  */
  program = make_string (cmdname, strlen (cmdname));
  if (NILP (Ffile_executable_p (program)))
    {
      struct gcpro gcpro1;
      
      full = Qnil;
      GCPRO1 (program);
Stefan Monnier's avatar
Stefan Monnier committed
747
      openp (Vexec_path, program, Vexec_suffixes, &full, 1);
748 749 750 751 752 753
      UNGCPRO;
      if (NILP (full))
	{
	  errno = EINVAL;
	  return -1;
	}
Geoff Voelker's avatar
Geoff Voelker committed
754
      program = full;
755 756
    }

Geoff Voelker's avatar
Geoff Voelker committed
757 758
  /* make sure argv[0] and cmdname are both in DOS format */
  cmdname = XSTRING (program)->data;
759 760
  unixtodos_filename (cmdname);
  argv[0] = cmdname;
761

762
  /* Determine whether program is a 16-bit DOS executable, or a w32
Geoff Voelker's avatar
Geoff Voelker committed
763 764 765
     executable that is implicitly linked to the Cygnus dll (implying it
     was compiled with the Cygnus GNU toolchain and hence relies on
     cygwin.dll to parse the command line - we use this to decide how to
766 767 768 769 770
     escape quote chars in command line args that must be quoted).

     Also determine whether it is a GUI app, so that we don't hide its
     initial window unless specifically requested.  */
  w32_executable_type (cmdname, &is_dos_app, &is_cygnus_app, &is_gui_app);
Geoff Voelker's avatar
Geoff Voelker committed
771 772 773 774 775

  /* On Windows 95, if cmdname is a DOS app, we invoke a helper
     application to start it by specifying the helper app as cmdname,
     while leaving the real app name as argv[0].  */
  if (is_dos_app)
776
    {
Geoff Voelker's avatar
Geoff Voelker committed
777 778 779 780 781 782 783 784 785
      cmdname = alloca (MAXPATHLEN);
      if (egetenv ("CMDPROXY"))
	strcpy (cmdname, egetenv ("CMDPROXY"));
      else
	{
	  strcpy (cmdname, XSTRING (Vinvocation_directory)->data);
	  strcat (cmdname, "cmdproxy.exe");
	}
      unixtodos_filename (cmdname);
786
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
787 788 789 790 791
  
  /* we have to do some conjuring here to put argv and envp into the
     form CreateProcess wants...  argv needs to be a space separated/null
     terminated list of parameters, and envp is a null
     separated/double-null terminated list of parameters.
792

Geoff Voelker's avatar
Geoff Voelker committed
793 794 795 796 797 798 799
     Additionally, zero-length args and args containing whitespace or
     quote chars need to be wrapped in double quotes - for this to work,
     embedded quotes need to be escaped as well.  The aim is to ensure
     the child process reconstructs the argv array we start with
     exactly, so we treat quotes at the beginning and end of arguments
     as embedded quotes.

800
     The w32 GNU-based library from Cygnus doubles quotes to escape
Geoff Voelker's avatar
Geoff Voelker committed
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
     them, while MSVC uses backslash for escaping.  (Actually the MSVC
     startup code does attempt to recognise doubled quotes and accept
     them, but gets it wrong and ends up requiring three quotes to get a
     single embedded quote!)  So by default we decide whether to use
     quote or backslash as the escape character based on whether the
     binary is apparently a Cygnus compiled app.

     Note that using backslash to escape embedded quotes requires
     additional special handling if an embedded quote is already
     preceeded by backslash, or if an arg requiring quoting ends with
     backslash.  In such cases, the run of escape characters needs to be
     doubled.  For consistency, we apply this special handling as long
     as the escape character is not quote.

     Since we have no idea how large argv and envp are likely to be we
     figure out list lengths on the fly and allocate them.  */

  if (!NILP (Vw32_quote_process_args))
    {
      do_quoting = 1;
      /* Override escape char by binding w32-quote-process-args to
	 desired character, or use t for auto-selection.  */
      if (INTEGERP (Vw32_quote_process_args))
	escape_char = XINT (Vw32_quote_process_args);
      else
	escape_char = is_cygnus_app ? '"' : '\\';
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
828
  
829 830 831 832
  /* Cygwin apps needs quoting a bit more often */ 
  if (escape_char == '"')
    sepchars = "\r\n\t\f '";

Richard M. Stallman's avatar
Richard M. Stallman committed
833 834 835 836 837
  /* do argv...  */
  arglen = 0;
  targ = argv;
  while (*targ)
    {
838
      char * p = *targ;
Geoff Voelker's avatar
Geoff Voelker committed
839 840
      int need_quotes = 0;
      int escape_char_run = 0;
841 842

      if (*p == 0)
Geoff Voelker's avatar
Geoff Voelker committed
843 844 845
	need_quotes = 1;
      for ( ; *p; p++)
	{
846 847 848 849
	  if (escape_char == '"' && *p == '\\')
	    /* If it's a Cygwin app, \ needs to be escaped.  */
	    arglen++;
	  else if (*p == '"')
Geoff Voelker's avatar
Geoff Voelker committed
850 851 852 853 854 855 856 857 858 859 860 861 862
	    {
	      /* allow for embedded quotes to be escaped */
	      arglen++;
	      need_quotes = 1;
	      /* handle the case where the embedded quote is already escaped */
	      if (escape_char_run > 0)
		{
		  /* To preserve the arg exactly, we need to double the
		     preceding escape characters (plus adding one to
		     escape the quote character itself).  */
		  arglen += escape_char_run;
		}
	    }
863
	  else if (strchr (sepchars, *p) != NULL)
Geoff Voelker's avatar
Geoff Voelker committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
	    {
	      need_quotes = 1;
	    }

	  if (*p == escape_char && escape_char != '"')
	    escape_char_run++;
	  else
	    escape_char_run = 0;
	}
      if (need_quotes)
	{
	  arglen += 2;
	  /* handle the case where the arg ends with an escape char - we
	     must not let the enclosing quote be escaped.  */
	  if (escape_char_run > 0)
	    arglen += escape_char_run;
	}
Richard M. Stallman's avatar
Richard M. Stallman committed
881 882
      arglen += strlen (*targ++) + 1;
    }
883
  cmdline = alloca (arglen);
Richard M. Stallman's avatar
Richard M. Stallman committed
884 885 886 887
  targ = argv;
  parg = cmdline;
  while (*targ)
    {
888
      char * p = *targ;
Geoff Voelker's avatar
Geoff Voelker committed
889
      int need_quotes = 0;
890 891

      if (*p == 0)
Geoff Voelker's avatar
Geoff Voelker committed
892
	need_quotes = 1;
893

Geoff Voelker's avatar
Geoff Voelker committed
894
      if (do_quoting)
895 896
	{
	  for ( ; *p; p++)
897
	    if ((strchr (sepchars, *p) != NULL) || *p == '"')
Geoff Voelker's avatar
Geoff Voelker committed
898
	      need_quotes = 1;
899
	}
Geoff Voelker's avatar
Geoff Voelker committed
900
      if (need_quotes)
901
	{
Geoff Voelker's avatar
Geoff Voelker committed
902
	  int escape_char_run = 0;
903 904 905 906 907 908 909
	  char * first;
	  char * last;

	  p = *targ;
	  first = p;
	  last = p + strlen (p) - 1;
	  *parg++ = '"';
Geoff Voelker's avatar
Geoff Voelker committed
910 911 912 913 914 915 916
#if 0
	  /* This version does not escape quotes if they occur at the
	     beginning or end of the arg - this could lead to incorrect
	     behaviour when the arg itself represents a command line
	     containing quoted args.  I believe this was originally done
	     as a hack to make some things work, before
	     `w32-quote-process-args' was added.  */
917 918 919
	  while (*p)
	    {
	      if (*p == '"' && p > first && p < last)
Geoff Voelker's avatar
Geoff Voelker committed
920
		*parg++ = escape_char;	/* escape embedded quotes */
921 922
	      *parg++ = *p++;
	    }
Geoff Voelker's avatar
Geoff Voelker committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936
#else
	  for ( ; *p; p++)
	    {
	      if (*p == '"')
		{
		  /* double preceding escape chars if any */
		  while (escape_char_run > 0)
		    {
		      *parg++ = escape_char;
		      escape_char_run--;
		    }
		  /* escape all quote chars, even at beginning or end */
		  *parg++ = escape_char;
		}
937 938
	      else if (escape_char == '"' && *p == '\\')
		*parg++ = '\\';
Geoff Voelker's avatar
Geoff Voelker committed
939 940 941 942 943 944 945 946 947 948 949 950 951 952
	      *parg++ = *p;

	      if (*p == escape_char && escape_char != '"')
		escape_char_run++;
	      else
		escape_char_run = 0;
	    }
	  /* double escape chars before enclosing quote */
	  while (escape_char_run > 0)
	    {
	      *parg++ = escape_char;
	      escape_char_run--;
	    }
#endif
953 954 955 956 957 958 959
	  *parg++ = '"';
	}
      else
	{
	  strcpy (parg, *targ);
	  parg += strlen (*targ);
	}
Richard M. Stallman's avatar
Richard M. Stallman committed
960
      *parg++ = ' ';
961
      targ++;
Richard M. Stallman's avatar
Richard M. Stallman committed
962 963 964 965 966 967
    }
  *--parg = '\0';
  
  /* and envp...  */
  arglen = 1;
  targ = envp;
968
  numenv = 1; /* for end null */
Richard M. Stallman's avatar
Richard M. Stallman committed
969 970 971
  while (*targ)
    {
      arglen += strlen (*targ++) + 1;
972
      numenv++;
Richard M. Stallman's avatar
Richard M. Stallman committed
973
    }
974
  /* extra env vars... */
975
  sprintf (ppid_env_var_buffer, "EM_PARENT_PROCESS_ID=%d", 
Richard M. Stallman's avatar
Richard M. Stallman committed
976 977
	   GetCurrentProcessId ());
  arglen += strlen (ppid_env_var_buffer) + 1;
978
  numenv++;
Richard M. Stallman's avatar
Richard M. Stallman committed
979

980 981 982 983 984
  /* merge env passed in and extra env into one, and sort it.  */
  targ = (char **) alloca (numenv * sizeof (char *));
  merge_and_sort_env (envp, extra_env, targ);

  /* concatenate env entries.  */
985
  env = alloca (arglen);
Richard M. Stallman's avatar
Richard M. Stallman committed
986 987 988 989 990 991 992 993 994
  parg = env;
  while (*targ)
    {
      strcpy (parg, *targ);
      parg += strlen (*targ++);
      *parg++ = '\0';
    }
  *parg++ = '\0';
  *parg = '\0';
995 996 997 998 999 1000 1001

  cp = new_child ();
  if (cp == NULL)
    {
      errno = EAGAIN;
      return -1;
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
1002 1003
  
  /* Now create the process.  */
1004
  if (!create_child (cmdname, cmdline, env, is_gui_app, &pid, cp))
Richard M. Stallman's avatar
Richard M. Stallman committed
1005
    {
1006
      delete_child (cp);
Richard M. Stallman's avatar
Richard M. Stallman committed
1007
      errno = ENOEXEC;
1008
      return -1;
Richard M. Stallman's avatar
Richard M. Stallman committed
1009 1010
    }
  
1011
  return pid;
Richard M. Stallman's avatar
Richard M. Stallman committed
1012 1013 1014 1015 1016
}

/* Emulate the select call
   Wait for available input on any of the given rfds, or timeout if
   a timeout is given and no input is detected
Geoff Voelker's avatar
Geoff Voelker committed
1017 1018 1019 1020 1021 1022 1023
   wfds and efds are not supported and must be NULL.

   For simplicity, we detect the death of child processes here and
   synchronously call the SIGCHLD handler.  Since it is possible for
   children to be created without a corresponding pipe handle from which
   to read output, we wait separately on the process handles as well as
   the char_avail events for each process pipe.  We only call
1024 1025 1026 1027 1028 1029 1030
   wait/reap_process when the process actually terminates.

   To reduce the number of places in which Emacs can be hung such that
   C-g is not able to interrupt it, we always wait on interrupt_handle
   (which is signalled by the input thread when C-g is detected).  If we
   detect that we were woken up by C-g, we return -1 with errno set to
   EINTR as on Unix.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
1031 1032 1033

/* From ntterm.c */
extern HANDLE keyboard_handle;
1034 1035 1036 1037

/* From w32xfns.c */
extern HANDLE interrupt_handle;

Richard M. Stallman's avatar
Richard M. Stallman committed
1038 1039 1040 1041
/* From process.c */
extern int proc_buffered_char[];

int 
1042 1043
sys_select (int nfds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
	    EMACS_TIME *timeout)
Richard M. Stallman's avatar
Richard M. Stallman committed
1044 1045
{
  SELECT_TYPE orfds;
Geoff Voelker's avatar
Geoff Voelker committed
1046 1047
  DWORD timeout_ms, start_time;
  int i, nh, nc, nr;
Richard M. Stallman's avatar
Richard M. Stallman committed
1048
  DWORD active;
Geoff Voelker's avatar
Geoff Voelker committed
1049 1050
  child_process *cp, *cps[MAX_CHILDREN];
  HANDLE wait_hnd[MAXDESC + MAX_CHILDREN];
1051
  int fdindex[MAXDESC];   /* mapping from wait handles back to descriptors */
Richard M. Stallman's avatar
Richard M. Stallman committed
1052
  
Geoff Voelker's avatar
Geoff Voelker committed
1053 1054
  timeout_ms = timeout ? (timeout->tv_sec * 1000 + timeout->tv_usec / 1000) : INFINITE;

Richard M. Stallman's avatar
Richard M. Stallman committed
1055 1056 1057
  /* If the descriptor sets are NULL but timeout isn't, then just Sleep.  */
  if (rfds == NULL && wfds == NULL && efds == NULL && timeout != NULL) 
    {
Geoff Voelker's avatar
Geoff Voelker committed
1058
      Sleep (timeout_ms);
Richard M. Stallman's avatar
Richard M. Stallman committed
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
      return 0;
    }

  /* Otherwise, we only handle rfds, so fail otherwise.  */
  if (rfds == NULL || wfds != NULL || efds != NULL)
    {
      errno = EINVAL;
      return -1;
    }
  
  orfds = *rfds;
  FD_ZERO (rfds);
  nr = 0;
1072 1073 1074 1075

  /* Always wait on interrupt_handle, to detect C-g (quit).  */
  wait_hnd[0] = interrupt_handle;
  fdindex[0] = -1;
Richard M. Stallman's avatar
Richard M. Stallman committed
1076
  
Geoff Voelker's avatar
Geoff Voelker committed
1077
  /* Build a list of pipe handles to wait on.  */
1078
  nh = 1;
Richard M. Stallman's avatar
Richard M. Stallman committed
1079 1080 1081 1082 1083
  for (i = 0; i < nfds; i++)
    if (FD_ISSET (i, &orfds))
      {
	if (i == 0)
	  {
1084 1085 1086 1087 1088 1089 1090
	    if (keyboard_handle)
	      {
		/* Handle stdin specially */
		wait_hnd[nh] = keyboard_handle;
		fdindex[nh] = i;
		nh++;
	      }
Richard M. Stallman's avatar
Richard M. Stallman committed
1091 1092 1093 1094 1095 1096

	    /* Check for any emacs-generated input in the queue since
	       it won't be detected in the wait */
	    if (detect_input_pending ())
	      {
		FD_SET (i, rfds);
1097
		return 1;
Richard M. Stallman's avatar
Richard M. Stallman committed
1098 1099 1100 1101
	      }
	  }
	else
	  {
1102 1103
	    /* Child process and socket input */
	    cp = fd_info[i].cp;
Richard M. Stallman's avatar
Richard M. Stallman committed
1104 1105
	    if (cp)
	      {
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
		int current_status = cp->status;

		if (current_status == STATUS_READ_ACKNOWLEDGED)
		  {
		    /* Tell reader thread which file handle to use. */
		    cp->fd = i;
		    /* Wake up the reader thread for this process */
		    cp->status = STATUS_READ_READY;
		    if (!SetEvent (cp->char_consumed))
		      DebPrint (("nt_select.SetEvent failed with "
				 "%lu for fd %ld\n", GetLastError (), i));
		  }

#ifdef CHECK_INTERLOCK
		/* slightly crude cross-checking of interlock between threads */

		current_status = cp->status;
		if (WaitForSingleObject (cp->char_avail, 0) == WAIT_OBJECT_0)
		  {
		    /* char_avail has been signalled, so status (which may
		       have changed) should indicate read has completed
		       but has not been acknowledged. */
		    current_status = cp->status;
Geoff Voelker's avatar
Geoff Voelker committed
1129 1130
		    if (current_status != STATUS_READ_SUCCEEDED
			&& current_status != STATUS_READ_FAILED)
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
		      DebPrint (("char_avail set, but read not completed: status %d\n",
				 current_status));
		  }
		else
		  {
		    /* char_avail has not been signalled, so status should
		       indicate that read is in progress; small possibility
		       that read has completed but event wasn't yet signalled
		       when we tested it (because a context switch occurred
		       or if running on separate CPUs). */
Geoff Voelker's avatar
Geoff Voelker committed
1141 1142 1143 1144
		    if (current_status != STATUS_READ_READY
			&& current_status != STATUS_READ_IN_PROGRESS
			&& current_status != STATUS_READ_SUCCEEDED
			&& current_status != STATUS_READ_FAILED)
1145 1146 1147 1148 1149 1150 1151 1152
		      DebPrint (("char_avail reset, but read status is bad: %d\n",
				 current_status));
		  }
#endif
		wait_hnd[nh] = cp->char_avail;
		fdindex[nh] = i;
		if (!wait_hnd[nh]) abort ();
		nh++;
Richard M. Stallman's avatar
Richard M. Stallman committed
1153 1154 1155 1156 1157 1158 1159
#ifdef FULL_DEBUG
		DebPrint (("select waiting on child %d fd %d\n",
			   cp-child_procs, i));
#endif
	      }
	    else
	      {
1160
		/* Unable to find something to wait on for this fd, skip */
1161 1162 1163 1164 1165 1166 1167 1168