int atexit (void (*fn) (void)) { extern int __cxa_atexit(void (*)(void*), void*, void*); extern void *__dso_handle; extern void *__ImageBase; void *fixed_dso_handle = &__dso_handle; /* Check for being called from inside the executable. If so, use NULL as __dso_handle. This allows to link executables with GCC versions not providing __dso_handle in crtbegin{S}.o. In this case our own __dso_handle defined in lib/dso_handle.c is used. However, our __dso_handle always points to &__ImageBase, while the __dso_handle for executables provided by crtbegin.o usually points to NULL. That's what we remodel here. */ if (&__ImageBase == (void **) GetModuleHandleW (NULL)) fixed_dso_handle = NULL; /* With recent Cygwin versions starting with API version 0.280 we call __cxa_atexit (which is actually the cygwin__cxa_atexit wrapper in dcrt0.cc) with the address of __dso_handle since that's how g++ generates calls to __cxa_atexit as well. However, when running an application built with this atexit under an older Cygwin version, the __cxa_atexit entry point is the one from newlib, which expects the *value* of __dso_handle. So, check for the Cygwin version we're running under. Older version prior to 0.280 don't know CW_FIXED_ATEXIT and return -1. 0.280 and later return 0. */ else if (cygwin_internal (CW_FIXED_ATEXIT) != 0) fixed_dso_handle = __dso_handle; return __cxa_atexit ((void (*)(void*))fn, NULL, fixed_dso_handle); }
static DWORD WINAPI shell_exec_thread(void *data) { wchar *wpath = data; #ifdef __CYGWIN__ /* Need to sync the Windows environment */ cygwin_internal(CW_SYNC_WINENV); #endif if ((INT_PTR)ShellExecuteW(wnd, 0, wpath, 0, 0, SW_SHOWNORMAL) <= 32) { uint error = GetLastError(); if (error != ERROR_CANCELLED) { int msglen = 1024; wchar * msg = newn(wchar, msglen); FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM | 64, 0, error, 0, msg, msglen, 0 ); wchar sep[] = W("\n"); msg = renewn(msg, wcslen(msg) + wcslen(sep) + wcslen(wpath) + 1); wcscat(msg, sep); wcscat(msg, wpath); message_box_w(0, msg, null, MB_ICONERROR, null); } } free(wpath); return 0; }
static err_t get_errcode_from_winerr(DWORD win_error) { uintptr_t res = cygwin_internal(CW_GET_ERRNO_FROM_WINERROR, win_error, EACCES); return res; }
xdo_t xdo_new(void *xdo) { (void)xdo; #if defined(__CYGWIN__) cygwin_internal(CW_SYNC_WINENV); #endif return((xdo_t)1); }
void cygwin_premain0(int argc, char **argv, struct per_process *myself) { static struct __cygwin_perfile pf[] = { { "", O_RDONLY | O_TEXT }, /*{ "", O_WRONLY | O_BINARY },*/ { NULL, 0 } }; cygwin_internal(CW_PERFILE, pf); }
static unsigned long cygwin_version(void) { struct per_process *p; p = (struct per_process *)cygwin_internal(CW_USER_DATA); if (p != NULL) { return (p->dll_major * 1000) + p->dll_minor; } return 0; }
const char * cygwin_ssh_privsep_user() { static char cyg_privsep_user[DNLEN + UNLEN + 2]; if (!cyg_privsep_user[0]) { #ifdef CW_CYGNAME_FROM_WINNAME if (cygwin_internal (CW_CYGNAME_FROM_WINNAME, "sshd", cyg_privsep_user, sizeof cyg_privsep_user) != 0) #endif strlcpy(cyg_privsep_user, "sshd", sizeof(cyg_privsep_user)); } return cyg_privsep_user; }
static int usage (FILE * stream) { fprintf (stream, "Usage: %s [OPTION]...\n" "\n" "Write /etc/group-like output to stdout\n" "\n" "Options:\n" "\n" " -l,--local [machine] print local groups\n" " (from local machine if no machine specified)\n" " -L,--Local machine ditto, but generate groupname with machine prefix\n" " -d,--domain [domain] print domain groups\n" " (from current domain if no domain specified)\n" " -c,--current print current group\n" " -S,--separator char for -l use character char as domain\\group\n" " separator in groupname instead of default '%s'\n" " -o,--id-offset offset change the default offset (0x10000) added to\n" " gids of foreign machine accounts.\n" " -g,--group groupname only return information for the specified group\n" " one of -l, -d must be specified, too\n" " -b,--no-builtin don't print BUILTIN groups\n" " -U,--unix grouplist print UNIX groups when using -l on a UNIX Samba\n" " server. grouplist is a comma-separated list of\n" " groupnames or gid ranges (root,-25,50-100).\n" " (enumerating large ranges can take a long time!)\n" " -h,--help print this message\n" " -v,--version print version information and exit\n" "\n" "Default is to print local groups on stand-alone machines, plus domain\n" "groups on domain controllers and domain member machines.\n\n" "Don't use this command to generate a local /etc/group file, unless you\n" "really need one. See the Cygwin User's Guide for more information.\n" "\n", program_invocation_short_name, (const char *) cygwin_internal (CW_GETNSSSEP)); return 1; }
static void ATTRIBUTE_NORETURN __report_error (const char *msg, ...) { #ifdef __CYGWIN__ /* This function is used to print short error messages * to stderr, which may occur during DLL initialization * while fixing up 'pseudo' relocations. This early, we * may not be able to use cygwin stdio functions, so we * use the win32 WriteFile api. This should work with both * normal win32 console IO handles, redirected ones, and * cygwin ptys. */ char buf[SHORT_MSG_BUF_SZ]; wchar_t module[MAX_PATH]; char * posix_module = NULL; static const char UNKNOWN_MODULE[] = "<unknown module>: "; static const size_t UNKNOWN_MODULE_LEN = sizeof (UNKNOWN_MODULE) - 1; static const char CYGWIN_FAILURE_MSG[] = "Cygwin runtime failure: "; static const size_t CYGWIN_FAILURE_MSG_LEN = sizeof (CYGWIN_FAILURE_MSG) - 1; DWORD len; DWORD done; va_list args; HANDLE errh = GetStdHandle (STD_ERROR_HANDLE); ssize_t modulelen = GetModuleFileNameW (NULL, module, sizeof (module)); if (errh == INVALID_HANDLE_VALUE) cygwin_internal (CW_EXIT_PROCESS, STATUS_ILLEGAL_DLL_PSEUDO_RELOCATION, 1); if (modulelen > 0) posix_module = cygwin_create_path (CCP_WIN_W_TO_POSIX, module); va_start (args, msg); len = (DWORD) vsnprintf (buf, SHORT_MSG_BUF_SZ, msg, args); va_end (args); buf[SHORT_MSG_BUF_SZ-1] = '\0'; /* paranoia */ if (posix_module) { WriteFile (errh, (PCVOID)CYGWIN_FAILURE_MSG, CYGWIN_FAILURE_MSG_LEN, &done, NULL); WriteFile (errh, (PCVOID)posix_module, strlen(posix_module), &done, NULL); WriteFile (errh, (PCVOID)": ", 2, &done, NULL); WriteFile (errh, (PCVOID)buf, len, &done, NULL); free (posix_module); } else { WriteFile (errh, (PCVOID)CYGWIN_FAILURE_MSG, CYGWIN_FAILURE_MSG_LEN, &done, NULL); WriteFile (errh, (PCVOID)UNKNOWN_MODULE, UNKNOWN_MODULE_LEN, &done, NULL); WriteFile (errh, (PCVOID)buf, len, &done, NULL); } WriteFile (errh, (PCVOID)"\n", 1, &done, NULL); cygwin_internal (CW_EXIT_PROCESS, STATUS_ILLEGAL_DLL_PSEUDO_RELOCATION, 1); /* not reached, but silences noreturn warning */ abort (); #else va_list argp; va_start (argp, msg); # ifdef __MINGW64_VERSION_MAJOR fprintf (stderr, "Mingw-w64 runtime failure:\n"); # else fprintf (stderr, "Mingw runtime failure:\n"); # endif vfprintf (stderr, msg, argp); va_end (argp); abort (); #endif }
static void copyCygwinEnvToWin32() { cygwin_internal(CW_SYNC_WINENV); // synchronize the Win32 environment with the Cygwin environment }
int __attribute__((__stdcall__)) DllMain(void *hinstDLL, unsigned long fdwReason, void *lpvReserved) { (void)hinstDLL; (void)fdwReason; (void)lpvReserved; cygwin_internal (CW_HOOK, "_Exit", _Exit); cygwin_internal (CW_HOOK, "_exit", _exit); cygwin_internal (CW_HOOK, "close", close); cygwin_internal (CW_HOOK, "creat", creat); cygwin_internal (CW_HOOK, "execv", execv); cygwin_internal (CW_HOOK, "execve", execve); cygwin_internal (CW_HOOK, "execvp", execvp); cygwin_internal (CW_HOOK, "fopen", fopen); cygwin_internal (CW_HOOK, "freopen", freopen); cygwin_internal (CW_HOOK, "open64", open64); cygwin_internal (CW_HOOK, "fork", fork); cygwin_internal (CW_HOOK, "link", link); cygwin_internal (CW_HOOK, "mkdir", mkdir); cygwin_internal (CW_HOOK, "open", open); cygwin_internal (CW_HOOK, "popen", popen); cygwin_internal (CW_HOOK, "pthread_create", pthread_create); cygwin_internal (CW_HOOK, "pthread_exit", pthread_exit); cygwin_internal (CW_HOOK, "rename", rename); cygwin_internal (CW_HOOK, "symlink", symlink); cygwin_internal (CW_HOOK, "system", system); cygwin_internal (CW_HOOK, "unlink", unlink); cygwin_internal (CW_HOOK, "vfork", vfork); return 1; }
int main(int argc, char **argv) { struct cmdline_args args = {0}; int ret = 0; setvbuf(stderr, NULL, _IOFBF, 0); setvbuf(stdout, NULL, _IOFBF, 0); opdb_reset(); ctrlc_init(); args.devarg.vcc_mv = 3000; args.devarg.requested_serial = NULL; if (parse_cmdline_args(argc, argv, &args) < 0) goto fail_parse; if (args.flags & OPT_EMBEDDED) input_module = &input_async; if (input_module->init() < 0) goto fail_input; output_set_embedded(args.flags & OPT_EMBEDDED); if (sockets_init() < 0) { ret = -1; goto fail_sockets; } printc_dbg("%s", version_text); printc_dbg("%s\n", chipinfo_copyright()); if (setup_driver(&args) < 0) { ret = -1; goto fail_driver; } if (device_probe_id(device_default, args.devarg.forced_chip_id) < 0) printc_err("warning: device ID probe failed\n"); simio_init(); if (!(args.flags & OPT_NO_RC)) process_rc_file(args.alt_config); /* Process commands */ if (optind < argc) { while (optind < argc) { if (process_command(argv[optind++]) < 0) { ret = -1; break; } } } else { reader_loop(); } simio_exit(); device_destroy(); stab_exit(); fail_driver: sockets_exit(); fail_sockets: input_module->exit(); fail_input: fail_parse: /* We need to do this on Windows, because in embedded mode we * may still have a running background thread for input. If so, * returning from main() won't cause the process to terminate. */ #if defined(__CYGWIN__) cygwin_internal(CW_EXIT_PROCESS, (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE, 1); #elif defined(__Windows__) ExitProcess(ret); #endif return ret; }
int main (int argc, char **argv) { int print_domlist = 0; domlist_t domlist[32]; char *opt, *p; int print_current = 0; int print_builtin = 1; char *print_unix = NULL; const char *sep_char = (const char *) cygwin_internal (CW_GETNSSSEP); DWORD id_offset = 0x10000, off; int c, i; char *disp_groupname = NULL; //BOOL in_domain; int optional_args = 0; if (!isatty (1)) setmode (1, O_BINARY); /* Use locale from environment. If not set or set to "C", use UTF-8. */ setlocale (LC_CTYPE, ""); if (!strcmp (setlocale (LC_CTYPE, NULL), "C")) setlocale (LC_CTYPE, "en_US.UTF-8"); fetch_current_pgrp_sid (); if (argc == 1) { int enums = ENUM_PRIMARY | ENUM_LOCAL | ENUM_BUILTIN; uintptr_t ticket = cygwin_internal (CW_SETENT, TRUE, enums, NULL); if (ticket) { struct group *grp; while ((grp = (struct group *) cygwin_internal (CW_GETENT, TRUE, ticket))) printf ("%s:%s:%u:\n", grp->gr_name, grp->gr_passwd, grp->gr_gid); cygwin_internal (CW_ENDENT, TRUE, ticket); } return 0; } unsetenv ("POSIXLY_CORRECT"); /* To get optional arg processing right. */ while ((c = getopt_long (argc, argv, opts, longopts, NULL)) != EOF) switch (c) { case 'd': case 'D': case 'l': case 'L': if (print_domlist >= 32) { fprintf (stderr, "%s: Can not enumerate from more than 32 " "domains and machines.\n", program_invocation_short_name); return 1; } domlist[print_domlist].domain = (c == 'd' || c == 'D'); opt = optarg ?: argv[optind] && argv[optind][0] != '-' ? argv[optind] : NULL; if (argv[optind] && opt == argv[optind]) ++optional_args; for (i = 0; i < print_domlist; ++i) if (domlist[i].domain == domlist[print_domlist].domain && ((!domlist[i].str && !opt) || (domlist[i].str && opt && (off = strlen (domlist[i].str)) && !strncmp (domlist[i].str, opt, off) && (!opt[off] || opt[off] == ',')))) { fprintf (stderr, "%s: Duplicate %s '%s'. Skipping...\n", program_invocation_short_name, domlist[i].domain ? "domain" : "machine", domlist[i].str); goto skip; } domlist[print_domlist].str = opt; if (opt && (p = strchr (opt, ','))) { if (p == opt) { fprintf (stderr, "%s: Malformed machine,offset string '%s'. " "Skipping...\n", program_invocation_short_name, opt); break; } *p = '\0'; } domlist[print_domlist++].with_dom = (c == 'L'); skip: break; case 'S': sep_char = optarg; if (strlen (sep_char) > 1) { fprintf (stderr, "%s: Only one ASCII character allowed as " "domain\\user separator character.\n", program_invocation_short_name); return 1; } if (*sep_char == ':') { fprintf (stderr, "%s: Colon not allowed as domain\\user separator " "character.\n", program_invocation_short_name); return 1; } break; case 'U': print_unix = optarg; break; case 'c': case 'C': print_current = 1; break; case 'o': id_offset = strtol (optarg, NULL, 10); break; case 'b': print_builtin = 0; break; case 's': break; case 'u': break; case 'g': disp_groupname = optarg; break; case 'h': usage (stdout); return 0; case 'V': print_version (); return 0; default: fprintf (stderr, "Try `%s --help' for more information.\n", argv[0]); return 1; } optind += optional_args; if (argv[optind]) { fprintf (stderr, "mkgroup: non-option command line argument `%s' is not allowed.\n" "Try `mkgroup --help' for more information.\n", argv[optind]); exit (1); } struct group *pgrp = NULL; if (print_current) pgrp = (struct group *) cygwin_internal (CW_GETGRSID, TRUE, curr_pgrp.psid); int enums = ENUM_NONE; WCHAR tdoms[print_domlist * 258]; PWCHAR t = tdoms; if (!disp_groupname && print_builtin && print_domlist) enums |= ENUM_BUILTIN; for (i = 0; i < print_domlist; ++i) { if (domlist[i].domain) { if (domlist[i].str) { enums |= ENUM_TDOMS; t += mbstowcs (t, domlist[i].str, 257); *t++ = L'\0'; } else enums |= ENUM_PRIMARY; } else if (!domlist[i].str) enums |= ENUM_LOCAL; } if (t > tdoms) *t++ = L'\0'; if (enums) { uintptr_t ticket = cygwin_internal (CW_SETENT, TRUE, enums, t > tdoms ? tdoms : NULL); if (ticket) { struct group *grp; const char *nss_sep = (const char *) cygwin_internal (CW_GETNSSSEP); while ((grp = (struct group *) cygwin_internal (CW_GETENT, TRUE, ticket))) { if (disp_groupname && strcasecmp (disp_groupname, grp->gr_name) != 0 && (!(p = strchr (grp->gr_name, nss_sep[0])) || strcasecmp (disp_groupname, p + 1) != 0)) continue; printf ("%s:%s:%u:\n", grp->gr_name, grp->gr_passwd, grp->gr_gid); if (pgrp && !strcmp (grp->gr_passwd, pgrp->gr_passwd)) got_curr_pgrp = TRUE; } cygwin_internal (CW_ENDENT, TRUE, ticket); } } if (print_current && !got_curr_pgrp) printf ("%s:%s:%u:\n", pgrp->gr_name, pgrp->gr_passwd, pgrp->gr_gid); off = 0xfd000000; for (i = 0; i < print_domlist; ++i) { if (domlist[i].domain || !domlist[i].str) continue; if (!enum_local_groups (domlist + i, sep_char, off, disp_groupname, print_builtin, print_current)) { enum_groups (domlist + i, sep_char, off, disp_groupname, print_current); if (!domlist[i].domain && domlist[i].str && print_unix) enum_unix_groups (domlist + i, sep_char, 0xff000000, print_unix); off += id_offset; } } return 0; }