Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions winsup/cygwin/fhandler/termios.cc
Original file line number Diff line number Diff line change
Expand Up @@ -702,24 +702,43 @@ fhandler_termios::fstat (struct stat *buf)
}

static bool
is_console_app (const WCHAR *filename)
is_console_app (path_conv &pc)
{
tmp_pathbuf tp;
WCHAR *native_path = tp.w_get ();
pc.get_wide_win32_path (native_path);

wchar_t *e = wcsrchr (native_path, L'.');
if (e && (wcscasecmp (e, L".bat") == 0 || wcscasecmp (e, L".cmd") == 0))
return true;

if (pc.is_app_execution_alias ())
{
UNICODE_STRING upath;
RtlInitUnicodeString (&upath, native_path);
path_conv target (&upath, PC_SYM_FOLLOW);
target.get_wide_win32_path (native_path);
}

HANDLE h;
h = CreateFileW (filename, GENERIC_READ, FILE_SHARE_READ,
h = CreateFileW (native_path, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE)
return true;
char buf[1024];
DWORD n;
ReadFile (h, buf, sizeof (buf), &n, 0);
BOOL res = ReadFile (h, buf, sizeof (buf), &n, 0);
CloseHandle (h);
if (!res)
return true;
/* The offset of Subsystem is the same for both IMAGE_NT_HEADERS32 and
IMAGE_NT_HEADERS64, so only IMAGE_NT_HEADERS32 is used here. */
IMAGE_NT_HEADERS32 *p = (IMAGE_NT_HEADERS32 *) memmem (buf, n, "PE\0\0", 4);
if (p && (char *) &p->OptionalHeader.DllCharacteristics <= buf + n)
return p->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI;
wchar_t *e = wcsrchr (filename, L'.');
if (e && (wcscasecmp (e, L".bat") == 0 || wcscasecmp (e, L".cmd") == 0))
return true;
return false;
/* Return true for unknown to avoid standard handles from being unset.
Setting-up standard handles for GUI apps is pointless, but not unsafe. */
return true;
}

int
Expand Down Expand Up @@ -755,7 +774,7 @@ fhandler_termios::ioctl (unsigned int cmd, void *varg)

void
fhandler_termios::spawn_worker::setup (bool iscygwin, HANDLE h_stdin,
const WCHAR *runpath, bool nopcon,
path_conv &pc, bool nopcon,
bool reset_sendsig,
const WCHAR *envblock)
{
Expand Down Expand Up @@ -794,7 +813,7 @@ fhandler_termios::spawn_worker::setup (bool iscygwin, HANDLE h_stdin,
ptys->setup_locale ();
}
}
if (!iscygwin && ptys_primary && is_console_app (runpath))
if (!iscygwin && ptys_primary && is_console_app (pc))
{
if (h_stdin == ptys_primary->get_handle_nat ())
stdin_is_ptys = true;
Expand Down
2 changes: 1 addition & 1 deletion winsup/cygwin/local_includes/fhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2035,7 +2035,7 @@ class fhandler_termios: public fhandler_base
spawn_worker () :
ptys_need_cleanup (false), cons_need_cleanup (false),
stdin_is_ptys (false), ptys_ttyp (NULL) {}
void setup (bool iscygwin, HANDLE h_stdin, const WCHAR *runpath,
void setup (bool iscygwin, HANDLE h_stdin, path_conv &pc,
bool nopcon, bool reset_sendsig, const WCHAR *envblock);
bool need_cleanup () { return ptys_need_cleanup || cons_need_cleanup; }
void cleanup ();
Expand Down
5 changes: 5 additions & 0 deletions winsup/cygwin/local_includes/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ enum path_types
PATH_SOCKET = _BIT ( 5), /* AF_UNIX socket file */
PATH_RESOLVE_PROCFD = _BIT ( 6), /* fd symlink via /proc */
PATH_REP_NOAPI = _BIT ( 7), /* rep. point unknown to WinAPI */
PATH_APPEXECLINK = _BIT ( 8), /* rep. point app execution alias */
PATH_DONT_USE = _BIT (31) /* conversion to signed happens. */
};

Expand Down Expand Up @@ -214,6 +215,10 @@ class path_conv
{
return (path_flags & (PATH_REP | PATH_REP_NOAPI)) == PATH_REP;
}
int is_app_execution_alias () const
{
return path_flags & PATH_APPEXECLINK;
}

int isfifo () const {return dev.is_device (FH_FIFO);}
int iscygdrive () const {return dev.is_device (FH_CYGDRIVE);}
Expand Down
2 changes: 1 addition & 1 deletion winsup/cygwin/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2661,7 +2661,7 @@ check_reparse_point_target (HANDLE h, bool remote, PREPARSE_DATA_BUFFER rp,
if (i == 2 && n > 0 && n < size)
{
RtlInitCountedUnicodeString (psymbuf, buf, n * sizeof (WCHAR));
return PATH_SYMLINK | PATH_REP;
return PATH_SYMLINK | PATH_REP | PATH_APPEXECLINK;
}
if (i == 2)
break;
Expand Down
2 changes: 1 addition & 1 deletion winsup/cygwin/spawn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ child_info_spawn::worker (const char *prog_arg, const char *const *argv,

bool no_pcon = mode != _P_OVERLAY && mode != _P_WAIT;
term_spawn_worker.setup (iscygwin (), handle (fileno_stdin, false),
runpath, no_pcon, reset_sendsig, envblock);
real_path, no_pcon, reset_sendsig, envblock);

/* Set up needed handles for stdio */
si.dwFlags = STARTF_USESTDHANDLES;
Expand Down
Loading