annotate src/process-nt.c @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents c6012109f545
children a86b2b5e0111
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 373
diff changeset
1 /* Asynchronous subprocess implementation for Win32
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
2 Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
3 Free Software Foundation, Inc.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
4 Copyright (C) 1995 Sun Microsystems, Inc.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
5 Copyright (C) 1995, 1996 Ben Wing.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
6
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
7 This file is part of XEmacs.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
8
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
9 XEmacs is free software; you can redistribute it and/or modify it
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
10 under the terms of the GNU General Public License as published by the
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
11 Free Software Foundation; either version 2, or (at your option) any
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
12 later version.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
13
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
17 for more details.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
18
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
20 along with XEmacs; see the file COPYING. If not, write to
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
22 Boston, MA 02111-1307, USA. */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
23
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
24 /* Written by Kirill M. Katsnelson <kkm@kis.ru>, April 1998 */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
25
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
26 #include <config.h>
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
27 #include "lisp.h"
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
28
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
29 #include "hash.h"
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
30 #include "lstream.h"
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
31 #include "process.h"
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
32 #include "procimpl.h"
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
33 #include "sysdep.h"
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
34
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
35 #ifndef __MINGW32__
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
36 #include <shellapi.h>
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
37 #else
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
38 #include <errno.h>
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
39 #endif
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
40 #include <signal.h>
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
41 #ifdef HAVE_SOCKETS
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
42 #include <winsock.h>
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
43 #endif
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
44
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
45 /* Arbitrary size limit for code fragments passed to run_in_other_process */
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
46 #define FRAGMENT_CODE_SIZE 32
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
47
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
48 /* Bound by winnt.el */
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
49 Lisp_Object Qnt_quote_process_args;
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
50
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 373
diff changeset
51 /* Implementation-specific data. Pointed to by Lisp_Process->process_data */
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
52 struct nt_process_data
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
53 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
54 HANDLE h_process;
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
55 int need_enable_child_signals;
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
56 };
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
57
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
58 #define NT_DATA(p) ((struct nt_process_data*)((p)->process_data))
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
59
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
60 /*-----------------------------------------------------------------------*/
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
61 /* Process helpers */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
62 /*-----------------------------------------------------------------------*/
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
63
276
6330739388db Import from CVS: tag r21-0b36
cvs
parents: 274
diff changeset
64 /* This one breaks process abstraction. Prototype is in console-msw.h,
6330739388db Import from CVS: tag r21-0b36
cvs
parents: 274
diff changeset
65 used by select_process method in event-msw.c */
6330739388db Import from CVS: tag r21-0b36
cvs
parents: 274
diff changeset
66 HANDLE
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
67 get_nt_process_handle (Lisp_Process *p)
276
6330739388db Import from CVS: tag r21-0b36
cvs
parents: 274
diff changeset
68 {
6330739388db Import from CVS: tag r21-0b36
cvs
parents: 274
diff changeset
69 return (NT_DATA (p)->h_process);
6330739388db Import from CVS: tag r21-0b36
cvs
parents: 274
diff changeset
70 }
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
71
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
72 /*-----------------------------------------------------------------------*/
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
73 /* Running remote threads. See Microsoft Systems Journal 1994 Number 5 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
74 /* Jeffrey Richter, Load Your 32-bit DLL into Another Process's Address..*/
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
75 /*-----------------------------------------------------------------------*/
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
76
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
77 typedef struct
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
78 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
79 HANDLE h_process;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
80 HANDLE h_thread;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
81 LPVOID address;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
82 } process_memory;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
83
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
84 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
85 * Allocate SIZE bytes in H_PROCESS address space. Fill in PMC used
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
86 * further by other routines. Return nonzero if successful.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
87 *
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
88 * The memory in other process is allocated by creating a suspended
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
89 * thread. Initial stack of that thread is used as the memory
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
90 * block. The thread entry point is the routine ExitThread in
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
91 * kernel32.dll, so the allocated memory is freed just by resuming the
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
92 * thread, which immediately terminates after that.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
93 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
94
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
95 static int
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
96 alloc_process_memory (HANDLE h_process, size_t size,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
97 process_memory* pmc)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
98 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
99 LPTHREAD_START_ROUTINE adr_ExitThread =
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
100 (LPTHREAD_START_ROUTINE)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
101 GetProcAddress (GetModuleHandle ("kernel32"), "ExitThread");
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
102 DWORD dw_unused;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
103 CONTEXT context;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
104 MEMORY_BASIC_INFORMATION mbi;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
105
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
106 pmc->h_process = h_process;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
107 pmc->h_thread = CreateRemoteThread (h_process, NULL, size,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
108 adr_ExitThread, NULL,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
109 CREATE_SUSPENDED, &dw_unused);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
110 if (pmc->h_thread == NULL)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
111 return 0;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
112
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
113 /* Get context, for thread's stack pointer */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
114 context.ContextFlags = CONTEXT_CONTROL;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
115 if (!GetThreadContext (pmc->h_thread, &context))
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
116 goto failure;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
117
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
118 /* Determine base address of the committed range */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
119 if (sizeof(mbi) != VirtualQueryEx (h_process,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
120 #if defined (_X86_)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
121 (LPDWORD)context.Esp - 1,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
122 #elif defined (_ALPHA_)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
123 (LPDWORD)context.IntSp - 1,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
124 #else
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
125 #error Unknown processor architecture
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
126 #endif
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
127 &mbi, sizeof(mbi)))
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
128 goto failure;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
129
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
130 /* Change the page protection of the allocated memory to executable,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
131 read, and write. */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
132 if (!VirtualProtectEx (h_process, mbi.BaseAddress, size,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
133 PAGE_EXECUTE_READWRITE, &dw_unused))
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
134 goto failure;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
135
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
136 pmc->address = mbi.BaseAddress;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
137 return 1;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
138
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
139 failure:
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
140 ResumeThread (pmc->h_thread);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
141 pmc->address = 0;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
142 return 0;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
143 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
144
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
145 static void
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
146 free_process_memory (process_memory* pmc)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
147 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
148 ResumeThread (pmc->h_thread);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
149 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
150
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
151 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
152 * Run ROUTINE in the context of process determined by H_PROCESS. The
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
153 * routine is passed the address of DATA as parameter. The ROUTINE must
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
154 * not be longer than ROUTINE_CODE_SIZE bytes. DATA_SIZE is the size of
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
155 * DATA structure.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
156 *
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
157 * Note that the code must be positionally independent, and compiled
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
158 * without stack checks (they cause implicit calls into CRT so will
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
159 * fail). DATA should not refer any data in calling process, as both
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
160 * routine and its data are copied into remote process. Size of data
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
161 * and code together should not exceed one page (4K on x86 systems).
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
162 *
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
163 * Return the value returned by ROUTINE, or (DWORD)-1 if call failed.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
164 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
165 static DWORD
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
166 run_in_other_process (HANDLE h_process,
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
167 LPTHREAD_START_ROUTINE routine,
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
168 LPVOID data, size_t data_size)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
169 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
170 process_memory pm;
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
171 const size_t code_size = FRAGMENT_CODE_SIZE;
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
172 /* Need at most 3 extra bytes of memory, for data alignment */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
173 size_t total_size = code_size + data_size + 3;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
174 LPVOID remote_data;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
175 HANDLE h_thread;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
176 DWORD dw_unused;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
177
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
178 /* Allocate memory */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
179 if (!alloc_process_memory (h_process, total_size, &pm))
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
180 return (DWORD)-1;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
181
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
182 /* Copy code */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
183 if (!WriteProcessMemory (h_process, pm.address, (LPVOID)routine,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
184 code_size, NULL))
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
185 goto failure;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
186
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
187 /* Copy data */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
188 if (data_size)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
189 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
190 remote_data = (LPBYTE)pm.address + ((code_size + 4) & ~3);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
191 if (!WriteProcessMemory (h_process, remote_data, data, data_size, NULL))
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
192 goto failure;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
193 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
194 else
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
195 remote_data = NULL;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
196
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
197 /* Execute the remote copy of code, passing it remote data */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
198 h_thread = CreateRemoteThread (h_process, NULL, 0,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
199 (LPTHREAD_START_ROUTINE) pm.address,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
200 remote_data, 0, &dw_unused);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
201 if (h_thread == NULL)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
202 goto failure;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
203
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
204 /* Wait till thread finishes */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
205 WaitForSingleObject (h_thread, INFINITE);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
206
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
207 /* Free remote memory */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
208 free_process_memory (&pm);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
209
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
210 /* Return thread's exit code */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
211 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
212 DWORD exit_code;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
213 GetExitCodeThread (h_thread, &exit_code);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
214 CloseHandle (h_thread);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
215 return exit_code;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
216 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
217
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
218 failure:
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
219 free_process_memory (&pm);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
220 return (DWORD)-1;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
221 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
222
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
223 /*-----------------------------------------------------------------------*/
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
224 /* Sending signals */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
225 /*-----------------------------------------------------------------------*/
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
226
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
227 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
228 * We handle the following signals:
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
229 *
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
230 * SIGKILL, SIGTERM, SIGQUIT, SIGHUP - These four translate to ExitProcess
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
231 * executed by the remote process
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
232 * SIGINT - The remote process is sent CTRL_BREAK_EVENT
373
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
233 *
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
234 * The MSVC5.0 compiler feels free to re-order functions within a
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
235 * compilation unit, so we have no way of finding out the size of the
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
236 * following functions. Therefore these functions must not be larger than
6240c7796c7a Import from CVS: tag r21-2b2
cvs
parents: 371
diff changeset
237 * FRAGMENT_CODE_SIZE.
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
238 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
239
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
240 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
241 * Sending SIGKILL
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
242 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
243 typedef struct
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
244 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
245 void (WINAPI *adr_ExitProcess) (UINT);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
246 } sigkill_data;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
247
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
248 static DWORD WINAPI
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
249 sigkill_proc (sigkill_data* data)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
250 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
251 (*data->adr_ExitProcess)(255);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
252 return 1;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
253 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
254
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
255 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
256 * Sending break or control c
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
257 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
258 typedef struct
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
259 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
260 BOOL (WINAPI *adr_GenerateConsoleCtrlEvent) (DWORD, DWORD);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
261 DWORD event;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
262 } sigint_data;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
263
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
264 static DWORD WINAPI
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
265 sigint_proc (sigint_data* data)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
266 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
267 return (*data->adr_GenerateConsoleCtrlEvent) (data->event, 0);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
268 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
269
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
270 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
271 * Enabling signals
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
272 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
273 typedef struct
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
274 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
275 BOOL (WINAPI *adr_SetConsoleCtrlHandler) (LPVOID, BOOL);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
276 } sig_enable_data;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
277
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
278 static DWORD WINAPI
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
279 sig_enable_proc (sig_enable_data* data)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
280 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
281 (*data->adr_SetConsoleCtrlHandler) (NULL, FALSE);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
282 return 1;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
283 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
284
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
285 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
286 * Send signal SIGNO to process H_PROCESS.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
287 * Return nonzero if successful.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
288 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
289
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
290 /* This code assigns a return value of GetProcAddress to function pointers
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
291 of many different types. Instead of heavy obscure casts, we just disable
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
292 warnings about assignments to different function pointer types. */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
293 #pragma warning (disable : 4113)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
294
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
295 static int
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
296 send_signal (HANDLE h_process, int signo)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
297 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
298 HMODULE h_kernel = GetModuleHandle ("kernel32");
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
299 DWORD retval;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
300
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
301 assert (h_kernel != NULL);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
302
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
303 switch (signo)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
304 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
305 case SIGKILL:
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
306 case SIGTERM:
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
307 case SIGQUIT:
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
308 case SIGHUP:
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
309 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
310 sigkill_data d;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
311 d.adr_ExitProcess = GetProcAddress (h_kernel, "ExitProcess");
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
312 assert (d.adr_ExitProcess);
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
313 retval = run_in_other_process (h_process,
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
314 (LPTHREAD_START_ROUTINE)sigkill_proc,
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
315 &d, sizeof (d));
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
316 break;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
317 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
318 case SIGINT:
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
319 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
320 sigint_data d;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
321 d.adr_GenerateConsoleCtrlEvent =
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
322 GetProcAddress (h_kernel, "GenerateConsoleCtrlEvent");
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
323 assert (d.adr_GenerateConsoleCtrlEvent);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
324 d.event = CTRL_C_EVENT;
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
325 retval = run_in_other_process (h_process,
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
326 (LPTHREAD_START_ROUTINE)sigint_proc,
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
327 &d, sizeof (d));
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
328 break;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
329 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
330 default:
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
331 assert (0);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
332 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
333
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
334 return (int)retval > 0 ? 1 : 0;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
335 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
336
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
337 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
338 * Enable CTRL_C_EVENT handling in a new child process
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
339 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
340 static void
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
341 enable_child_signals (HANDLE h_process)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
342 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
343 HMODULE h_kernel = GetModuleHandle ("kernel32");
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
344 sig_enable_data d;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
345
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
346 assert (h_kernel != NULL);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
347 d.adr_SetConsoleCtrlHandler =
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
348 GetProcAddress (h_kernel, "SetConsoleCtrlHandler");
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
349 assert (d.adr_SetConsoleCtrlHandler);
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
350 run_in_other_process (h_process, (LPTHREAD_START_ROUTINE)sig_enable_proc,
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
351 &d, sizeof (d));
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
352 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
353
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
354 #pragma warning (default : 4113)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
355
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
356 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
357 * Signal error if SIGNO is not supported
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
358 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
359 static void
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
360 validate_signal_number (int signo)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
361 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
362 if (signo != SIGKILL && signo != SIGTERM
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
363 && signo != SIGQUIT && signo != SIGINT
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
364 && signo != SIGHUP)
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
365 signal_simple_error ("Signal number not supported", make_int (signo));
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
366 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
367
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
368 /*-----------------------------------------------------------------------*/
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
369 /* Process methods */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
370 /*-----------------------------------------------------------------------*/
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
371
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
372 /*
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
373 * Allocate and initialize Lisp_Process->process_data
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
374 */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
375
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
376 static void
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
377 nt_alloc_process_data (Lisp_Process *p)
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
378 {
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
379 p->process_data = xnew_and_zero (struct nt_process_data);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
380 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
381
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
382 static void
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
383 nt_finalize_process_data (Lisp_Process *p, int for_disksave)
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
384 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
385 assert (!for_disksave);
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
386 if (NT_DATA(p)->h_process)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
387 CloseHandle (NT_DATA(p)->h_process);
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
388 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
389
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
390 /*
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 373
diff changeset
391 * Initialize XEmacs process implementation once
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
392 */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
393 static void
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
394 nt_init_process (void)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
395 {
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
396 /* Initialize winsock */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
397 WSADATA wsa_data;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
398 /* Request Winsock v1.1 Note the order: (minor=1, major=1) */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
399 WSAStartup (MAKEWORD (1,1), &wsa_data);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
400 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
401
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
402 /*
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
403 * Fork off a subprocess. P is a pointer to newly created subprocess
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
404 * object. If this function signals, the caller is responsible for
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
405 * deleting (and finalizing) the process object.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
406 *
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 373
diff changeset
407 * The method must return PID of the new process, a (positive??? ####) number
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
408 * which fits into Lisp_Int. No return value indicates an error, the method
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
409 * must signal an error instead.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
410 */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
411
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
412 static void
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
413 signal_cannot_launch (Lisp_Object image_file, DWORD err)
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
414 {
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
415 mswindows_set_errno (err);
290
c9fe270a4101 Import from CVS: tag r21-0b43
cvs
parents: 288
diff changeset
416 signal_simple_error_2 ("Error starting", image_file, lisp_strerror (errno));
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
417 }
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
418
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
419 static int
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
420 nt_create_process (Lisp_Process *p,
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
421 Lisp_Object *argv, int nargv,
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
422 Lisp_Object program, Lisp_Object cur_dir)
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
423 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
424 HANDLE hmyshove, hmyslurp, hprocin, hprocout, hprocerr;
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
425 LPTSTR command_line;
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
426 BOOL do_io, windowed;
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
427 char *proc_env;
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
428
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
429 /* Find out whether the application is windowed or not */
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
430 {
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
431 /* SHGetFileInfo tends to return ERROR_FILE_NOT_FOUND on most
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
432 errors. This leads to bogus error message. */
298
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
433 DWORD image_type;
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
434 char *p = strrchr ((char *)XSTRING_DATA (program), '.');
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
435 if (p != NULL &&
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
436 (stricmp (p, ".exe") == 0 ||
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
437 stricmp (p, ".com") == 0 ||
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
438 stricmp (p, ".bat") == 0 ||
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
439 stricmp (p, ".cmd") == 0))
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
440 {
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
441 image_type = SHGetFileInfo ((char *)XSTRING_DATA (program), 0,NULL,
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
442 0, SHGFI_EXETYPE);
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
443 }
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
444 else
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
445 {
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
446 char progname[MAX_PATH];
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
447 sprintf (progname, "%s.exe", (char *)XSTRING_DATA (program));
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
448 image_type = SHGetFileInfo (progname, 0, NULL, 0, SHGFI_EXETYPE);
70ad99077275 Import from CVS: tag r21-0b47
cvs
parents: 290
diff changeset
449 }
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
450 if (image_type == 0)
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
451 signal_cannot_launch (program, (GetLastError () == ERROR_FILE_NOT_FOUND
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
452 ? ERROR_BAD_FORMAT : GetLastError ()));
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
453 windowed = HIWORD (image_type) != 0;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
454 }
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
455
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
456 /* Decide whether to do I/O on process handles, or just mark the
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
457 process exited immediately upon successful launching. We do I/O if the
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
458 process is a console one, or if it is windowed but windowed_process_io
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
459 is non-zero */
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
460 do_io = !windowed || windowed_process_io ;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
461
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
462 if (do_io)
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
463 {
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
464 /* Create two unidirectional named pipes */
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
465 HANDLE htmp;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
466 SECURITY_ATTRIBUTES sa;
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
467
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
468 sa.nLength = sizeof(sa);
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
469 sa.bInheritHandle = TRUE;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
470 sa.lpSecurityDescriptor = NULL;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
471
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
472 CreatePipe (&hprocin, &hmyshove, &sa, 0);
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
473 CreatePipe (&hmyslurp, &hprocout, &sa, 0);
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
474
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
475 /* Duplicate the stdout handle for use as stderr */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
476 DuplicateHandle(GetCurrentProcess(), hprocout, GetCurrentProcess(), &hprocerr,
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
477 0, TRUE, DUPLICATE_SAME_ACCESS);
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
478
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
479 /* Stupid Win32 allows to create a pipe with *both* ends either
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
480 inheritable or not. We need process ends inheritable, and local
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
481 ends not inheritable. */
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
482 DuplicateHandle (GetCurrentProcess(), hmyshove, GetCurrentProcess(), &htmp,
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
483 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
484 hmyshove = htmp;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
485 DuplicateHandle (GetCurrentProcess(), hmyslurp, GetCurrentProcess(), &htmp,
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
486 0, FALSE, DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS);
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
487 hmyslurp = htmp;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
488 }
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
489
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
490 /* Convert an argv vector into Win32 style command line by a call to
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
491 lisp function `nt-quote-process-args' which see (in winnt.el)*/
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
492 {
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
493 int i;
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
494 Lisp_Object args_or_ret = Qnil;
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
495 struct gcpro gcpro1;
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
496
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
497 GCPRO1 (args_or_ret);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
498
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
499 for (i = 0; i < nargv; ++i)
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
500 args_or_ret = Fcons (*argv++, args_or_ret);
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
501 args_or_ret = Fnreverse (args_or_ret);
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
502 args_or_ret = Fcons (program, args_or_ret);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
503
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
504 args_or_ret = call1 (Qnt_quote_process_args, args_or_ret);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
505
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
506 if (!STRINGP (args_or_ret))
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
507 /* Luser wrote his/her own clever version */
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
508 error ("Bogus return value from `nt-quote-process-args'");
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
509
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
510 command_line = alloca_array (char, (XSTRING_LENGTH (program)
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
511 + XSTRING_LENGTH (args_or_ret) + 2));
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
512 strcpy (command_line, XSTRING_DATA (program));
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
513 strcat (command_line, " ");
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
514 strcat (command_line, XSTRING_DATA (args_or_ret));
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
515
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
516 UNGCPRO; /* args_or_ret */
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
517 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
518
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
519 /* Set `proc_env' to a nul-separated array of the strings in
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
520 Vprocess_environment terminated by 2 nuls. */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
521
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
522 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
523 extern int compare_env (const char **strp1, const char **strp2);
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
524 char **env;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
525 REGISTER Lisp_Object tem;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
526 REGISTER char **new_env;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
527 REGISTER int new_length = 0, i, new_space;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
528 char *penv;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
529
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
530 for (tem = Vprocess_environment;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
531 (CONSP (tem)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
532 && STRINGP (XCAR (tem)));
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
533 tem = XCDR (tem))
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
534 new_length++;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
535
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
536 /* new_length + 1 to include terminating 0. */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
537 env = new_env = alloca_array (char *, new_length + 1);
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
538
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
539 /* Copy the Vprocess_environment strings into new_env. */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
540 for (tem = Vprocess_environment;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
541 (CONSP (tem)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
542 && STRINGP (XCAR (tem)));
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
543 tem = XCDR (tem))
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
544 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
545 char **ep = env;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
546 char *string = (char *) XSTRING_DATA (XCAR (tem));
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
547 /* See if this string duplicates any string already in the env.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
548 If so, don't put it in.
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
549 When an env var has multiple definitions,
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
550 we keep the definition that comes first in process-environment. */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
551 for (; ep != new_env; ep++)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
552 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
553 char *p = *ep, *q = string;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
554 while (1)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
555 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
556 if (*q == 0)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
557 /* The string is malformed; might as well drop it. */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
558 goto duplicate;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
559 if (*q != *p)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
560 break;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
561 if (*q == '=')
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
562 goto duplicate;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
563 p++, q++;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
564 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
565 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
566 *new_env++ = string;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
567 duplicate: ;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
568 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
569 *new_env = 0;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
570
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
571 /* Sort the environment variables */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
572 new_length = new_env - env;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
573 qsort (env, new_length, sizeof (char *), compare_env);
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
574
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
575 /* Work out how much space to allocate */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
576 new_space = 0;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
577 for (i = 0; i < new_length; i++)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
578 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
579 new_space += strlen(env[i]) + 1;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
580 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
581 new_space++;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
582
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
583 /* Allocate space and copy variables into it */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
584 penv = proc_env = alloca(new_space);
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
585 for (i = 0; i < new_length; i++)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
586 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
587 strcpy(penv, env[i]);
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
588 penv += strlen(env[i]) + 1;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
589 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
590 *penv = 0;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
591 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
592
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
593 /* Create process */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
594 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
595 STARTUPINFO si;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
596 PROCESS_INFORMATION pi;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
597 DWORD err;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
598
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
599 xzero (si);
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
600 si.dwFlags = STARTF_USESHOWWINDOW;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
601 si.wShowWindow = windowed ? SW_SHOWNORMAL : SW_HIDE;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
602 if (do_io)
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
603 {
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
604 si.hStdInput = hprocin;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
605 si.hStdOutput = hprocout;
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
606 si.hStdError = hprocerr;
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
607 si.dwFlags |= STARTF_USESTDHANDLES;
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
608 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
609
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
610 err = (CreateProcess (NULL, command_line, NULL, NULL, TRUE,
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
611 CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
612 | CREATE_SUSPENDED,
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
613 proc_env, (char *) XSTRING_DATA (cur_dir), &si, &pi)
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
614 ? 0 : GetLastError ());
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
615
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
616 if (do_io)
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
617 {
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
618 /* These just have been inherited; we do not need a copy */
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
619 CloseHandle (hprocin);
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
620 CloseHandle (hprocout);
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
621 CloseHandle (hprocerr);
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
622 }
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
623
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
624 /* Handle process creation failure */
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
625 if (err)
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
626 {
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
627 if (do_io)
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
628 {
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
629 CloseHandle (hmyshove);
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
630 CloseHandle (hmyslurp);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
631 }
288
e11d67e05968 Import from CVS: tag r21-0b42
cvs
parents: 286
diff changeset
632 signal_cannot_launch (program, GetLastError ());
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
633 }
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
634
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
635 /* The process started successfully */
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
636 if (do_io)
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
637 {
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
638 NT_DATA(p)->h_process = pi.hProcess;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
639 init_process_io_handles (p, (void*)hmyslurp, (void*)hmyshove, 0);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
640 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
641 else
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
642 {
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
643 /* Indicate as if the process has exited immediately. */
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
644 p->status_symbol = Qexit;
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
645 CloseHandle (pi.hProcess);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
646 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
647
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
648 ResumeThread (pi.hThread);
278
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
649 CloseHandle (pi.hThread);
90d73dddcdc4 Import from CVS: tag r21-0b37
cvs
parents: 276
diff changeset
650
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
651 /* Remember to enable child signals later if this is not a windowed
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
652 app. Can't do it right now because that screws up the MKS Toolkit
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
653 shell. */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
654 if (!windowed)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
655 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
656 NT_DATA(p)->need_enable_child_signals = 10;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
657 kick_status_notify ();
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
658 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
659
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
660 return ((int)pi.dwProcessId);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
661 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
662 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
663
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
664 /*
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
665 * This method is called to update status fields of the process
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
666 * structure. If the process has not existed, this method is expected
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
667 * to do nothing.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
668 *
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
669 * The method is called only for real child processes.
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
670 */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
671
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
672 static void
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
673 nt_update_status_if_terminated (Lisp_Process* p)
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
674 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
675 DWORD exit_code;
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
676
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
677 if (NT_DATA(p)->need_enable_child_signals > 1)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
678 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
679 NT_DATA(p)->need_enable_child_signals -= 1;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
680 kick_status_notify ();
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
681 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
682 else if (NT_DATA(p)->need_enable_child_signals == 1)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
683 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
684 enable_child_signals(NT_DATA(p)->h_process);
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
685 NT_DATA(p)->need_enable_child_signals = 0;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
686 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
687
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
688 if (GetExitCodeProcess (NT_DATA(p)->h_process, &exit_code)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
689 && exit_code != STILL_ACTIVE)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
690 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
691 p->tick++;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
692 p->core_dumped = 0;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
693 /* The exit code can be a code returned by process, or an
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
694 NTSTATUS value. We cannot accurately handle the latter since
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
695 it is a full 32 bit integer */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
696 if (exit_code & 0xC0000000)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
697 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
698 p->status_symbol = Qsignal;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
699 p->exit_code = exit_code & 0x1FFFFFFF;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
700 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
701 else
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
702 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
703 p->status_symbol = Qexit;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
704 p->exit_code = exit_code;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
705 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
706 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
707 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
708
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
709 /*
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 373
diff changeset
710 * Stuff the entire contents of LSTREAM to the process output pipe
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
711 */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
712
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
713 /* #### If only this function could be somehow merged with
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
714 unix_send_process... */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
715
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
716 static void
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
717 nt_send_process (Lisp_Object proc, struct lstream* lstream)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
718 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
719 volatile Lisp_Object vol_proc = proc;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
720 Lisp_Process *volatile p = XPROCESS (proc);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
721
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
722 /* use a reasonable-sized buffer (somewhere around the size of the
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
723 stream buffer) so as to avoid inundating the stream with blocked
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
724 data. */
390
c6012109f545 Import from CVS: tag r21-2-10
cvs
parents: 380
diff changeset
725 Bufbyte chunkbuf[128];
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
726 Bytecount chunklen;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
727
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
728 while (1)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
729 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
730 ssize_t writeret;
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
731
390
c6012109f545 Import from CVS: tag r21-2-10
cvs
parents: 380
diff changeset
732 chunklen = Lstream_read (lstream, chunkbuf, 128);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
733 if (chunklen <= 0)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
734 break; /* perhaps should abort() if < 0?
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
735 This should never happen. */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
736
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
737 /* Lstream_write() will never successfully write less than the
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
738 amount sent in. In the worst case, it just buffers the
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
739 unwritten data. */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
740 writeret = Lstream_write (XLSTREAM (DATA_OUTSTREAM(p)), chunkbuf,
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
741 chunklen);
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
742 Lstream_flush (XLSTREAM (DATA_OUTSTREAM(p)));
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
743 if (writeret < 0)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
744 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
745 p->status_symbol = Qexit;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
746 p->exit_code = ERROR_BROKEN_PIPE;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
747 p->core_dumped = 0;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
748 p->tick++;
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
749 process_tick++;
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
750 deactivate_process (*((Lisp_Object *) (&vol_proc)));
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
751 error ("Broken pipe error sending to process %s; closed it",
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
752 XSTRING_DATA (p->name));
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
753 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
754
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
755 {
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
756 int wait_ms = 25;
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
757 while (Lstream_was_blocked_p (XLSTREAM (p->pipe_outstream)))
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
758 {
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
759 /* Buffer is full. Wait, accepting input; that may allow
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
760 the program to finish doing output and read more. */
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
761 Faccept_process_output (Qnil, Qzero, make_int (wait_ms));
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
762 Lstream_flush (XLSTREAM (p->pipe_outstream));
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
763 wait_ms = min (1000, 2 * wait_ms);
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
764 }
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
765 }
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
766 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
767 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
768
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
769 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
770 * Send a signal number SIGNO to PROCESS.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
771 * CURRENT_GROUP means send to the process group that currently owns
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
772 * the terminal being used to communicate with PROCESS.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
773 * This is used for various commands in shell mode.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
774 * If NOMSG is zero, insert signal-announcements into process's buffers
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
775 * right away.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
776 *
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
777 * If we can, we try to signal PROCESS by sending control characters
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
778 * down the pty. This allows us to signal inferiors who have changed
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
779 * their uid, for which killpg would return an EPERM error.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
780 *
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
781 * The method signals an error if the given SIGNO is not valid
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
782 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
783
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
784 static void
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
785 nt_kill_child_process (Lisp_Object proc, int signo,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
786 int current_group, int nomsg)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
787 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
788 Lisp_Process *p = XPROCESS (proc);
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
789
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
790 /* Enable child signals if necessary. This may lose the first
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
791 but it's better than nothing. */
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
792 if (NT_DATA(p)->need_enable_child_signals > 0)
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
793 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
794 enable_child_signals(NT_DATA(p)->h_process);
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
795 NT_DATA(p)->need_enable_child_signals = 0;
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
796 }
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
797
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
798 /* Signal error if SIGNO cannot be sent */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
799 validate_signal_number (signo);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
800
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
801 /* Send signal */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
802 if (!send_signal (NT_DATA(p)->h_process, signo))
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
803 error ("Cannot send signal to process");
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
804 }
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
805
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
806 /*
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
807 * Kill any process in the system given its PID.
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
808 *
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
809 * Returns zero if a signal successfully sent, or
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
810 * negative number upon failure
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
811 */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
812 static int
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
813 nt_kill_process_by_pid (int pid, int signo)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
814 {
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
815 HANDLE h_process;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
816 int send_result;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
817
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
818 /* Signal error if SIGNO cannot be sent */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
819 validate_signal_number (signo);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
820
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
821 /* Try to open the process with required privileges */
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
822 h_process = OpenProcess (PROCESS_CREATE_THREAD
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
823 | PROCESS_QUERY_INFORMATION
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
824 | PROCESS_VM_OPERATION
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
825 | PROCESS_VM_WRITE,
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
826 FALSE, pid);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
827 if (h_process == NULL)
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
828 return -1;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
829
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
830 send_result = send_signal (h_process, signo);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
831
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
832 CloseHandle (h_process);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
833
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
834 return send_result ? 0 : -1;
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
835 }
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
836
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
837 /*-----------------------------------------------------------------------*/
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
838 /* Sockets connections */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
839 /*-----------------------------------------------------------------------*/
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
840 #ifdef HAVE_SOCKETS
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
841
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
842 /* #### Hey MS, how long Winsock 2 for '95 will be in beta? */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
843
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
844 #define SOCK_TIMER_ID 666
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
845 #define XM_SOCKREPLY (WM_USER + 666)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
846
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
847 static int
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
848 get_internet_address (Lisp_Object host, struct sockaddr_in *address,
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
849 Error_behavior errb)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
850 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
851 char buf [MAXGETHOSTSTRUCT];
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
852 HWND hwnd;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
853 HANDLE hasync;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
854 int success = 0;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
855
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
856 address->sin_family = AF_INET;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
857
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
858 /* First check if HOST is already a numeric address */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
859 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
860 unsigned long inaddr = inet_addr (XSTRING_DATA (host));
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
861 if (inaddr != INADDR_NONE)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
862 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
863 address->sin_addr.s_addr = inaddr;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
864 return 1;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
865 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
866 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
867
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
868 /* Create a window which will receive completion messages */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
869 hwnd = CreateWindow ("STATIC", NULL, WS_OVERLAPPED, 0, 0, 1, 1,
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
870 NULL, NULL, NULL, NULL);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
871 assert (hwnd);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
872
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
873 /* Post name resolution request */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
874 hasync = WSAAsyncGetHostByName (hwnd, XM_SOCKREPLY, XSTRING_DATA (host),
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
875 buf, sizeof (buf));
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
876 if (hasync == NULL)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
877 goto done;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
878
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
879 /* Set a timer to poll for quit every 250 ms */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
880 SetTimer (hwnd, SOCK_TIMER_ID, 250, NULL);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
881
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
882 while (1)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
883 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
884 MSG msg;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
885 GetMessage (&msg, hwnd, 0, 0);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
886 if (msg.message == XM_SOCKREPLY)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
887 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
888 /* Ok, got an answer */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
889 if (WSAGETASYNCERROR(msg.lParam) == NO_ERROR)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
890 success = 1;
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
891 else
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
892 {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
893 warn_when_safe(Qstream, Qwarning,
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
894 "cannot get IP address for host \"%s\"",
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
895 XSTRING_DATA (host));
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
896 }
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
897 goto done;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
898 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
899 else if (msg.message == WM_TIMER && msg.wParam == SOCK_TIMER_ID)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
900 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
901 if (QUITP)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
902 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
903 WSACancelAsyncRequest (hasync);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
904 KillTimer (hwnd, SOCK_TIMER_ID);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
905 DestroyWindow (hwnd);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
906 REALLY_QUIT;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
907 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
908 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
909 DispatchMessage (&msg);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
910 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
911
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
912 done:
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
913 KillTimer (hwnd, SOCK_TIMER_ID);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
914 DestroyWindow (hwnd);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
915 if (success)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
916 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
917 /* BUF starts with struct hostent */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
918 struct hostent* he = (struct hostent*) buf;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
919 address->sin_addr.s_addr = *(unsigned long*)he->h_addr_list[0];
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
920 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
921 return success;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
922 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
923
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
924 static Lisp_Object
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
925 nt_canonicalize_host_name (Lisp_Object host)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
926 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
927 struct sockaddr_in address;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
928
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
929 if (!get_internet_address (host, &address, ERROR_ME_NOT))
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
930 return host;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
931
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
932 if (address.sin_family == AF_INET)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
933 return build_string (inet_ntoa (address.sin_addr));
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
934 else
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
935 return host;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
936 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
937
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
938 /* open a TCP network connection to a given HOST/SERVICE. Treated
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
939 exactly like a normal process when reading and writing. Only
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
940 differences are in status display and process deletion. A network
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
941 connection has no PID; you cannot signal it. All you can do is
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
942 deactivate and close it via delete-process */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
943
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
944 static void
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
945 nt_open_network_stream (Lisp_Object name, Lisp_Object host, Lisp_Object service,
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
946 Lisp_Object protocol, void** vinfd, void** voutfd)
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
947 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
948 struct sockaddr_in address;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
949 SOCKET s;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
950 int port;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
951 int retval;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
952
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
953 CHECK_STRING (host);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
954
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
955 if (!EQ (protocol, Qtcp))
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
956 error ("Unsupported protocol \"%s\"",
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
957 string_data (symbol_name (XSYMBOL (protocol))));
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
958
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
959 if (INTP (service))
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
960 port = htons ((unsigned short) XINT (service));
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
961 else
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
962 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
963 struct servent *svc_info;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
964 CHECK_STRING (service);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
965 svc_info = getservbyname ((char *) XSTRING_DATA (service), "tcp");
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
966 if (svc_info == 0)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
967 error ("Unknown service \"%s\"", XSTRING_DATA (service));
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
968 port = svc_info->s_port;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
969 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
970
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
971 get_internet_address (host, &address, ERROR_ME);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
972 address.sin_port = port;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
973
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
974 s = socket (address.sin_family, SOCK_STREAM, 0);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
975 if (s < 0)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
976 report_file_error ("error creating socket", list1 (name));
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
977
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
978 /* We don't want to be blocked on connect */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
979 {
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
980 unsigned long nonblock = 1;
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
981 ioctlsocket (s, FIONBIO, &nonblock);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
982 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
983
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
984 retval = connect (s, (struct sockaddr *) &address, sizeof (address));
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
985 if (retval != NO_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
986 goto connect_failed;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
987 /* Wait while connection is established */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
988 while (1)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
989 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
990 fd_set fdset;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
991 struct timeval tv;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
992 int nsel;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
993
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
994 if (QUITP)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
995 {
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
996 closesocket (s);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
997 REALLY_QUIT;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
998 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
999
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1000 /* Poll for quit every 250 ms */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1001 tv.tv_sec = 0;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1002 tv.tv_usec = 250 * 1000;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1003
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1004 FD_ZERO (&fdset);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1005 FD_SET (s, &fdset);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1006 nsel = select (0, NULL, &fdset, &fdset, &tv);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1007
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1008 if (nsel > 0)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1009 {
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 373
diff changeset
1010 /* Check: was connection successful or not? */
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1011 tv.tv_usec = 0;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1012 nsel = select (0, NULL, NULL, &fdset, &tv);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1013 if (nsel > 0)
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1014 goto connect_failed;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1015 else
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1016 break;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1017 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1018 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1019
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1020 /* We are connected at this point */
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1021 *vinfd = (void*)s;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1022 DuplicateHandle (GetCurrentProcess(), (HANDLE)s,
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1023 GetCurrentProcess(), (LPHANDLE)voutfd,
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1024 0, FALSE, DUPLICATE_SAME_ACCESS);
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1025 return;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1026
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1027 connect_failed:
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1028 closesocket (s);
398
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1029 if (INTP (service)) {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1030 warn_when_safe(Qstream, Qwarning,
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1031 "failure to open network stream to host \"%s\" for service \"%d\"",
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1032 XSTRING_DATA (host),
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1033 (unsigned short) XINT (service));
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1034 }
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1035 else {
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1036 warn_when_safe(Qstream, Qwarning,
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1037 "failure to open network stream to host \"%s\" for service \"%s\"",
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1038 XSTRING_DATA (host),
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1039 XSTRING_DATA (service));
74fd4e045ea6 Import from CVS: tag r21-2-29
cvs
parents: 390
diff changeset
1040 }
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1041 report_file_error ("connection failed", list2 (host, name));
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1042 }
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1043
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1044 #endif
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
1045
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1046 /*-----------------------------------------------------------------------*/
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1047 /* Initialization */
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1048 /*-----------------------------------------------------------------------*/
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1049
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1050 void
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1051 process_type_create_nt (void)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1052 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1053 PROCESS_HAS_METHOD (nt, alloc_process_data);
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1054 PROCESS_HAS_METHOD (nt, finalize_process_data);
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1055 PROCESS_HAS_METHOD (nt, init_process);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1056 PROCESS_HAS_METHOD (nt, create_process);
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1057 PROCESS_HAS_METHOD (nt, update_status_if_terminated);
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1058 PROCESS_HAS_METHOD (nt, send_process);
280
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
1059 PROCESS_HAS_METHOD (nt, kill_child_process);
7df0dd720c89 Import from CVS: tag r21-0b38
cvs
parents: 278
diff changeset
1060 PROCESS_HAS_METHOD (nt, kill_process_by_pid);
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1061 #ifdef HAVE_SOCKETS
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1062 PROCESS_HAS_METHOD (nt, canonicalize_host_name);
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1063 PROCESS_HAS_METHOD (nt, open_network_stream);
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1064 #ifdef HAVE_MULTICAST
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 280
diff changeset
1065 #error I won't do this until '95 has winsock2
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1066 PROCESS_HAS_METHOD (nt, open_multicast_group);
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1067 #endif
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1068 #endif
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1069 }
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1070
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1071 void
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
1072 syms_of_process_nt (void)
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
1073 {
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
1074 defsymbol (&Qnt_quote_process_args, "nt-quote-process-args");
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
1075 }
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
1076
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 282
diff changeset
1077 void
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1078 vars_of_process_nt (void)
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1079 {
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents:
diff changeset
1080 }