KERNEL32: implement mailslots

Mike McCormack mike at codeweavers.com
Mon Mar 21 10:05:54 CST 2005


Hi All,

This is an implementation of mailslots.  It has some known limitations:

* works for processes sharing the same wineserver
* does not work over the network
* does not get message counts and message sizes right all the time
* does not work for zero byte reads and writes

To apply this patch:

cd wine
patch -p0 < mailslot-implementation.diff
tools/make_requests
./configure
make depend
make

Test program at http://mandoo.dyndns.org/winetests/mailslot.zip
which is mostly the same as the mailslot regression test in 
dlls/kernel32/tests/mailslot.c

Mike


ChangeLog:
* implement mailslots
-------------- next part --------------
Index: dlls/ntdll/file.c
===================================================================
RCS file: /home/wine/wine/dlls/ntdll/file.c,v
retrieving revision 1.78
diff -u -p -r1.78 file.c
--- dlls/ntdll/file.c	21 Mar 2005 10:28:23 -0000	1.78
+++ dlls/ntdll/file.c	21 Mar 2005 15:48:10 -0000
@@ -130,6 +130,7 @@ NTSTATUS WINAPI NtCreateFile( PHANDLE ha
                               ULONG options, PVOID ea_buffer, ULONG ea_length )
 {
     static const WCHAR pipeW[] = {'\\','?','?','\\','p','i','p','e','\\'};
+    static const WCHAR mailslotW[] = {'\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
     ANSI_STRING unix_name;
     int created = FALSE;
 
@@ -166,6 +167,25 @@ NTSTATUS WINAPI NtCreateFile( PHANDLE ha
         return io->u.Status;
     }
 
+    /* check for mailslot */
+
+    if (attr->ObjectName->Length > sizeof(mailslotW) &&
+        !memicmpW( attr->ObjectName->Buffer, mailslotW, sizeof(mailslotW)/sizeof(WCHAR) ))
+    {
+        SERVER_START_REQ( open_mailslot )
+        {
+            req->access = access;
+            req->sharing = sharing;
+            req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
+            wine_server_add_data( req, attr->ObjectName->Buffer + 4,
+                                  attr->ObjectName->Length - 4*sizeof(WCHAR) );
+            io->u.Status = wine_server_call( req );
+            *handle = reply->handle;
+        }
+        SERVER_END_REQ;
+        return io->u.Status;
+    }
+
     io->u.Status = wine_nt_to_unix_file_name( attr->ObjectName, &unix_name, disposition,
                                               !(attr->Attributes & OBJ_CASE_INSENSITIVE) );
 
@@ -442,6 +462,12 @@ NTSTATUS WINAPI NtReadFile(HANDLE hFile,
     io_status->u.Status = wine_server_handle_to_fd( hFile, GENERIC_READ, &unix_handle, &flags );
     if (io_status->u.Status) return io_status->u.Status;
 
+    if (flags & FD_FLAG_MAILSLOT_EMPTY)
+    {
+        wine_server_release_fd( hFile, unix_handle );
+        return STATUS_IO_TIMEOUT;
+    }
+
     if (flags & FD_FLAG_RECV_SHUTDOWN)
     {
         wine_server_release_fd( hFile, unix_handle );
@@ -1738,4 +1764,57 @@ NTSTATUS WINAPI NtCancelIoFile( HANDLE h
      */
     timeout.u.LowPart = timeout.u.HighPart = 0;
     return io_status->u.Status = NtDelayExecution( TRUE, &timeout );
+}
+
+/******************************************************************************
+ *  NtCreateMailslotFile	[NTDLL.@]
+ *  ZwCreateMailslotFile	[NTDLL.@]
+ *
+ * PARAMS
+ *  pHandle          [O] pointer to receive the handle created
+ *  DesiredAccess    [I] access mode (read, write, etc)
+ *  ObjectAttributes [I] fully qualified NT path of the mailslot
+ *  IoStatusBlock    [O] receives completion status and other info
+ *  CreateOptions    [I]
+ *  MailslotQuota    [I]
+ *  MaxMessageSize   [I]
+ *  TimeOut          [I]
+ *
+ * RETURNS
+ *  An NT status code
+ */
+NTSTATUS WINAPI NtCreateMailslotFile(PHANDLE pHandle, ULONG DesiredAccess,
+     POBJECT_ATTRIBUTES attr, PIO_STATUS_BLOCK IoStatusBlock,
+     ULONG CreateOptions, ULONG MailslotQuota, ULONG MaxMessageSize,
+     PLARGE_INTEGER TimeOut)
+{
+    static const WCHAR leadin[] = {
+        '\\','?','?','\\','M','A','I','L','S','L','O','T','\\'};
+    NTSTATUS ret;
+
+    TRACE("%p %08lx %p %p %08lx %08lx %08lx %p\n",
+              pHandle, DesiredAccess, attr, IoStatusBlock,
+              CreateOptions, MailslotQuota, MaxMessageSize, TimeOut);
+
+    if (attr->ObjectName->Length < sizeof(leadin) ||
+        strncmpiW( attr->ObjectName->Buffer, 
+                   leadin, sizeof(leadin)/sizeof(leadin[0]) ))
+    {
+        return STATUS_OBJECT_NAME_INVALID;
+    }
+
+    SERVER_START_REQ( create_mailslot )
+    {
+        req->max_msgsize = MaxMessageSize;
+        req->read_timeout = TimeOut->QuadPart / -10000;
+        req->inherit = (attr->Attributes & OBJ_INHERIT) != 0;
+        wine_server_add_data( req, attr->ObjectName->Buffer + 4,
+                              attr->ObjectName->Length - 4*sizeof(WCHAR) );
+        ret = wine_server_call( req );
+        if( ret == STATUS_SUCCESS )
+            *pHandle = reply->handle;
+    }
+    SERVER_END_REQ;
+ 
+    return ret;
 }
Index: dlls/ntdll/nt.c
===================================================================
RCS file: /home/wine/wine/dlls/ntdll/nt.c,v
retrieving revision 1.74
diff -u -p -r1.74 nt.c
--- dlls/ntdll/nt.c	23 Feb 2005 20:29:17 -0000	1.74
+++ dlls/ntdll/nt.c	21 Mar 2005 15:48:10 -0000
@@ -540,16 +540,6 @@ NTSTATUS WINAPI NtQueryPerformanceCounte
 }
 
 /******************************************************************************
- *  NtCreateMailslotFile	[NTDLL.@]
- *  ZwCreateMailslotFile	[NTDLL.@]
- */
-NTSTATUS WINAPI NtCreateMailslotFile(DWORD x1,DWORD x2,DWORD x3,DWORD x4,DWORD x5,DWORD x6,DWORD x7,DWORD x8)
-{
-	FIXME("(0x%08lx,0x%08lx,0x%08lx,0x%08lx,0x%08lx,0x%08lx,0x%08lx,0x%08lx),stub!\n",x1,x2,x3,x4,x5,x6,x7,x8);
-	return 0;
-}
-
-/******************************************************************************
  * NtQuerySystemInformation [NTDLL.@]
  * ZwQuerySystemInformation [NTDLL.@]
  *
Index: dlls/kernel/file.c
===================================================================
RCS file: /home/wine/wine/dlls/kernel/file.c,v
retrieving revision 1.36
diff -u -p -r1.36 file.c
--- dlls/kernel/file.c	17 Mar 2005 19:00:10 -0000	1.36
+++ dlls/kernel/file.c	21 Mar 2005 15:48:10 -0000
@@ -1217,9 +1217,11 @@ HANDLE WINAPI CreateFileW( LPCWSTR filen
     if (!strncmpW(filename, bkslashes_with_dotW, 4))
     {
         static const WCHAR pipeW[] = {'P','I','P','E','\\',0};
+        static const WCHAR mailslotW[] = {'M','A','I','L','S','L','O','T','\\',0};
 
         if ((isalphaW(filename[4]) && filename[5] == ':' && filename[6] == '\0') ||
-            !strncmpiW( filename + 4, pipeW, 5 ))
+            !strncmpiW( filename + 4, pipeW, 5 ) ||
+            !strncmpiW( filename + 4, mailslotW, 9 ))
         {
             dosdev = 0;
         }
Index: dlls/kernel/sync.c
===================================================================
RCS file: /home/wine/wine/dlls/kernel/sync.c,v
retrieving revision 1.71
diff -u -p -r1.71 sync.c
--- dlls/kernel/sync.c	4 Mar 2005 12:38:37 -0000	1.71
+++ dlls/kernel/sync.c	21 Mar 2005 15:48:10 -0000
@@ -1574,10 +1574,56 @@ HANDLE WINAPI CreateMailslotA( LPCSTR lp
 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
                                DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
 {
-    FIXME("(%s,%ld,%ld,%p): stub\n", debugstr_w(lpName),
+    HANDLE handle = INVALID_HANDLE_VALUE;
+    OBJECT_ATTRIBUTES attr;
+    UNICODE_STRING nameW;
+    LARGE_INTEGER timeout;
+    IO_STATUS_BLOCK iosb;
+    NTSTATUS status;
+
+    TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
           nMaxMessageSize, lReadTimeout, sa);
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return INVALID_HANDLE_VALUE;
+
+    if (!lpName)
+    {
+        SetLastError( ERROR_PATH_NOT_FOUND );
+        return INVALID_HANDLE_VALUE;
+    }
+
+    if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
+    {
+        ERR("invalid name\n");
+        RtlFreeUnicodeString( &nameW );
+        return INVALID_HANDLE_VALUE;
+    }
+
+    if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
+    {
+        SetLastError( ERROR_FILENAME_EXCED_RANGE );
+        RtlFreeUnicodeString( &nameW );
+        return INVALID_HANDLE_VALUE;
+    }
+
+    attr.Length = sizeof(attr);
+    attr.RootDirectory = 0;
+    attr.Attributes = OBJ_CASE_INSENSITIVE;
+    attr.ObjectName = &nameW;
+    attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
+    attr.SecurityQualityOfService = NULL;
+
+    timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;;
+
+    SetLastError(0);
+
+    status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
+                                   &iosb, 0, 0, nMaxMessageSize, &timeout );
+    if (status)
+    {
+        SetLastError( RtlNtStatusToDosError(status) );
+        handle = INVALID_HANDLE_VALUE;
+    }
+
+    return handle;
 }
 
 
@@ -1601,9 +1647,32 @@ BOOL WINAPI GetMailslotInfo( HANDLE hMai
                                LPDWORD lpNextSize, LPDWORD lpMessageCount,
                                LPDWORD lpReadTimeout )
 {
-    FIXME("(%p): stub\n",hMailslot);
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return FALSE;
+    BOOL r;
+
+    TRACE("%p %p %p %p %p\n",hMailslot,
+          lpMaxMessageSize,lpNextSize,lpMessageCount,lpReadTimeout);
+
+    SERVER_START_REQ( mailslot_info )
+    {
+        req->handle = hMailslot;
+        req->flags = 0;
+        SetLastError(0);
+        r = !wine_server_call_err( req );
+        if( r )
+        {
+            if( lpMaxMessageSize )
+                *lpMaxMessageSize = reply->max_msgsize;
+            if( lpNextSize )
+                *lpNextSize = reply->next_msgsize;
+            if( lpMessageCount )
+                *lpMessageCount = reply->msg_count;
+            if( lpReadTimeout )
+                *lpReadTimeout = reply->read_timeout;
+        }
+    }
+    SERVER_END_REQ;
+
+    return r;
 }
 
 
@@ -1622,9 +1691,21 @@ BOOL WINAPI GetMailslotInfo( HANDLE hMai
  */
 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
 {
-    FIXME("%p %ld: stub\n", hMailslot, dwReadTimeout);
-    SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
-    return FALSE;
+    BOOL r;
+
+    TRACE("%p %ld\n", hMailslot, dwReadTimeout);
+
+    SERVER_START_REQ( mailslot_info )
+    {
+        req->handle = hMailslot;
+        req->flags = MAILSLOT_SET_INFO;
+        req->read_timeout = dwReadTimeout;
+        SetLastError(0);
+        r = !wine_server_call_err( req );
+    }
+    SERVER_END_REQ;
+
+    return r;
 }
 
 
Index: server/Makefile.in
===================================================================
RCS file: /home/wine/wine/server/Makefile.in,v
retrieving revision 1.53
diff -u -p -r1.53 Makefile.in
--- server/Makefile.in	14 Jan 2005 19:54:39 -0000	1.53
+++ server/Makefile.in	21 Mar 2005 15:48:10 -0000
@@ -21,6 +21,7 @@ C_SRCS = \
 	file.c \
 	handle.c \
 	hook.c \
+	mailslot.c \
 	main.c \
 	mapping.c \
 	mutex.c \
Index: server/protocol.def
===================================================================
RCS file: /home/wine/wine/server/protocol.def,v
retrieving revision 1.123
diff -u -p -r1.123 protocol.def
--- server/protocol.def	21 Mar 2005 12:37:00 -0000	1.123
+++ server/protocol.def	21 Mar 2005 15:48:11 -0000
@@ -616,6 +616,7 @@ enum event_op { PULSE_EVENT, SET_EVENT, 
 #define FD_FLAG_SEND_SHUTDOWN      0x08
 #define FD_FLAG_AVAILABLE          0x10 /* in overlap read/write operation, 
                                          * only handle available data (don't wait) */
+#define FD_FLAG_MAILSLOT_EMPTY     0x20 /* mailslot has no data waiting */
 
 
 /* Flush a file buffers */
@@ -2265,4 +2266,38 @@ enum message_type
     int           impersonation_level; /* impersonation level of the new token */
 @REPLY
     obj_handle_t  new_handle; /* duplicated handle */
+ at END
+
+/* create a mailslot */
+ at REQ(create_mailslot)
+    unsigned int   max_msgsize;
+    unsigned int   read_timeout;
+    int            inherit;
+    VARARG(name,unicode_str); /* mailslot name */
+ at REPLY
+    obj_handle_t   handle;    /* handle to the mailslot */
+ at END
+
+
+/* Open an existing mailslot */
+ at REQ(open_mailslot)
+    unsigned int   access;
+    int            inherit;      /* inherit flag */
+    int            sharing;      /* sharing mode */
+    VARARG(name,unicode_str);    /* mailslot name */
+ at REPLY
+    obj_handle_t   handle;       /* handle to the mailslot */
+ at END
+
+
+#define MAILSLOT_SET_INFO  1
+ at REQ(mailslot_info)
+    obj_handle_t   handle;    /* handle to the mailslot */
+    int            flags;
+    unsigned int   read_timeout;
+ at REPLY
+    unsigned int   max_msgsize;
+    unsigned int   read_timeout;
+    unsigned int   msg_count;
+    unsigned int   next_msgsize;
 @END
Index: include/winternl.h
===================================================================
RCS file: /home/wine/wine/include/winternl.h,v
retrieving revision 1.110
diff -u -p -r1.110 winternl.h
--- include/winternl.h	19 Mar 2005 17:14:12 -0000	1.110
+++ include/winternl.h	21 Mar 2005 15:48:11 -0000
@@ -1424,6 +1424,7 @@ NTSTATUS  WINAPI NtCreateEvent(PHANDLE,A
 NTSTATUS  WINAPI NtCreateFile(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,PLARGE_INTEGER,ULONG,ULONG,ULONG,ULONG,PVOID,ULONG);
 NTSTATUS  WINAPI NtCreateIoCompletion(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,ULONG);
 NTSTATUS  WINAPI NtCreateKey(PHKEY,ACCESS_MASK,const OBJECT_ATTRIBUTES*,ULONG,const UNICODE_STRING*,ULONG,PULONG);
+NTSTATUS  WINAPI NtCreateMailslotFile(PHANDLE,ACCESS_MASK,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,ULONG,ULONG,ULONG,PLARGE_INTEGER);
 NTSTATUS  WINAPI NtCreateMutant(HANDLE*,ACCESS_MASK,const OBJECT_ATTRIBUTES*,BOOLEAN);
 NTSTATUS  WINAPI NtCreateNamedPipeFile(PHANDLE,ULONG,POBJECT_ATTRIBUTES,PIO_STATUS_BLOCK,ULONG,ULONG,ULONG,ULONG,ULONG,ULONG,ULONG,ULONG,ULONG,PLARGE_INTEGER);
 NTSTATUS  WINAPI NtCreateSection(HANDLE*,ACCESS_MASK,const OBJECT_ATTRIBUTES*,const LARGE_INTEGER*,ULONG,ULONG,HANDLE);
Index: dlls/kernel/tests/mailslot.c
===================================================================
RCS file: /home/wine/wine/dlls/kernel/tests/mailslot.c,v
retrieving revision 1.5
diff -u -p -r1.5 mailslot.c
--- dlls/kernel/tests/mailslot.c	10 Feb 2005 19:19:36 -0000	1.5
+++ dlls/kernel/tests/mailslot.c	21 Mar 2005 15:48:11 -0000
@@ -57,24 +57,16 @@ static int mailslot_test()
     hSlot = CreateMailslot( "blah", 0, 0, NULL );
     ok( hSlot == INVALID_HANDLE_VALUE,
             "Created mailslot with invalid name\n");
-    todo_wine
-    {
-       ok( GetLastError() == ERROR_INVALID_NAME,
-           "error should be ERROR_INVALID_NAME\n");
-    }
+    ok( GetLastError() == ERROR_INVALID_NAME,
+        "error should be ERROR_INVALID_NAME\n");
 
     /* open a mailslot with a null name */
     hSlot = CreateMailslot( NULL, 0, 0, NULL );
     ok( hSlot == INVALID_HANDLE_VALUE,
             "Created mailslot with invalid name\n");
-    todo_wine
-    {
-        ok( GetLastError() == ERROR_PATH_NOT_FOUND,
-            "error should be ERROR_PATH_NOT_FOUND\n");
-    }
+    ok( GetLastError() == ERROR_PATH_NOT_FOUND,
+        "error should be ERROR_PATH_NOT_FOUND\n");
 
-    todo_wine
-    {
     /* valid open, but with wacky parameters ... then check them */
     hSlot = CreateMailslot( szmspath, -1, -1, NULL );
     ok( hSlot != INVALID_HANDLE_VALUE , "mailslot with valid name failed\n");
@@ -83,22 +75,15 @@ static int mailslot_test()
            "getmailslotinfo failed\n");
     ok( dwMax == ~0UL, "dwMax incorrect\n");
     ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
-    }
     ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
-    todo_wine
-    {
     ok( dwTimeout == ~0UL, "dwTimeout incorrect\n");
     ok( GetMailslotInfo( hSlot, NULL, NULL, NULL, NULL ),
             "getmailslotinfo failed\n");
     ok( CloseHandle(hSlot), "failed to close mailslot\n");
-    }
 
-    todo_wine
-    {
     /* now open it for real */
     hSlot = CreateMailslot( szmspath, 0, 0, NULL );
     ok( hSlot != INVALID_HANDLE_VALUE , "valid mailslot failed\n");
-    }
 
     /* try and read/write to it */
     count = 0;
@@ -112,8 +97,6 @@ static int mailslot_test()
     hWriter = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
                              0, NULL, OPEN_EXISTING, 0, NULL);
     ok( hWriter == INVALID_HANDLE_VALUE, "bad sharing mode\n");
-    todo_wine
-    {
     ok( GetLastError() == ERROR_SHARING_VIOLATION,
             "error should be ERROR_SHARING_VIOLATION\n");
 
@@ -121,7 +104,6 @@ static int mailslot_test()
     hWriter = CreateFile(szmspath, GENERIC_READ|GENERIC_WRITE,
                              FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
     ok( hWriter != INVALID_HANDLE_VALUE, "existing mailslot\n");
-    }
 
     /*
      * opening a client should make no difference to
@@ -138,11 +120,8 @@ static int mailslot_test()
      */
     ok( !ReadFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
             "can read client\n");
-    todo_wine
-    {
     ok( WriteFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
             "can't write client\n");
-    }
     ok( !ReadFile( hWriter, buffer, sizeof buffer/2, &count, NULL),
             "can read client\n");
 
@@ -150,12 +129,9 @@ static int mailslot_test()
      * seeing as there's something in the slot,
      * we should be able to read it once
      */
-    todo_wine
-    {
     ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
             "slot read\n");
     ok( count == (sizeof buffer/2), "short read\n" );
-    }
 
     /* but not again */
     ok( !ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
@@ -180,8 +156,6 @@ static int mailslot_test()
     hSlot2 = CreateMailslot( szmspath, 0, 0, NULL );
     ok( hSlot2 == INVALID_HANDLE_VALUE , "opened two mailslots\n");
 
-    todo_wine
-    {
     /* close the client again */
     ok( CloseHandle( hWriter ), "closing the client\n");
 
@@ -192,7 +166,6 @@ static int mailslot_test()
     hWriter = CreateFile(szmspath, GENERIC_WRITE,
               FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
     ok( hWriter != INVALID_HANDLE_VALUE, "sharing writer\n");
-    }
 
     /*
      * now try open another as a writer ...
@@ -202,8 +175,6 @@ static int mailslot_test()
                      FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
     ok( hWriter2 == INVALID_HANDLE_VALUE, "greedy writer succeeded\n");
 
-    todo_wine
-    {
     /* now try open another as a writer ... and share with the first */
     hWriter2 = CreateFile(szmspath, GENERIC_WRITE,
               FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
@@ -214,7 +185,6 @@ static int mailslot_test()
     ok( GetMailslotInfo( hSlot, &dwMax, &dwNext, &dwMsgCount, &dwTimeout ),
         "getmailslotinfo failed\n");
     ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
-    }
     ok( dwMax == 0, "dwMax incorrect\n");
     ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
     ok( dwTimeout == 0, "dwTimeout incorrect\n");
@@ -223,8 +193,6 @@ static int mailslot_test()
     ok( !ReadFile( hSlot, buffer, sizeof buffer, &count, NULL), "slot read\n");
 
     /* write two messages */
-    todo_wine
-    {
     buffer[0] = 'a';
     ok( WriteFile( hWriter, buffer, 1, &count, NULL), "1st write failed\n");
 
@@ -232,7 +200,9 @@ static int mailslot_test()
     dwNext = dwMsgCount = 0;
     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
         "getmailslotinfo failed\n");
+    todo_wine {
     ok( dwNext == 1, "dwNext incorrect\n");
+    }
     ok( dwMsgCount == 1, "dwMsgCount incorrect\n");
 
     buffer[0] = 'b';
@@ -243,8 +213,10 @@ static int mailslot_test()
     dwNext = dwMsgCount = 0;
     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
         "getmailslotinfo failed\n");
+    todo_wine {
     ok( dwNext == 1, "dwNext incorrect\n");
     ok( dwMsgCount == 2, "dwMsgCount incorrect\n");
+    }
 
     /* write a 3rd message with zero size */
     ok( WriteFile( hWriter2, buffer, 0, &count, NULL), "3rd write failed\n");
@@ -253,8 +225,10 @@ static int mailslot_test()
     dwNext = dwMsgCount = 0;
     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
         "getmailslotinfo failed\n");
+    todo_wine {
     ok( dwNext == 1, "dwNext incorrect\n");
     ok( dwMsgCount == 3, "dwMsgCount incorrect\n");
+    }
 
     buffer[0]=buffer[1]=0;
 
@@ -271,8 +245,10 @@ static int mailslot_test()
     dwNext = dwMsgCount = 0;
     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
         "getmailslotinfo failed\n");
+    todo_wine {
     ok( dwNext == 2, "dwNext incorrect\n");
     ok( dwMsgCount == 2, "dwMsgCount incorrect\n");
+    }
 
     /* read the second message */
     ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
@@ -284,13 +260,13 @@ static int mailslot_test()
     dwNext = dwMsgCount = 0;
     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
         "getmailslotinfo failed\n");
-    }
+    todo_wine {
     ok( dwNext == 0, "dwNext incorrect\n");
-    todo_wine
-    {
     ok( dwMsgCount == 1, "dwMsgCount incorrect\n");
+    }
 
     /* read the 3rd (zero length) message */
+    todo_wine {
     ok( ReadFile( hSlot, buffer, sizeof buffer, &count, NULL),
         "3rd slot read failed\n");
     }
@@ -300,13 +276,10 @@ static int mailslot_test()
      * now there should be no more messages
      * check the mailslot info
      */
-    todo_wine
-    {
     dwNext = dwMsgCount = 0;
     ok( GetMailslotInfo( hSlot, NULL, &dwNext, &dwMsgCount, NULL ),
         "getmailslotinfo failed\n");
     ok( dwNext == MAILSLOT_NO_MESSAGE, "dwNext incorrect\n");
-    }
     ok( dwMsgCount == 0, "dwMsgCount incorrect\n");
 
     /* check that reads fail */
@@ -314,12 +287,9 @@ static int mailslot_test()
         "3rd slot read succeeded\n");
 
     /* finally close the mailslot and its client */
-    todo_wine
-    {
     ok( CloseHandle( hWriter2 ), "closing 2nd client\n");
     ok( CloseHandle( hWriter ), "closing the client\n");
     ok( CloseHandle( hSlot ), "closing the mailslot\n");
-    }
 
     return 0;
 }
