build smlib with NDK
[reactos.git] / reactos / lib / smlib / connect.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: reactos/lib/smlib/connect.c
6 * PURPOSE: Connect to the API LPC port exposed by the SM
7 */
8 #include <windows.h>
9 #define NTOS_MODE_USER
10 #include <ndk/ntndk.h>
11
12 #include <sm/helper.h>
13
14 #define NDEBUG
15 #include <debug.h>
16
17 /**********************************************************************
18 * NAME EXPORTED
19 * SmConnectApiPort/4
20 *
21 * DESCRIPTION
22 * Connect to SM API port and register a session "begin" port (Sb)
23 * or to issue API requests to SmApiPort.
24 *
25 * ARGUMENTS
26 * pSbApiPortName: name of the Sb port the calling subsystem
27 * server already created in the system name space;
28 * hSbApiPort: LPC port handle (checked, but not used: the
29 * subsystem is required to have already created
30 * the callback port before it connects to the SM);
31 * wSubsystem: a valid IMAGE_SUBSYSTEM_xxx value;
32 * phSmApiPort: a pointer to a HANDLE, which will be
33 * filled with a valid client-side LPC comm port.
34 *
35 * There should be only two ways to call this API:
36 * a) subsystems willing to register with SM will use it
37 * with full parameters (the function checks them);
38 * b) regular SM clients, will set to 0 the 1st, the 2nd,
39 * and the 3rd parameter.
40 *
41 * RETURN VALUE
42 * If all three optional values are omitted, an LPC status.
43 * STATUS_INVALID_PARAMETER_MIX if PortName is defined and
44 * both hSbApiPort and wSubsystem are 0.
45 */
46 NTSTATUS STDCALL
47 SmConnectApiPort (IN PUNICODE_STRING pSbApiPortName OPTIONAL,
48 IN HANDLE hSbApiPort OPTIONAL,
49 IN WORD wSubSystemId OPTIONAL,
50 IN OUT PHANDLE phSmApiPort)
51 {
52 UNICODE_STRING SmApiPortName;
53 SECURITY_QUALITY_OF_SERVICE SecurityQos;
54 NTSTATUS Status = STATUS_SUCCESS;
55 SM_CONNECT_DATA ConnectData = {0,0,{0}};
56 ULONG ConnectDataLength = 0;
57
58 DPRINT("SMLIB: %s called\n", __FUNCTION__);
59
60 if (pSbApiPortName)
61 {
62 if (pSbApiPortName->Length > (sizeof pSbApiPortName->Buffer[0] * SM_SB_NAME_MAX_LENGTH))
63 {
64 return STATUS_INVALID_PARAMETER_1;
65 }
66 if (NULL == hSbApiPort || IMAGE_SUBSYSTEM_UNKNOWN == wSubSystemId)
67 {
68 return STATUS_INVALID_PARAMETER_MIX;
69 }
70 RtlZeroMemory (& ConnectData, sizeof ConnectData);
71 ConnectData.Unused = 0;
72 ConnectData.SubSystemId = wSubSystemId;
73 if (pSbApiPortName->Length > 0)
74 {
75 RtlCopyMemory (& ConnectData.SbName,
76 pSbApiPortName->Buffer,
77 pSbApiPortName->Length);
78 }
79 }
80 ConnectDataLength = sizeof ConnectData;
81
82 SecurityQos.Length = sizeof (SecurityQos);
83 SecurityQos.ImpersonationLevel = SecurityIdentification;
84 SecurityQos.ContextTrackingMode = TRUE;
85 SecurityQos.EffectiveOnly = TRUE;
86
87 RtlInitUnicodeString (& SmApiPortName, SM_API_PORT_NAME);
88
89 Status = NtConnectPort (
90 phSmApiPort,
91 & SmApiPortName,
92 & SecurityQos,
93 NULL,
94 NULL,
95 NULL,
96 & ConnectData,
97 & ConnectDataLength
98 );
99 if (NT_SUCCESS(Status))
100 {
101 return STATUS_SUCCESS;
102 }
103 DPRINT("SMLIB: %s failed (Status=0x%08lx)\n", __FUNCTION__, Status);
104 return Status;
105 }
106
107 /* EOF */