newlib-cygwin/winsup/cygwin/fhandler_mailslot.cc

171 lines
4.3 KiB
C++
Raw Normal View History

/* fhandler_mailslot.cc. See fhandler.h for a description of the fhandler classes.
Remove unneeded header files from source files throughout. Update copyrights where appropriate. * globals.cc: New file for generic global variables. * mkglobals_h: New file to generate globals.h. * mkstatic: New Script used to build a (currently non-working) static libcygwin_s.a. * Makefile.in: Add unused rule to build a non-working libcygwin_s.a. (DLL_OFILES): Add globals.o. Make all objects rely on globals.h. (globals.h): New target. Generate globals.h. * cygtls.h: Honor new CYGTLS_HANDLE define to control when the HANDLE operator is allowed in _cygtls. * dcrt0.cc: Move most globals to globals.cc. * init.cc: Ditto. * environ.cc (strip_title_path): Remove now-unneeded extern. * fhandler_serial.cc (fhandler_serial::open): Ditto. * pinfo.cc: Ditto. (commune_process): Ditto. * shared.cc: Ditto. * glob.cc: Ditto. * strace.cc: Ditto. * exceptions.cc: Define CYGTLS_HANDLE before including winsup.h. * path.cc (stat_suffixes): Move here. * security.h: Add forward class path_conv declaration. * smallprint.cc (__small_vsprintf): Make a true c++ function. (__small_sprintf): Ditto. (small_printf): Ditto. (console_printf): Ditto. (__small_vswprintf): Ditto. (__small_swprintf): Ditto. * spawn.cc (spawn_guts): Remove _stdcall decoration in favor of regparm. (hExeced): Move to globals.cc * strfuncs.cc (current_codepage): Ditto. (active_codepage): Ditto. * sync.cc (lock_process::locker): Move here from dcrt0.cc. * syscalls.cc (stat_suffixes): Move to path.cc. * tty.cc (tty::create_master): Uncapitalize fatal warning for consistency. * winsup.h: Include globals.h to declare most of the grab bag list of globals which were previously defined here. * mount.h: Move USER_* defines back to shared_info.h. * speclib: Force temporary directory cleanup.
2009-01-03 13:12:22 +08:00
Copyright 2005, 2007, 2008, 2009
Red Hat, Inc.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include "cygerrno.h"
#include "path.h"
#include "fhandler.h"
#include "ntdll.h"
/**********************************************************************/
/* fhandler_mailslot */
fhandler_mailslot::fhandler_mailslot ()
: fhandler_base ()
{
}
int __stdcall
fhandler_mailslot::fstat (struct __stat64 *buf)
{
debug_printf ("here");
fhandler_base::fstat (buf);
if (is_auto_device ())
{
buf->st_mode = S_IFCHR | S_IRUSR | S_IWUSR;
buf->st_uid = geteuid32 ();
buf->st_gid = getegid32 ();
buf->st_nlink = 1;
buf->st_blksize = PREFERRED_IO_BLKSIZE;
time_as_timestruc_t (&buf->st_ctim);
buf->st_atim = buf->st_mtim = buf->st_birthtim = buf->st_ctim;
}
return 0;
}
int
fhandler_mailslot::open (int flags, mode_t mode)
{
int res = 0;
NTSTATUS status;
IO_STATUS_BLOCK io;
OBJECT_ATTRIBUTES attr;
HANDLE x;
LARGE_INTEGER timeout;
switch (flags & O_ACCMODE)
{
case O_RDONLY: /* Server */
timeout.QuadPart = (flags & O_NONBLOCK) ? 0LL : 0x8000000000000000LL;
status = NtCreateMailslotFile (&x, GENERIC_READ | SYNCHRONIZE,
pc.get_object_attr (attr, sec_none),
&io, FILE_SYNCHRONOUS_IO_NONALERT,
0, 0, &timeout);
if (!NT_SUCCESS (status))
2005-08-12 10:39:13 +08:00
{
/* FIXME: It's not possible to open the read side of an existing
mailslot again. You'll get a handle, but using it in ReadFile
returns ERROR_INVALID_PARAMETER. On the other hand,
NtCreateMailslotFile returns with STATUS_OBJECT_NAME_EXISTS if
the mailslot has been created already.
So this is an exclusive open for now. *Duplicating* read side
handles works, though, so it might be an option to duplicate
the handle from the first process to the current process for
opening the mailslot. */
#if 0
if (status != STATUS_OBJECT_NAME_COLLISION)
{
__seterrno_from_nt_status (status);
break;
}
status = NtOpenFile (&x, GENERIC_READ | SYNCHRONIZE,
pc.get_object_attr (attr, sec_none), &io,
FILE_SHARE_VALID_FLAGS,
FILE_SYNCHRONOUS_IO_NONALERT);
#endif
if (!NT_SUCCESS (status))
{
__seterrno_from_nt_status (status);
break;
}
}
set_io_handle (x);
set_flags (flags, O_BINARY);
res = 1;
set_open_status ();
break;
case O_WRONLY: /* Client */
/* The client is the DLL exclusively. Don't allow opening from
2005-08-12 10:39:13 +08:00
application code. */
extern fhandler_mailslot *dev_kmsg;
if (this != dev_kmsg)
2005-08-12 10:39:13 +08:00
{
set_errno (EPERM); /* As on Linux. */
break;
}
status = NtOpenFile (&x, GENERIC_WRITE | SYNCHRONIZE,
pc.get_object_attr (attr, sec_none), &io,
FILE_SHARE_VALID_FLAGS,
FILE_SYNCHRONOUS_IO_NONALERT);
if (!NT_SUCCESS (status))
{
__seterrno_from_nt_status (status);
break;
}
set_io_handle (x);
set_flags (flags, O_BINARY);
res = 1;
set_open_status ();
break;
default:
set_errno (EINVAL);
break;
}
return res;
}
* sigproc.h (wait_for_sigthread): Eliminate parameter. * sigproc.cc (wait_for_sigthread): Ditto. Don't synchronize with wait_sig after receiving an event that it is ready to go. (init_sig_pipe): New function. (wait_sig): Call init_sig_pipe to create pipes for communicating signals to this process. Don't send sigCONT signal when initializing. * fork.cc (frok::child): Accommodate wait_for_sigpipe parameter change. * fhandler.h (fhandler_*::write): Make ssize_t/__stdcall. (fhandler_*::write_overlapped): Ditto. (fhandler_*::raw_write): Ditto. (fhandler_*::readv): Ditto. (fhandler_*::writev): Ditto. (fhandler_*::raw_read): Make __stdcall. * fhandler: Accommodate changes to read/write functions throughout. * fhandler_clipboard.cc: Ditto. * fhandler_console.cc: Ditto. * fhandler_dsp.cc: Ditto. * fhandler_fifo.cc: Ditto. * fhandler_mailslot.cc: Ditto. * fhandler_mem.cc: Ditto. * fhandler_mem.cc: Ditto. * fhandler_random.cc: Ditto. * fhandler_tape.cc: Ditto. * fhandler_tty.cc: Ditto. * fhandler_virtual.cc: Ditto. * fhandler_windows.cc: Ditto. * fhandler_zero.cc: Ditto. * syscalls.cc (readv): Use ssize_t as temp variable. * fhandler.cc (fhandler_base::read): Coerce returned len to signed or it will never be treated as < 0. (fhandler_base::wait_overlapped): Minimize calls to GetLastError. Remove duplicate debugging test. Fix error return. * fhandler.h (fhandler_fifo::fifo_name): Declare new function. (fhandler_fifo::close): Ditto. (fhandler_fifo::dup): Ditto. (fhandler_fifo::close_on_exec): Ditto. * fhandler.cc (fhandler_fifo::fifo_name): Define new function. (FIFO_BUF_SIZE): New define. (cnp): Ditto. (fhandler_fifo::open): Rework. Use cnp to open named pipe. Always open write side as a client. Open dummy client when writing and can't connect. (wait): Rework. Implement fifo_wait_for_next_client. Handle signals during connect better. Add new fifo_wait_for_server code which polls (sigh) waiting for server. (fhandler_fifo::raw_read): Handle transition states when one client closes and another is available. (fhandler_fifo::close): Define. (fhandler_fifo::dup): Ditto. (fhandler_fifo::close_on_exec): Ditto.
2009-07-25 04:54:33 +08:00
ssize_t __stdcall
fhandler_mailslot::write (const void *ptr, size_t len)
{
/* Check for 425/426 byte weirdness */
if (len == 425 || len == 426)
{
char buf[427];
buf[425] = buf[426] = '\0';
memcpy (buf, ptr, len);
return raw_write (buf, 427);
}
return raw_write (ptr, len);
}
int
fhandler_mailslot::ioctl (unsigned int cmd, void *buf)
{
int res = -1;
NTSTATUS status;
IO_STATUS_BLOCK io;
switch (cmd)
{
case FIONBIO:
{
FILE_MAILSLOT_SET_INFORMATION fmsi;
fmsi.ReadTimeout.QuadPart = buf ? 0LL : 0x8000000000000000LL;
status = NtSetInformationFile (get_handle (), &io, &fmsi, sizeof fmsi,
FileMailslotSetInformation);
if (!NT_SUCCESS (status))
{
debug_printf ("NtSetInformationFile (%X): %08x",
fmsi.ReadTimeout.QuadPart, status);
__seterrno_from_nt_status (status);
break;
}
}
/*FALLTHRU*/
default:
res = fhandler_base::ioctl (cmd, buf);
break;
}
return res;
}