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