1 /* fhandler_mailslot.cc. See fhandler.h for a description of the fhandler classes.
3 Copyright 2005, 2007, 2008, 2009
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
18 #include "shared_info.h"
21 /**********************************************************************/
22 /* fhandler_mailslot */
24 fhandler_mailslot::fhandler_mailslot ()
30 fhandler_mailslot::fstat (struct __stat64 *buf)
32 debug_printf ("here");
34 fhandler_base::fstat (buf);
35 if (is_auto_device ())
37 buf->st_mode = S_IFCHR | S_IRUSR | S_IWUSR;
38 buf->st_uid = geteuid32 ();
39 buf->st_gid = getegid32 ();
41 buf->st_blksize = PREFERRED_IO_BLKSIZE;
42 time_as_timestruc_t (&buf->st_ctim);
43 buf->st_atim = buf->st_mtim = buf->st_birthtim = buf->st_ctim;
49 fhandler_mailslot::get_object_attr (OBJECT_ATTRIBUTES &attr,
53 RtlCopyUnicodeString (path, pc.get_nt_native_path ());
54 RtlAppendUnicodeStringToString (path, &installation_key);
55 InitializeObjectAttributes (&attr, path, OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
61 fhandler_mailslot::open (int flags, mode_t mode)
66 OBJECT_ATTRIBUTES attr;
68 LARGE_INTEGER timeout;
74 switch (flags & O_ACCMODE)
76 case O_RDONLY: /* Server */
77 timeout.QuadPart = (flags & O_NONBLOCK) ? 0LL : 0x8000000000000000LL;
78 status = NtCreateMailslotFile (&x, GENERIC_READ | SYNCHRONIZE,
79 get_object_attr (attr, &path),
80 &io, FILE_SYNCHRONOUS_IO_NONALERT,
82 if (!NT_SUCCESS (status))
84 /* FIXME: It's not possible to open the read side of an existing
85 mailslot again. You'll get a handle, but using it in ReadFile
86 returns ERROR_INVALID_PARAMETER. On the other hand,
87 NtCreateMailslotFile returns with STATUS_OBJECT_NAME_EXISTS if
88 the mailslot has been created already.
89 So this is an exclusive open for now. *Duplicating* read side
90 handles works, though, so it might be an option to duplicate
91 the handle from the first process to the current process for
92 opening the mailslot. */
94 if (status != STATUS_OBJECT_NAME_COLLISION)
96 __seterrno_from_nt_status (status);
99 status = NtOpenFile (&x, GENERIC_READ | SYNCHRONIZE,
100 get_object_attr (attr, &path), &io,
101 FILE_SHARE_VALID_FLAGS,
102 FILE_SYNCHRONOUS_IO_NONALERT);
104 if (!NT_SUCCESS (status))
106 __seterrno_from_nt_status (status);
111 set_flags (flags, O_BINARY);
115 case O_WRONLY: /* Client */
116 /* The client is the DLL exclusively. Don't allow opening from
118 extern fhandler_mailslot *dev_kmsg;
119 if (this != dev_kmsg)
121 set_errno (EPERM); /* As on Linux. */
124 status = NtOpenFile (&x, GENERIC_WRITE | SYNCHRONIZE,
125 get_object_attr (attr, &path), &io,
126 FILE_SHARE_VALID_FLAGS,
127 FILE_SYNCHRONOUS_IO_NONALERT);
128 if (!NT_SUCCESS (status))
130 __seterrno_from_nt_status (status);
134 set_flags (flags, O_BINARY);
146 fhandler_mailslot::write (const void *ptr, size_t len)
148 /* Check for 425/426 byte weirdness */
149 if (len == 425 || len == 426)
152 buf[425] = buf[426] = '\0';
153 memcpy (buf, ptr, len);
154 return raw_write (buf, 427);
156 return raw_write (ptr, len);
160 fhandler_mailslot::ioctl (unsigned int cmd, void *buf)
170 FILE_MAILSLOT_SET_INFORMATION fmsi;
171 fmsi.ReadTimeout.QuadPart = buf ? 0LL : 0x8000000000000000LL;
172 status = NtSetInformationFile (get_handle (), &io, &fmsi, sizeof fmsi,
173 FileMailslotSetInformation);
174 if (!NT_SUCCESS (status))
176 debug_printf ("NtSetInformationFile (%X): %08x",
177 fmsi.ReadTimeout.QuadPart, status);
178 __seterrno_from_nt_status (status);
184 res = fhandler_base::ioctl (cmd, buf);