SMDLL: helper to use the SM
authorEmanuele Aliberti <ea@iol.it>
Sun, 6 Feb 2005 17:00:53 +0000 (17:00 +0000)
committerEmanuele Aliberti <ea@iol.it>
Sun, 6 Feb 2005 17:00:53 +0000 (17:00 +0000)
svn path=/trunk/; revision=13443

reactos/lib/smdll/compses.c [new file with mode: 0644]
reactos/lib/smdll/connect.c [new file with mode: 0644]
reactos/lib/smdll/dllmain.c [new file with mode: 0644]
reactos/lib/smdll/execpgm.c [new file with mode: 0644]
reactos/lib/smdll/makefile [new file with mode: 0644]
reactos/lib/smdll/readme.txt [new file with mode: 0644]
reactos/lib/smdll/smdll.def [new file with mode: 0644]
reactos/lib/smdll/smdll.rc [new file with mode: 0644]
reactos/lib/smdll/testapi.c [new file with mode: 0644]

diff --git a/reactos/lib/smdll/compses.c b/reactos/lib/smdll/compses.c
new file mode 100644 (file)
index 0000000..9f7e324
--- /dev/null
@@ -0,0 +1,39 @@
+/* $Id$\r
+ *\r
+ * COPYRIGHT:       See COPYING in the top level directory\r
+ * PROJECT:         ReactOS system libraries\r
+ * FILE:            lib/smlib/compses.c\r
+ * PURPOSE:         Call SM API SM_API_COMPLETE_SESSION\r
+ */\r
+#define NTOS_MODE_USER\r
+#include <ntos.h>\r
+#include <sm/api.h>\r
+#include <sm/helper.h>\r
+\r
+NTSTATUS STDCALL\r
+SmCompleteSession (HANDLE hSmApiPort, HANDLE hSbApiPort, HANDLE hApiPort)\r
+{\r
+  NTSTATUS         Status;\r
+  SM_PORT_MESSAGE  SmReqMsg;\r
+    \r
+  /* Marshal Ses in the LPC message */\r
+  SmReqMsg.CompSes.hApiPort   = hApiPort;\r
+  SmReqMsg.CompSes.hSbApiPort = hSbApiPort;\r
+\r
+  /* SM API to invoke */\r
+  SmReqMsg.ApiIndex = SM_API_COMPLETE_SESSION;\r
+\r
+  /* Port message */\r
+  SmReqMsg.Header.MessageType = LPC_NEW_MESSAGE;\r
+  SmReqMsg.Header.DataSize    = SM_PORT_DATA_SIZE(SmReqMsg.CompSes);\r
+  SmReqMsg.Header.MessageSize = SM_PORT_MESSAGE_SIZE;\r
+  Status = NtRequestWaitReplyPort (hSmApiPort, (PLPC_MESSAGE) & SmReqMsg, (PLPC_MESSAGE) & SmReqMsg);\r
+  if (NT_SUCCESS(Status))\r
+  {\r
+    return SmReqMsg.Status;\r
+  }\r
+  DbgPrint ("%s failed (Status=0x%08lx)\n", __FUNCTION__, Status);\r
+  return Status;\r
+}\r
+\r
+/* EOF */\r
diff --git a/reactos/lib/smdll/connect.c b/reactos/lib/smdll/connect.c
new file mode 100644 (file)
index 0000000..1ac5d51
--- /dev/null
@@ -0,0 +1,83 @@
+/* $Id$\r
+ *\r
+ * COPYRIGHT:  See COPYING in the top level directory\r
+ * PROJECT:    ReactOS system libraries\r
+ * FILE:       reactos/lib/smdll/connect.c\r
+ * PURPOSE:    Connect to the API LPC port exposed by the SM\r
+ */\r
+#define NTOS_MODE_USER\r
+#include <ntos.h>\r
+#include <sm/api.h>\r
+#include <sm/helper.h>\r
+#include <pe.h>\r
+\r
+/**********************************************************************\r
+ * NAME                                                        EXPORTED\r
+ *     SmConnectApiPort/4\r
+ *\r
+ * DESCRIPTION\r
+ *     Connect to SM API port and register a session "begin" port (Sb)\r
+ *     or to issue API requests to SmApiPort.\r
+ *\r
+ * ARGUMENTS\r
+ *     pSbApiPortName: name of the Sb port the calling subsystem\r
+ *             server already created in the system name space;\r
+ *     hSbApiPort: LPC port handle (checked, but not used);\r
+ *     dwSubsystem: a valid IMAGE_SUBSYSTEM_xxx value;\r
+ *     phSmApiPort: a pointer to a HANDLE, which will be\r
+ *             filled with a valid client-side LPC comm port.\r
+ *     \r
+ * RETURN VALUE\r
+ *     If all three optional values are omitted, an LPC status.\r
+ *     STATUS_INVALID_PARAMETER_MIX if PortName is defined and\r
+ *     both hSbApiPort and dwSubsystem are 0.\r
+ */\r
+NTSTATUS STDCALL\r
+SmConnectApiPort (IN      PUNICODE_STRING  pSbApiPortName  OPTIONAL,\r
+                 IN      HANDLE           hSbApiPort      OPTIONAL,\r
+                 IN      DWORD            dwSubsystem     OPTIONAL,\r
+                 IN OUT  PHANDLE          phSmApiPort)\r
+{\r
+  UNICODE_STRING              SmApiPortName;\r
+  SECURITY_QUALITY_OF_SERVICE SecurityQos;\r
+  NTSTATUS                    Status = STATUS_SUCCESS;\r
+  SM_CONNECT_DATA             ConnectData = {0,{0}};\r
+  ULONG                       ConnectDataLength = 0;\r
+\r
+  if (pSbApiPortName)\r
+  {\r
+    if (NULL == hSbApiPort || IMAGE_SUBSYSTEM_UNKNOWN == dwSubsystem)\r
+    {\r
+      return STATUS_INVALID_PARAMETER_MIX;\r
+    }\r
+    ConnectData.Subsystem = dwSubsystem;\r
+    memmove (& ConnectData.SbName, pSbApiPortName->Buffer, pSbApiPortName->Length);\r
+  }\r
+  ConnectDataLength = sizeof (ConnectData);\r
+\r
+  SecurityQos.Length              = sizeof (SecurityQos);\r
+  SecurityQos.ImpersonationLevel  = SecurityIdentification;\r
+  SecurityQos.ContextTrackingMode = TRUE;\r
+  SecurityQos.EffectiveOnly       = TRUE;\r
+\r
+  RtlInitUnicodeString (& SmApiPortName, SM_API_PORT_NAME);\r
+\r
+  Status = NtConnectPort (\r
+             phSmApiPort,\r
+             & SmApiPortName,\r
+             & SecurityQos,\r
+             NULL,\r
+             NULL,\r
+             NULL,\r
+             & ConnectData,\r
+             & ConnectDataLength\r
+             );\r
+  if (NT_SUCCESS(Status))\r
+  {\r
+    return STATUS_SUCCESS;\r
+  }\r
+  DbgPrint ("%s failed (Status=0x%08lx)\n", __FUNCTION__, Status);\r
+  return Status;\r
+}\r
+\r
+/* EOF */\r
diff --git a/reactos/lib/smdll/dllmain.c b/reactos/lib/smdll/dllmain.c
new file mode 100644 (file)
index 0000000..3d6e488
--- /dev/null
@@ -0,0 +1,17 @@
+/* $Id$\r
+ *\r
+ * COPYRIGHT:       See COPYING in the top level directory\r
+ * PROJECT:         ReactOS\r
+ * FILE:            lib/smdll/dllmain.c\r
+ * PURPOSE:         SM Helper Library\r
+ */\r
+\r
+#define NTOS_MODE_USER\r
+#include <ntos.h>\r
+\r
+BOOL STDCALL DllMain(HANDLE hinstDll, DWORD fdwReason, LPVOID fImpLoad)\r
+{\r
+  return TRUE;\r
+}\r
+\r
+/* EOF */\r
diff --git a/reactos/lib/smdll/execpgm.c b/reactos/lib/smdll/execpgm.c
new file mode 100644 (file)
index 0000000..f5b2e5f
--- /dev/null
@@ -0,0 +1,49 @@
+/* $Id$\r
+ *\r
+ * COPYRIGHT:       See COPYING in the top level directory\r
+ * PROJECT:         ReactOS system libraries\r
+ * FILE:            lib/smdll/execpgm.c\r
+ * PURPOSE:         Call SM API SM_API_EXECPGM\r
+ */\r
+#define NTOS_MODE_USER\r
+#include <ntos.h>\r
+#include <sm/api.h>\r
+#include <sm/helper.h>\r
+#include <string.h>\r
+\r
+NTSTATUS STDCALL\r
+SmExecPgm (HANDLE hSmApiPort, PUNICODE_STRING Pgm)\r
+{\r
+  NTSTATUS         Status;\r
+  SM_PORT_MESSAGE  SmReqMsg;\r
+\r
+\r
+  /* Check Pgm's length */\r
+  if (Pgm->Length > (sizeof (Pgm->Buffer[0]) * SM_EXEXPGM_MAX_LENGTH))\r
+  {\r
+    return STATUS_INVALID_PARAMETER;\r
+  }\r
+  /* Marshal Pgm in the LPC message */\r
+  RtlZeroMemory (& SmReqMsg, sizeof SmReqMsg);\r
+  SmReqMsg.ExecPgm.NameLength = Pgm->Length;\r
+  RtlCopyMemory (SmReqMsg.ExecPgm.Name, Pgm->Buffer, Pgm->Length);\r
+               \r
+  /* SM API to invoke */\r
+  SmReqMsg.ApiIndex = SM_API_EXECUTE_PROGRAMME;\r
+\r
+  /* LPC message */\r
+  SmReqMsg.Header.MessageType = LPC_NEW_MESSAGE;\r
+  SmReqMsg.Header.DataSize    = SM_PORT_DATA_SIZE(SmReqMsg.ExecPgm);\r
+  SmReqMsg.Header.MessageSize = SM_PORT_MESSAGE_SIZE;\r
+\r
+  /* Call SM and wait for a reply */\r
+  Status = NtRequestWaitReplyPort (hSmApiPort, (PLPC_MESSAGE) & SmReqMsg, (PLPC_MESSAGE) & SmReqMsg);\r
+  if (NT_SUCCESS(Status))\r
+  {\r
+    return SmReqMsg.Status;\r
+  }\r
+  DbgPrint ("%s failed (Status=0x%08lx)\n", __FUNCTION__, Status);\r
+  return Status;\r
+}\r
+\r
+/* EOF */\r
diff --git a/reactos/lib/smdll/makefile b/reactos/lib/smdll/makefile
new file mode 100644 (file)
index 0000000..d14e1d5
--- /dev/null
@@ -0,0 +1,34 @@
+# $Id$\r
+\r
+PATH_TO_TOP = ../..\r
+\r
+TARGET_TYPE = dynlink\r
+\r
+TARGET_NAME = smdll\r
+\r
+TARGET_SDKLIBS = ntdll.a\r
+\r
+TARGET_CFLAGS = -I./include -Wall -Werror\r
+\r
+# require os code to explicitly request A/W version of structs/functions\r
+TARGET_CFLAGS += -D_DISABLE_TIDENTS\r
+\r
+TARGET_LFLAGS = -nostartfiles -nostdlib\r
+\r
+#TARGET_BASE =\r
+\r
+TARGET_OBJECTS = \\r
+       dllmain.o \\r
+       connect.o \\r
+       execpgm.o \\r
+       compses.o\r
+       \r
+DEP_OBJECTS = $(TARGET_OBJECTS)\r
+\r
+include $(PATH_TO_TOP)/rules.mak\r
+\r
+include $(TOOLS_PATH)/helper.mk\r
+\r
+include $(TOOLS_PATH)/depend.mk\r
+\r
+# EOF\r
diff --git a/reactos/lib/smdll/readme.txt b/reactos/lib/smdll/readme.txt
new file mode 100644 (file)
index 0000000..8265e59
--- /dev/null
@@ -0,0 +1,18 @@
+$Id$\r
+\r
+This is SMDLL: a helper library to talk to the ReactOS session manager (SM).\r
+\r
+It should be linked in the following components:\r
+\r
+a) the SM itself, because iy registers for managing native processes\r
+   IMAGE_SUBSYSTEM_NATIVE;\r
+\r
+b) environment subsystem servers, because each one should register in\r
+   the SM its own subsystem (willing to manageg those processes);\r
+\r
+c) terminal emulators for optional subsystems, like posixw32 and os2w32,\r
+   to ask the SM to start the optional subsystem server they need connect to;\r
+\r
+d) system and development utilites to debug/query the SM.\r
+\r
+2004-02-15 ea
\ No newline at end of file
diff --git a/reactos/lib/smdll/smdll.def b/reactos/lib/smdll/smdll.def
new file mode 100644 (file)
index 0000000..76d5246
--- /dev/null
@@ -0,0 +1,5 @@
+LIBRARY SMDLL.DLL\r
+EXPORTS\r
+SmCompleteSession@12\r
+SmConnectApiPort@16\r
+SmExecPgm@8
\ No newline at end of file
diff --git a/reactos/lib/smdll/smdll.rc b/reactos/lib/smdll/smdll.rc
new file mode 100644 (file)
index 0000000..ae16f7b
--- /dev/null
@@ -0,0 +1,4 @@
+#define REACTOS_STR_FILE_DESCRIPTION   "ReactOS SM Helper\0"\r
+#define REACTOS_STR_INTERNAL_NAME      "smdll.dll\0"\r
+#define REACTOS_STR_ORIGINAL_FILENAME  "smdll.dll\0"\r
+#include <reactos/version.rc>\r
diff --git a/reactos/lib/smdll/testapi.c b/reactos/lib/smdll/testapi.c
new file mode 100644 (file)
index 0000000..a1ecde2
--- /dev/null
@@ -0,0 +1,20 @@
+/* $Id$ */\r
+#define NTOS_MODE_USER\r
+#include <ntos.h>\r
+#include <sm/api.h>\r
+\r
+VOID STDCALL SmPrintPortMessage (PSM_PORT_MESSAGE SmMessage)\r
+{\r
+       DbgPrint ("SM_PORT_MESSAGE %08lx:\n", (ULONG) SmMessage);\r
+       DbgPrint ("  Header:\n");\r
+       DbgPrint ("    MessageType = %u\n", SmMessage->Header.MessageType);\r
+       DbgPrint ("    DataSize    = %d\n", SmMessage->Header.DataSize);\r
+       DbgPrint ("    MessageSize = %d\n", SmMessage->Header.MessageSize);\r
+       DbgPrint ("  ApiIndex      = %ld\n", SmMessage->ApiIndex);\r
+       DbgPrint ("  Status        = %08lx\n", SmMessage->Status);\r
+       DbgPrint ("  ExecPgm:\n");\r
+       DbgPrint ("    NameLength  = %ld\n", SmMessage->ExecPgm.NameLength);\r
+       DbgPrint ("    Name        = %ls\n", (LPWSTR) & SmMessage->ExecPgm.Name);\r
+}\r
+/* EOF */\r
+\r