2005-05-11 04:56:07 +08:00
|
|
|
/* fhandler_mailslot.cc. See fhandler.h for a description of the fhandler classes.
|
|
|
|
|
2013-01-21 12:34:52 +08:00
|
|
|
Copyright 2005, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Red Hat, Inc.
|
2005-05-11 04:56:07 +08:00
|
|
|
|
|
|
|
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"
|
2012-02-14 19:27:43 +08:00
|
|
|
#include "security.h"
|
2005-05-11 04:56:07 +08:00
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
2012-02-14 19:27:43 +08:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2005-05-11 04:56:07 +08:00
|
|
|
#include "ntdll.h"
|
2009-11-12 02:16:57 +08:00
|
|
|
#include "shared_info.h"
|
|
|
|
#include "tls_pbuf.h"
|
2005-05-11 04:56:07 +08:00
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
/* fhandler_mailslot */
|
|
|
|
|
|
|
|
fhandler_mailslot::fhandler_mailslot ()
|
2011-05-04 20:56:12 +08:00
|
|
|
: fhandler_base_overlapped ()
|
2005-05-11 04:56:07 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-21 12:34:52 +08:00
|
|
|
int __reg2
|
2013-04-23 17:44:36 +08:00
|
|
|
fhandler_mailslot::fstat (struct stat *buf)
|
2005-05-11 04:56:07 +08:00
|
|
|
{
|
|
|
|
debug_printf ("here");
|
|
|
|
|
2011-05-04 20:56:12 +08:00
|
|
|
fhandler_base_overlapped::fstat (buf);
|
2005-05-11 04:56:07 +08:00
|
|
|
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;
|
2007-01-04 17:17:55 +08:00
|
|
|
buf->st_blksize = PREFERRED_IO_BLKSIZE;
|
2005-05-11 04:56:07 +08:00
|
|
|
time_as_timestruc_t (&buf->st_ctim);
|
2007-03-06 22:48:25 +08:00
|
|
|
buf->st_atim = buf->st_mtim = buf->st_birthtim = buf->st_ctim;
|
2005-05-11 04:56:07 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-12 02:16:57 +08:00
|
|
|
POBJECT_ATTRIBUTES
|
|
|
|
fhandler_mailslot::get_object_attr (OBJECT_ATTRIBUTES &attr,
|
2010-01-15 02:46:02 +08:00
|
|
|
PUNICODE_STRING path,
|
|
|
|
int flags)
|
2009-11-12 02:16:57 +08:00
|
|
|
{
|
2011-06-06 13:02:13 +08:00
|
|
|
|
2009-11-12 02:16:57 +08:00
|
|
|
RtlCopyUnicodeString (path, pc.get_nt_native_path ());
|
2012-02-14 19:27:43 +08:00
|
|
|
RtlAppendUnicodeStringToString (path, &cygheap->installation_key);
|
2010-01-15 02:46:02 +08:00
|
|
|
InitializeObjectAttributes (&attr, path,
|
|
|
|
OBJ_CASE_INSENSITIVE
|
|
|
|
| (flags & O_CLOEXEC ? 0 : OBJ_INHERIT),
|
2009-11-12 02:16:57 +08:00
|
|
|
NULL, NULL);
|
|
|
|
return &attr;
|
|
|
|
}
|
|
|
|
|
2005-05-11 04:56:07 +08:00
|
|
|
int
|
|
|
|
fhandler_mailslot::open (int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
int res = 0;
|
2007-08-13 23:08:25 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
2005-05-11 04:56:07 +08:00
|
|
|
HANDLE x;
|
2007-08-13 23:08:25 +08:00
|
|
|
LARGE_INTEGER timeout;
|
2009-11-12 02:16:57 +08:00
|
|
|
tmp_pathbuf tp;
|
|
|
|
UNICODE_STRING path;
|
2005-05-11 04:56:07 +08:00
|
|
|
|
2009-11-12 02:16:57 +08:00
|
|
|
tp.u_get (&path);
|
2011-06-06 13:02:13 +08:00
|
|
|
|
2005-05-11 04:56:07 +08:00
|
|
|
switch (flags & O_ACCMODE)
|
|
|
|
{
|
|
|
|
case O_RDONLY: /* Server */
|
2007-08-13 23:08:25 +08:00
|
|
|
timeout.QuadPart = (flags & O_NONBLOCK) ? 0LL : 0x8000000000000000LL;
|
|
|
|
status = NtCreateMailslotFile (&x, GENERIC_READ | SYNCHRONIZE,
|
2010-01-15 02:46:02 +08:00
|
|
|
get_object_attr (attr, &path, flags),
|
2007-08-13 23:08:25 +08:00
|
|
|
&io, FILE_SYNCHRONOUS_IO_NONALERT,
|
|
|
|
0, 0, &timeout);
|
|
|
|
if (!NT_SUCCESS (status))
|
2005-08-12 10:39:13 +08:00
|
|
|
{
|
2005-05-11 04:56:07 +08:00
|
|
|
/* FIXME: It's not possible to open the read side of an existing
|
2007-08-13 23:08:25 +08:00
|
|
|
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.
|
2005-05-11 04:56:07 +08:00
|
|
|
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
|
2007-08-13 23:08:25 +08:00
|
|
|
if (status != STATUS_OBJECT_NAME_COLLISION)
|
2005-05-11 04:56:07 +08:00
|
|
|
{
|
2007-08-13 23:08:25 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2005-05-11 04:56:07 +08:00
|
|
|
break;
|
|
|
|
}
|
2007-08-13 23:08:25 +08:00
|
|
|
status = NtOpenFile (&x, GENERIC_READ | SYNCHRONIZE,
|
2010-01-15 02:46:02 +08:00
|
|
|
get_object_attr (attr, &path, flags), &io,
|
2007-08-13 23:08:25 +08:00
|
|
|
FILE_SHARE_VALID_FLAGS,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT);
|
2005-05-11 04:56:07 +08:00
|
|
|
#endif
|
2007-08-13 23:08:25 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
2005-05-11 04:56:07 +08:00
|
|
|
{
|
2007-08-13 23:08:25 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2005-05-11 04:56:07 +08:00
|
|
|
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. */
|
2005-05-11 04:56:07 +08:00
|
|
|
extern fhandler_mailslot *dev_kmsg;
|
|
|
|
if (this != dev_kmsg)
|
2005-08-12 10:39:13 +08:00
|
|
|
{
|
2005-05-11 04:56:07 +08:00
|
|
|
set_errno (EPERM); /* As on Linux. */
|
|
|
|
break;
|
|
|
|
}
|
2007-08-13 23:08:25 +08:00
|
|
|
status = NtOpenFile (&x, GENERIC_WRITE | SYNCHRONIZE,
|
2010-01-15 02:46:02 +08:00
|
|
|
get_object_attr (attr, &path, flags), &io,
|
2011-05-04 20:56:12 +08:00
|
|
|
FILE_SHARE_VALID_FLAGS, 0);
|
2007-08-13 23:08:25 +08:00
|
|
|
if (!NT_SUCCESS (status))
|
2005-05-11 04:56:07 +08:00
|
|
|
{
|
2007-08-13 23:08:25 +08:00
|
|
|
__seterrno_from_nt_status (status);
|
2005-05-11 04:56:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
set_io_handle (x);
|
|
|
|
set_flags (flags, O_BINARY);
|
|
|
|
res = 1;
|
|
|
|
set_open_status ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
set_errno (EINVAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-07-25 04:54:33 +08:00
|
|
|
ssize_t __stdcall
|
2011-05-04 20:56:12 +08:00
|
|
|
fhandler_mailslot::raw_write (const void *ptr, size_t len)
|
2005-05-11 04:56:07 +08:00
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
}
|
2011-05-06 01:44:42 +08:00
|
|
|
return fhandler_base_overlapped::raw_write (ptr, len);
|
2005-05-11 04:56:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-06-11 02:44:26 +08:00
|
|
|
fhandler_mailslot::ioctl (unsigned int cmd, void *buf)
|
2005-05-11 04:56:07 +08:00
|
|
|
{
|
|
|
|
int res = -1;
|
2007-08-13 23:08:25 +08:00
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
2005-05-11 04:56:07 +08:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case FIONBIO:
|
|
|
|
{
|
2007-08-13 23:08:25 +08:00
|
|
|
FILE_MAILSLOT_SET_INFORMATION fmsi;
|
|
|
|
fmsi.ReadTimeout.QuadPart = buf ? 0LL : 0x8000000000000000LL;
|
|
|
|
status = NtSetInformationFile (get_handle (), &io, &fmsi, sizeof fmsi,
|
|
|
|
FileMailslotSetInformation);
|
|
|
|
if (!NT_SUCCESS (status))
|
2005-05-11 04:56:07 +08:00
|
|
|
{
|
2013-04-23 17:44:36 +08:00
|
|
|
debug_printf ("NtSetInformationFile (%X): %p",
|
2007-08-13 23:08:25 +08:00
|
|
|
fmsi.ReadTimeout.QuadPart, status);
|
|
|
|
__seterrno_from_nt_status (status);
|
2005-05-11 04:56:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*FALLTHRU*/
|
|
|
|
default:
|
2011-05-04 20:56:12 +08:00
|
|
|
res = fhandler_base_overlapped::ioctl (cmd, buf);
|
2005-05-11 04:56:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|