2 * ReactOS Win32 Applications
3 * Copyright (C) 2005 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT : See COPYING in the top level directory
22 * PROJECT : ReactOS/Win32 Session Manager Control Tool
23 * FILE : subsys/system/sm/sm.c
24 * PROGRAMMER: Emanuele Aliberti (ea@reactos.com)
32 #define WIN32_NO_STATUS
38 #include <sm/helper.h>
40 VOID
PrintWin32Error(PWCHAR
,DWORD
); /* win32err.c */
42 #define SM_CMD(n) cmd_##n
43 #define SM_CMD_DECL(n) int SM_CMD(n)(int argc, char * argv[])
44 #define SM_CMD_CALL(n,c,v) SM_CMD(n)((c),(v))
46 HANDLE hSmApiPort
= (HANDLE
) 0;
48 VOID STDCALL
PrintStatusError (NTSTATUS Status
)
50 DWORD Win32Error
= RtlNtStatusToDosError (Status
);
52 PrintWin32Error (L
"sm", Win32Error
);
55 typedef struct _SM_CMD_DESCRIPTOR
57 TCHAR Name
[RC_STRING_MAX_SIZE
];
58 int (*EntryPoint
)(int,TCHAR
**);
59 TCHAR Synopsis
[RC_STRING_MAX_SIZE
];
60 TCHAR Description
[RC_STRING_MAX_SIZE
];
62 } SM_CMD_DESCRIPTOR
, *PSM_CMD_DESCRIPTOR
;
68 SM_CMD_DECL(shutdown
);
70 /* internal commands directory */
71 SM_CMD_DESCRIPTOR Command
[] =
73 {"boot", SM_CMD(boot
), _T("boot subsystem_name"), _T("bootstrap an optional environment subsystem;")},
74 {"help", SM_CMD(help
), _T("help [command]"), _T("print help for command;")},
75 {"info", SM_CMD(info
), _T("info [subsystem_id]"), _T("print information about a booted subsystem\n"
76 "if subsystem_id is omitted, a list of booted\n"
77 "environment subsystems is printed.")},
78 {"reboot", SM_CMD(reboot
), _T("reboot subsystem_id"), _T("reboot an optional environment subsystem;")},
79 {"shutdown", SM_CMD(shutdown
), _T("shutdown subsystem_id"), _T("shutdown an optional environment subsystem;")},
82 TCHAR UsageMessage
[RC_STRING_MAX_SIZE
];
83 void loadlang(PSM_CMD_DESCRIPTOR
);
85 PSM_CMD_DESCRIPTOR
LookupCommand (const TCHAR
* CommandName
)
88 const int command_count
= (sizeof Command
/ sizeof Command
[0]);
90 /* parse the command... */
92 for (i
=0; (i
< command_count
); i
++)
94 if (0 == _tcscmp(CommandName
, Command
[i
].Name
))
99 if (i
== command_count
)
101 LoadString( GetModuleHandle(NULL
), IDS_Unknown
, (LPTSTR
) UsageMessage
,RC_STRING_MAX_SIZE
);
103 _ftprintf(stderr
, UsageMessage
, CommandName
);
106 return & Command
[i
];
113 int rc
= EXIT_SUCCESS
;
114 ANSI_STRING ProgramA
;
115 UNICODE_STRING ProgramW
;
116 NTSTATUS Status
= STATUS_SUCCESS
;
121 RtlInitAnsiString (& ProgramA
, argv
[2]);
122 RtlAnsiStringToUnicodeString (& ProgramW
, & ProgramA
, TRUE
);
123 Status
= SmExecuteProgram (hSmApiPort
, & ProgramW
);
124 RtlFreeUnicodeString (& ProgramW
);
127 Status
= SmExecuteProgram (hSmApiPort
, & ProgramW
);
129 if (STATUS_SUCCESS
!= Status
)
131 PrintStatusError (Status
);
138 return SM_CMD_CALL(help
,3,argv
);
146 PSM_CMD_DESCRIPTOR cmd
= NULL
;
147 int rc
= EXIT_SUCCESS
;
152 for (i
=0; (i
< (sizeof Command
/ sizeof Command
[0])); i
++)
154 _tprintf(_T("%s\n"), Command
[i
].Synopsis
);
158 cmd
= LookupCommand (argv
[2]);
164 _tprintf(_T("%s\n%s\n\n%s\n"),
175 int rc
= EXIT_SUCCESS
;
176 NTSTATUS Status
= STATUS_SUCCESS
;
177 SM_INFORMATION_CLASS InformationClass
= SmBasicInformation
;
179 SM_BASIC_INFORMATION bi
;
180 SM_SUBSYSTEM_INFORMATION ssi
;
182 ULONG DataLength
= 0;
183 ULONG ReturnDataLength
= 0;
186 RtlZeroMemory (& Info
, sizeof Info
);
189 case 2: /* sm info */
190 InformationClass
= SmBasicInformation
;
191 DataLength
= sizeof Info
.bi
;
193 case 3: /* sm info id */
194 InformationClass
= SmSubSystemInformation
;
195 DataLength
= sizeof Info
.ssi
;
196 Info
.ssi
.SubSystemId
= atol(argv
[2]);
202 Status
= SmQueryInformation (hSmApiPort
,
207 if (STATUS_SUCCESS
!= Status
)
209 PrintStatusError (Status
);
215 LoadString( GetModuleHandle(NULL
), IDS_SM1
, (LPTSTR
) UsageMessage
,RC_STRING_MAX_SIZE
);
216 _tprintf(UsageMessage
);
218 LoadString( GetModuleHandle(NULL
), IDS_SM2
, (LPTSTR
) UsageMessage
,RC_STRING_MAX_SIZE
);
219 for (i
= 0; i
< Info
.bi
.SubSystemCount
; i
++)
221 _tprintf(UsageMessage
,
222 Info
.bi
.SubSystem
[i
].Id
,
223 Info
.bi
.SubSystem
[i
].ProcessId
,
224 Info
.bi
.SubSystem
[i
].Flags
);
228 LoadString( GetModuleHandle(NULL
), IDS_ID
, (LPTSTR
) UsageMessage
,RC_STRING_MAX_SIZE
);
230 _tprintf (UsageMessage
, Info
.ssi
.SubSystemId
, Info
.ssi
.Flags
, Info
.ssi
.ProcessId
);
231 wprintf(L
" NSRootNode: '%s'\n", Info
.ssi
.NameSpaceRootNode
);
239 SM_CMD_DECL(shutdown
)
241 int rc
= EXIT_SUCCESS
;
243 LoadString( GetModuleHandle(NULL
), IDS_Not_Imp
, (LPTSTR
) UsageMessage
,RC_STRING_MAX_SIZE
);
245 _ftprintf(stderr
,UsageMessage
);
251 int rc
= SM_CMD(shutdown
)(argc
,argv
);
252 if(EXIT_SUCCESS
== rc
)
254 rc
= SM_CMD(boot
)(argc
,argv
);
259 /* print command's synopsys */
260 int print_synopsys (int argc
, TCHAR
*argv
[])
262 LoadString( GetModuleHandle(NULL
), IDS_Mangers
, (LPTSTR
) UsageMessage
,RC_STRING_MAX_SIZE
);
263 _ftprintf (stderr
, UsageMessage
);
265 LoadString( GetModuleHandle(NULL
), IDS_USING
, (LPTSTR
) UsageMessage
,RC_STRING_MAX_SIZE
);
266 _tprintf (UsageMessage
);
270 /* parse and execute */
271 int pande (int argc
, TCHAR
*argv
[])
273 PSM_CMD_DESCRIPTOR Command
= NULL
;
274 NTSTATUS Status
= STATUS_SUCCESS
;
276 /* Lookup the user command... */
277 Command
= LookupCommand (argv
[1]);
282 /* Connect to the SM in non-registering mode. */
283 Status
= SmConnectApiPort (0, 0, 0, & hSmApiPort
);
284 if (STATUS_SUCCESS
== Status
)
286 /* ...and execute it */
287 return Command
->EntryPoint (argc
, argv
);
289 LoadString( GetModuleHandle(NULL
), IDS_FAILS_MNG
, (LPTSTR
) UsageMessage
,RC_STRING_MAX_SIZE
);
290 _ftprintf (stderr
, UsageMessage
, Status
);
295 void loadlang(PSM_CMD_DESCRIPTOR cmd
)
298 if (cmd
==NULL
) return;
301 LoadString( GetModuleHandle(NULL
), IDS_boot
+i
, (LPTSTR
) &cmd
->Synopsis
[i
],RC_STRING_MAX_SIZE
);
305 int _tmain (int argc
, TCHAR
*argv
[])
310 ? print_synopsys (argc
, argv
)
311 : pande (argc
, argv
);