--- /dev/null	2005-02-25 01:31:53.000000000 +0900
+++ server/mailslot.c	2005-03-22 00:35:48.000000000 +0900
@@ -0,0 +1,453 @@
+/*
+ * Server-side mailslot management
+ *
+ * Copyright (C) 1998 Alexandre Julliard
+ * Copyright (C) 2005 Mike McCormack
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include "config.h"
+#include "wine/port.h"
+#include "wine/unicode.h"
+
+#include <assert.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <sys/types.h>
+
+#ifdef HAVE_SYS_IOCTL_H
+#include <sys/ioctl.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+
+#include "windef.h"
+#include "winbase.h"
+
+#include "file.h"
+#include "handle.h"
+#include "thread.h"
+#include "request.h"
+
+struct mailslot
+{
+    struct object       obj;
+    struct fd          *fd;
+    struct fd          *write_fd;
+    unsigned int        max_msgsize;
+    unsigned int        read_timeout;
+    struct list         writers;
+    struct list         read_q;
+};
+
+/* mailslot functions */
+static void mailslot_dump( struct object*, int );
+static struct fd *mailslot_get_fd( struct object * );
+static void mailslot_destroy( struct object * );
+
+static const struct object_ops mailslot_ops =
+{
+    sizeof(struct mailslot),   /* size */
+    mailslot_dump,             /* dump */
+    default_fd_add_queue,      /* add_queue */
+    default_fd_remove_queue,   /* remove_queue */
+    default_fd_signaled,       /* signaled */
+    no_satisfied,              /* satisfied */
+    mailslot_get_fd,           /* get_fd */
+    mailslot_destroy           /* destroy */
+};
+
+static int mailslot_get_poll_events( struct fd * );
+static void mailslot_poll_event( struct fd *, int );
+static int mailslot_get_info( struct fd * );
+static void mailslot_queue_async( struct fd *, void*, void*, void*, int, int );
+static void mailslot_cancel_async( struct fd * );
+
+static const struct fd_ops mailslot_fd_ops =
+{
+    mailslot_get_poll_events,  /* get_poll_events */
+    mailslot_poll_event,       /* poll_event */
+    no_flush,                  /* flush */
+    mailslot_get_info,         /* get_file_info */
+    mailslot_queue_async,      /* queue_async */
+    mailslot_cancel_async      /* cancel_async */
+};
+
+struct mail_writer
+{
+    struct object         obj;
+    struct mailslot      *mailslot;
+    struct list           entry;
+    int                   access;
+    int                   sharing;
+};
+
+static void mail_writer_dump( struct object *obj, int verbose );
+static struct fd *mail_writer_get_fd( struct object *obj );
+static void mail_writer_destroy( struct object *obj);
+
+static const struct object_ops mail_writer_ops =
+{
+    sizeof(struct mail_writer), /* size */
+    mail_writer_dump,           /* dump */
+    no_add_queue,               /* add_queue */
+    NULL,                       /* remove_queue */
+    NULL,                       /* signaled */
+    NULL,                       /* satisfied */
+    mail_writer_get_fd,         /* get_fd */
+    mail_writer_destroy         /* destroy */
+};
+
+static int mail_writer_get_info( struct fd *fd );
+
+static const struct fd_ops mail_writer_fd_ops =
+{
+    NULL,                        /* get_poll_events */
+    NULL,                        /* poll_event */
+    no_flush,                    /* flush */
+    mail_writer_get_info,        /* get_file_info */
+    no_queue_async,              /* queue_async */
+    NULL                         /* cancel_async */
+};
+
+static void mailslot_destroy( struct object *obj)
+{
+    struct mailslot *mailslot = (struct mailslot *) obj;
+
+    assert( mailslot->fd );
+    assert( mailslot->write_fd );
+
+    async_terminate_queue( &mailslot->read_q, STATUS_CANCELLED );
+
+    release_object( mailslot->fd );
+    release_object( mailslot->write_fd );
+}
+
+static void mailslot_dump( struct object *obj, int verbose )
+{
+    struct mailslot *mailslot = (struct mailslot *) obj;
+
+    assert( obj->ops == &mailslot_ops );
+    fprintf( stderr, "mailslot  max_msgsize = %p read_timeout = %d %d\n",
+             mailslot, mailslot->max_msgsize, mailslot->read_timeout );
+}
+
+static int mailslot_has_message(struct mailslot *mailslot)
+{
+    struct pollfd pfd;
+
+    /* poll the socket to see if there's any messages */
+    pfd.fd = get_unix_fd( mailslot->fd );
+    pfd.events = POLLIN;
+    pfd.revents = 0;
+    return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
+}
+
+static int mailslot_get_info( struct fd *fd )
+{
+    struct mailslot *mailslot = get_fd_user( fd );
+    int flags = 0;
+    assert( mailslot->obj.ops == &mailslot_ops );
+
+    if (list_empty( &mailslot->writers ) || !mailslot_has_message( mailslot ))
+        flags |= FD_FLAG_MAILSLOT_EMPTY;
+
+    return flags;
+}
+
+static struct fd *mailslot_get_fd( struct object *obj )
+{
+    struct mailslot *mailslot = (struct mailslot *) obj;
+
+    return (struct fd *)grab_object( mailslot->fd );
+}
+
+static int mailslot_get_poll_events( struct fd *fd )
+{
+    struct mailslot *mailslot = get_fd_user( fd );
+    int events = 0;
+    assert( mailslot->obj.ops == &mailslot_ops );
+    
+    if( !list_empty( &mailslot->read_q ))
+        events |= POLLIN;
+
+    return events;
+}
+
+static void mailslot_poll_event( struct fd *fd, int event )
+{
+    struct mailslot *mailslot = get_fd_user( fd );
+
+    fprintf(stderr,"Poll event %02x\n",event);
+
+    if( !list_empty( &mailslot->read_q ) && (POLLIN & event) )
+        async_terminate_head( &mailslot->read_q, STATUS_ALERTED );
+
+    set_fd_events( fd, mailslot_get_poll_events(fd) );
+}
+
+static void
+mailslot_queue_async( struct fd *fd, void *apc, void *user,
+                      void *iosb, int type, int count )
+{
+    struct mailslot *mailslot = get_fd_user( fd );
+    int events;
+
+    assert(mailslot->obj.ops == &mailslot_ops);
+
+    if( type != ASYNC_TYPE_READ )
+    {
+        set_error(STATUS_INVALID_PARAMETER);
+        return;
+    }
+
+    if (!create_async( fd, current, 0, &mailslot->read_q, apc, user, iosb ))
+        return;
+
+    /* Check if the new pending request can be served immediately */
+    events = check_fd_events( fd, mailslot_get_poll_events( fd ) );
+    if (events)
+    {
+        mailslot_poll_event( fd, events );
+        return;
+    }
+
+    set_fd_events( fd, mailslot_get_poll_events( fd ));
+}
+
+static void mailslot_cancel_async( struct fd *fd )
+{
+    struct mailslot *mailslot = get_fd_user( fd );
+
+    assert(mailslot->obj.ops == &mailslot_ops);
+    async_terminate_queue( &mailslot->read_q, STATUS_CANCELLED );
+}
+
+struct mailslot *
+create_mailslot( const WCHAR *name, int len, int max_msgsize, int read_timeout )
+{
+    struct mailslot *mailslot;
+    int fds[2];
+    WCHAR slot[] = {'m','a','i','l','s','l','o','t','\\',0};
+
+    if( ( len <= strlenW( slot ) ) || strncmpiW( slot, name, strlenW( slot ) ) )
+    {
+        set_error( STATUS_OBJECT_NAME_INVALID );
+        return NULL;
+    }
+
+    mailslot = create_named_object( sync_namespace, &mailslot_ops, name, len );
+    if( !mailslot )
+        return NULL;
+
+    /* it already exists - there can only be one mailslot to read from */
+    if( get_error() == STATUS_OBJECT_NAME_COLLISION )
+    {
+        release_object( mailslot );
+        return NULL;
+    }
+
+    mailslot->fd = NULL;
+    mailslot->write_fd = NULL;
+    mailslot->max_msgsize = max_msgsize;
+    mailslot->read_timeout = read_timeout;
+    list_init( &mailslot->writers );
+    list_init( &mailslot->read_q );
+
+    if( !socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ) )
+    {
+        fcntl( fds[0], F_SETFL, O_NONBLOCK );
+        fcntl( fds[1], F_SETFL, O_NONBLOCK );
+        mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
+                                fds[1], &mailslot->obj );
+        mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
+                                fds[0], &mailslot->obj );
+    }
+    else
+        file_set_error();
+    if( mailslot->fd && mailslot->write_fd )
+        return mailslot;
+    release_object( mailslot );
+    return NULL;
+}
+
+static struct mailslot *open_mailslot( const WCHAR *name, size_t len )
+{
+    struct object *obj;
+
+    obj = find_object( sync_namespace, name, len );
+    if (obj)
+    {
+        if (obj->ops == &mailslot_ops)
+            return (struct mailslot *)obj;
+        release_object( obj );
+        set_error( STATUS_OBJECT_TYPE_MISMATCH );
+    }
+    else
+        set_error( STATUS_OBJECT_NAME_NOT_FOUND );
+
+    return NULL;
+}
+
+static void mail_writer_dump( struct object *obj, int verbose )
+{
+    struct mail_writer *writer = (struct mail_writer *) obj;
+
+    assert( obj->ops == &mail_writer_ops );
+    fprintf( stderr, "mail_writer %p\n", writer );
+}
+
+static void mail_writer_destroy( struct object *obj)
+{
+    struct mail_writer *writer = (struct mail_writer *) obj;
+
+    list_remove( &writer->entry );
+    release_object( writer->mailslot );
+}
+
+static int mail_writer_get_info( struct fd *fd )
+{
+    return 0;
+}
+
+static struct fd *mail_writer_get_fd( struct object *obj )
+{
+    struct mail_writer *writer = (struct mail_writer *) obj;
+
+    return (struct fd *)grab_object( writer->mailslot->write_fd );
+}
+
+/*
+ * Readers and writers cannot be mixed.
+ * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
+ */
+static struct mail_writer *
+create_mail_writer( struct mailslot *mailslot, int access, int sharing )
+{
+    struct mail_writer *writer;
+
+    if (!list_empty( &mailslot->writers ))
+    {
+        writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
+
+        if (((access & GENERIC_WRITE) || (writer->access & GENERIC_WRITE)) &&
+           !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
+        {
+            set_error( STATUS_SHARING_VIOLATION );
+            return 0;
+        }
+    }
+
+    writer = alloc_object( &mail_writer_ops );
+    if (!writer)
+        return NULL;
+
+    grab_object( mailslot );
+    writer->mailslot = mailslot;
+    writer->access = access;
+    writer->sharing = sharing;
+
+    list_add_head( &mailslot->writers, &writer->entry );
+ 
+    return writer;
+}
+
+static struct mailslot *
+get_mailslot_obj( struct process *process, obj_handle_t handle, unsigned int access )
+{
+    struct object *obj;
+    obj = get_handle_obj( process, handle, access, &mailslot_ops );
+    return (struct mailslot *) obj;
+}
+
+DECL_HANDLER(create_mailslot)
+{
+    struct mailslot *mailslot;
+
+    reply->handle = 0;
+    mailslot = create_mailslot( get_req_data(), get_req_data_size(),
+                                req->max_msgsize, req->read_timeout );
+    if( mailslot )
+    {
+        reply->handle = alloc_handle( current->process, mailslot,
+                                      GENERIC_READ, req->inherit );
+        release_object( mailslot );
+    }
+}
+
+DECL_HANDLER(open_mailslot)
+{
+    struct mailslot *mailslot;
+
+    reply->handle = 0;
+
+    if( ! ( req->sharing & FILE_SHARE_READ ) )
+    {
+        set_error( STATUS_SHARING_VIOLATION );
+        return;
+    }
+
+    mailslot = open_mailslot( get_req_data(), get_req_data_size() );
+    if( mailslot )
+    {
+        struct mail_writer *writer;
+        int access = req->access;
+
+        writer = create_mail_writer( mailslot, access, req->sharing );
+        if( writer )
+        {
+            access = access&GENERIC_WRITE;
+            reply->handle = alloc_handle( current->process, writer,
+                                          access, req->inherit );
+            release_object( writer );
+        }
+        release_object( mailslot );
+    }
+    else
+        set_error( STATUS_NO_SUCH_FILE );
+}
+
+DECL_HANDLER(mailslot_info)
+{
+    struct mailslot *mailslot;
+
+    mailslot = get_mailslot_obj( current->process, req->handle, 0 );
+    if( mailslot )
+    {
+        int r, fd = get_unix_fd( mailslot->fd );
+
+        if( req->flags & MAILSLOT_SET_INFO )
+            mailslot->read_timeout = req->read_timeout;
+        reply->max_msgsize = mailslot->max_msgsize;
+        reply->read_timeout = mailslot->read_timeout;
+        reply->msg_count = mailslot_has_message(mailslot);
+
+        /* get the size of the next message */
+        r = recv( fd, NULL, 0, MSG_PEEK | MSG_TRUNC );
+        if( r < 0 )
+            reply->next_msgsize = MAILSLOT_NO_MESSAGE;
+        else
+            reply->next_msgsize = r;
+
+        release_object( mailslot );
+    }
+}


More information about the wine-patches mailing list