1 /* external.cc: Interface to Cygwin internals from external programs.
3 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009 Red Hat, Inc.
6 Written by Christopher Faylor <cgf@cygnus.com>
8 This file is part of Cygwin.
10 This software is a copyrighted work licensed under the terms of the
11 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
17 #include "shared_info.h"
18 #include "cygwin_version.h"
26 #include "child_info.h"
28 #include "cygserver_setpwd.h"
34 child_info *get_cygwin_startup_info ();
35 static void exit_process (UINT, bool) __attribute__((noreturn));
39 static external_pinfo *
40 fillout_pinfo (pid_t pid, int winpid)
43 static external_pinfo ep;
44 static char ep_progname_long_buf[NT_MAX_PATH];
46 if ((nextpid = !!(pid & CW_NEXTPID)))
50 static unsigned int i;
51 if (!pids.npids || !nextpid)
60 memset (&ep, 0, sizeof ep);
61 while (i < pids.npids)
63 DWORD thispid = pids.winpid (i);
69 if (!nextpid && thispid != (DWORD) pid)
71 ep.pid = cygwin_pid (thispid);
72 ep.dwProcessId = thispid;
73 ep.process_state = PID_IN_USE;
77 else if (nextpid || p->pid == pid || (winpid && thispid == (DWORD) pid))
82 ep.dwProcessId = p->dwProcessId;
88 ep.start_time = p->start_time;
89 ep.rusage_self = p->rusage_self;
90 ep.rusage_children = p->rusage_children;
91 ep.progname[0] = '\0';
92 strncat (ep.progname, p->progname, MAX_PATH - 1);
94 ep.version = EXTERNAL_PINFO_VERSION;
96 ep.process_state = p->process_state;
101 ep.progname_long = ep_progname_long_buf;
102 strcpy (ep.progname_long, p->progname);
117 get_cygdrive_info (char *user, char *system, char *user_flags,
120 int res = mount_table->get_cygdrive_info (user, system, user_flags,
122 return (res == ERROR_SUCCESS) ? 1 : 0;
126 check_ntsec (const char *filename)
130 path_conv pc (filename);
131 return pc.has_acls ();
134 /* Copy cygwin environment variables to the Windows environment. */
139 PWCHAR envblock = NULL;
140 char **envp = build_env (cur_environ (), envblock, unused_envc, false);
145 for (char **e = envp; *e; e++)
153 PWCHAR eq = wcschr (p, L'=');
157 SetEnvironmentVariableW (p, ++eq);
160 p = wcschr (p, L'\0') + 1;
166 * Cygwin-specific wrapper for win32 ExitProcess and TerminateProcess.
167 * It ensures that the correct exit code, derived from the specified
168 * status value, will be made available to this process's parent (if
169 * that parent is also a cygwin process). If useTerminateProcess is
170 * true, then TerminateProcess(GetCurrentProcess(),...) will be used;
171 * otherwise, ExitProcess(...) is called.
173 * Used by startup code for cygwin processes which is linked statically
174 * into applications, and is not part of the cygwin DLL -- which is why
175 * this interface is exposed. "Normal" programs should use ANSI exit(),
176 * ANSI abort(), or POSIX _exit(), rather than this function -- because
177 * calling ExitProcess or TerminateProcess, even through this wrapper,
178 * skips much of the cygwin process cleanup code.
181 exit_process (UINT status, bool useTerminateProcess)
183 pid_t pid = getpid ();
184 external_pinfo * ep = fillout_pinfo (pid, 1);
185 DWORD dwpid = ep ? ep->dwProcessId : pid;
186 pinfo p (pid, PID_MAP_RW);
187 if ((dwpid == GetCurrentProcessId()) && (p->pid == ep->pid))
188 p.set_exit_code ((DWORD)status);
189 if (useTerminateProcess)
190 TerminateProcess (GetCurrentProcess(), status);
191 /* avoid 'else' clause to silence warning */
192 ExitProcess (status);
196 extern "C" unsigned long
197 cygwin_internal (cygwin_getinfo_types t, ...)
200 unsigned long res = -1;
209 case CW_UNLOCK_PINFO:
213 case CW_GETTHREADNAME:
214 res = (DWORD) cygthread::name (va_arg (arg, DWORD));
217 case CW_SETTHREADNAME:
225 res = (DWORD) fillout_pinfo (va_arg (arg, DWORD), 0);
228 case CW_GETVERSIONINFO:
229 res = (DWORD) cygwin_version_strings;
232 case CW_READ_V1_MOUNT_TABLES:
238 /* This is a kludge to work around a version of _cygwin_common_crt0
239 which overwrote the cxx_malloc field with the local DLL copy.
240 Hilarity ensues if the DLL is not loaded like while the process
242 __cygwin_user_data.cxx_malloc = &default_cygwin_cxx_malloc;
243 res = (DWORD) &__cygwin_user_data;
247 perfile_table = va_arg (arg, struct __cygwin_perfile *);
251 case CW_GET_CYGDRIVE_PREFIXES:
253 char *user = va_arg (arg, char *);
254 char *system = va_arg (arg, char *);
255 res = get_cygdrive_info (user, system, NULL, NULL);
259 case CW_GETPINFO_FULL:
260 res = (DWORD) fillout_pinfo (va_arg (arg, pid_t), 1);
263 case CW_INIT_EXCEPTIONS:
264 /* noop */ /* init_exceptions (va_arg (arg, exception_list *)); */
268 case CW_GET_CYGDRIVE_INFO:
270 char *user = va_arg (arg, char *);
271 char *system = va_arg (arg, char *);
272 char *user_flags = va_arg (arg, char *);
273 char *system_flags = va_arg (arg, char *);
274 res = get_cygdrive_info (user, system, user_flags, system_flags);
278 case CW_SET_CYGWIN_REGISTRY_NAME:
279 case CW_GET_CYGWIN_REGISTRY_NAME:
283 case CW_STRACE_TOGGLE:
285 pid_t pid = va_arg (arg, pid_t);
289 sig_send (p, __SIGSTRACE);
300 case CW_STRACE_ACTIVE:
302 res = strace.active ();
306 case CW_CYGWIN_PID_TO_WINPID:
308 pinfo p (va_arg (arg, pid_t));
309 res = p ? p->dwProcessId : 0;
312 case CW_EXTRACT_DOMAIN_AND_USER:
314 WCHAR nt_domain[MAX_DOMAIN_NAME_LEN + 1];
315 WCHAR nt_user[UNLEN + 1];
317 struct passwd *pw = va_arg (arg, struct passwd *);
318 char *domain = va_arg (arg, char *);
319 char *user = va_arg (arg, char *);
320 extract_nt_dom_user (pw, nt_domain, nt_user);
322 sys_wcstombs (domain, MAX_DOMAIN_NAME_LEN + 1, nt_domain);
324 sys_wcstombs (user, UNLEN + 1, nt_user);
331 pid_t pid = va_arg (arg, pid_t);
333 res = (DWORD) p->cmdline (n);
338 char *filename = va_arg (arg, char *);
339 res = check_ntsec (filename);
342 case CW_GET_ERRNO_FROM_WINERROR:
344 int error = va_arg (arg, int);
345 int deferrno = va_arg (arg, int);
346 res = geterrno_from_win_error (error, deferrno);
349 case CW_GET_POSIX_SECURITY_ATTRIBUTE:
352 security_descriptor sd;
353 int attribute = va_arg (arg, int);
354 PSECURITY_ATTRIBUTES psa = va_arg (arg, PSECURITY_ATTRIBUTES);
355 void *sd_buf = va_arg (arg, void *);
356 DWORD sd_buf_size = va_arg (arg, DWORD);
357 set_security_attribute (dummy, attribute, psa, sd);
358 if (!psa->lpSecurityDescriptor)
362 psa->lpSecurityDescriptor = sd_buf;
363 res = sd.copy (sd_buf, sd_buf_size);
369 res = getpagesize ();
372 case CW_GET_UID_FROM_SID:
374 cygpsid psid = va_arg (arg, PSID);
375 res = psid.get_id (false, NULL);
378 case CW_GET_GID_FROM_SID:
380 cygpsid psid = va_arg (arg, PSID);
381 res = psid.get_id (true, NULL);
386 const char *path = va_arg (arg, const char *);
387 path_conv p (path, PC_SYM_FOLLOW | PC_NULLEMPTY);
391 res = (unsigned long) -1;
399 const char *name = va_arg (arg, const char *);
400 const void *hookfn = va_arg (arg, const void *);
402 res = (unsigned long) hook_or_detect_cygwin (name, hookfn, subsys);
407 child_info_spawn *ci = (child_info_spawn *) get_cygwin_startup_info ();
408 res = (unsigned long) (ci ? ci->moreinfo->argv : NULL);
413 child_info_spawn *ci = (child_info_spawn *) get_cygwin_startup_info ();
414 res = (unsigned long) (ci ? ci->moreinfo->envp : NULL);
418 error_start_init (va_arg (arg, const char *));
425 case CW_CYGTLS_PADSIZE:
426 res = CYGTLS_PADSIZE;
428 case CW_SET_DOS_FILE_WARNING:
430 extern bool dos_file_warning;
431 dos_file_warning = va_arg (arg, int);
435 case CW_SET_PRIV_KEY:
437 const char *passwd = va_arg (arg, const char *);
438 res = setlsapwd (passwd);
443 const char *file = va_arg (arg, const char *);
444 int line = va_arg (arg, int);
445 seterrno(file, line);
449 case CW_EXIT_PROCESS:
451 UINT status = va_arg (arg, UINT);
452 int useTerminateProcess = va_arg (arg, int);
453 exit_process (status, !!useTerminateProcess); /* no return */
455 case CW_SET_EXTERNAL_TOKEN:
457 HANDLE token = va_arg (arg, HANDLE);
458 int type = va_arg (arg, int);
459 set_imp_token (token, type);
465 extern WCHAR installation_key_buf[18];
466 PWCHAR dest = va_arg (arg, PWCHAR);
467 wcscpy (dest, installation_key_buf);