Revision 211ce618
Added by Hamish Coleman almost 16 years ago
- ID 211ce61879a3f9c4435a2fc90041d58d65c203fd
scm.h | ||
---|---|---|
struct SCM_def {
|
||
char *name;
|
||
char *desc;
|
||
int mode; /* set to SVC_CONSOLE by the *-scm.c code */
|
||
int (*init)(int, char **);
|
||
int (*main)(int);
|
||
int (*stop)(int);
|
wconsd.c | ||
---|---|---|
WSADATA wsaData;
|
||
int err;
|
||
|
||
if (sd->mode==SVC_CONSOLE) {
|
||
/* We are running in a command-line mode */
|
||
dprintf_to_stdout=1;
|
||
|
||
dprintf(1,"\n"
|
||
"wconsd: Serial Console server (version %s)\n",VERSION);
|
||
dprintf(1,
|
||
" (see http://wob.zot.org/2/wiki/wconsd for more info)\n\n");
|
||
|
||
dprintf(1,"wconsd: Foreground mode\n");
|
||
}
|
||
|
||
/* setup the libcli early so that modules can use it */
|
||
if (!(cli = cli_init())) {
|
||
dprintf(1,"wconsd: wconsd_init: failed run cli_init\n");
|
||
... | ... | |
// debug info for when I test this as a service
|
||
dprintf(1,"wconsd: started with argc==%i\n",argc);
|
||
|
||
if (argc==1 || argc==0) {
|
||
/* assume that our messages are going to the debug log */
|
||
dprintf_to_stdout=0;
|
||
|
||
// assume that our messages are going to the debug log
|
||
dprintf_to_stdout=0;
|
||
|
||
if (SCM_Start(&sd)!=SVC_CONSOLE) {
|
||
return 0;
|
||
}
|
||
|
||
// fall through and try running as a command-line application
|
||
}
|
||
|
||
// We are running in debug mode (or any other command-line mode)
|
||
dprintf_to_stdout=1;
|
||
|
||
dprintf(1,"\n"
|
||
"wconsd: Serial Console server (version %s)\n",VERSION);
|
||
dprintf(1,
|
||
" (see http://wob.zot.org/2/wiki/wconsd for more info)\n\n");
|
||
|
||
/* we are running as a console app so simulate the SCM */
|
||
/* TODO - this could all be done inside the *-scm.c code */
|
||
int r=wconsd_init(argc,argv);
|
||
if (r!=0) {
|
||
dprintf(1,"wconsd: wconsd_init failed, return code %d\n",r);
|
||
if (SCM_Start(&sd,argc,argv)!=SVC_OK) {
|
||
dprintf(1,"Service startup failed\n");
|
||
return 1;
|
||
}
|
||
|
||
dprintf(1,"wconsd: Foreground mode\n");
|
||
wconsd_main(0);
|
||
|
||
return 0;
|
||
}
|
||
|
win-scm.c | ||
---|---|---|
svcStatus.dwCurrentState = SERVICE_START_PENDING;
|
||
SetServiceStatus( svcHandle, &svcStatus );
|
||
|
||
sd->mode=SVC_OK;
|
||
if ((err=sd->init(argc,argv))!=0) {
|
||
svcStatus.dwCurrentState = SERVICE_STOPPED;
|
||
svcStatus.dwWin32ExitCode = err;
|
||
... | ... | |
return;
|
||
}
|
||
|
||
int SCM_Start(struct SCM_def *sd) {
|
||
int SCM_Start_Console(const int argc, const char **argv) {
|
||
|
||
global_sd->mode=SVC_CONSOLE;
|
||
int err = sd->init(argc,argv);
|
||
if (err!=0) {
|
||
printf("SCM_Start_Console: init failed, return code %d\n",err);
|
||
return SVC_FAIL;
|
||
}
|
||
|
||
sd->main(0);
|
||
return SVC_OK;
|
||
}
|
||
|
||
int SCM_Start(struct SCM_def *sd, const int argc, const char **argv) {
|
||
SERVICE_TABLE_ENTRY ServiceTable[] = {
|
||
{ "", ServiceMain },
|
||
{ NULL, NULL }
|
||
... | ... | |
|
||
global_sd = sd;
|
||
|
||
/* If we have commandline args, then we cannot have been started
|
||
* by the Windows SCM
|
||
*/
|
||
if (argc<2) {
|
||
return SCM_Start_Console(argc,argv);
|
||
}
|
||
|
||
/* try to run as a service */
|
||
if (StartServiceCtrlDispatcher(ServiceTable)==0) {
|
||
int err = GetLastError();
|
||
|
||
if (err == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
|
||
/* TODO - could run the console service from here */
|
||
return SVC_CONSOLE;
|
||
return SCM_Start_Console(argc,argv);
|
||
}
|
||
|
||
/* any other error, assume fatal */
|
Also available in: Unified diff
Move the console mode handling inside the SCM for increased consistancy