Revision 39736270
Added by Hamish Coleman about 15 years ago
- ID 39736270c860147751b68999fd4645ae1717815d
| scm.h | ||
|---|---|---|
|
int mode; /* set to SVC_CONSOLE by the *-scm.c code */
|
||
|
int argc; char **argv; /* original cmdline args */
|
||
|
int (*init)(int, char **); /* called before main */
|
||
|
int (*main)(int); /* called to run the service */
|
||
|
int (*stop)(int); /* called by scm to tell the service to stop */
|
||
|
int (*main)(int, char **); /* called to run the service */
|
||
|
int (*stop)(void *); /* called by scm to tell the service to stop */
|
||
|
};
|
||
|
|
||
|
int SCM_Start(struct SCM_def *, int argc, char **argv);
|
||
| svctest.c | ||
|---|---|---|
|
dprintf(1,"%s:%i(%s) %s\n",__FILE__,__LINE__,__FUNCTION__,s)
|
||
|
|
||
|
int svctest_init(int argc, char **argv);
|
||
|
int svctest_main(int param1);
|
||
|
int svctest_stop(int param1);
|
||
|
int svctest_main(int argc, char **argv);
|
||
|
int svctest_stop(void *);
|
||
|
|
||
|
struct SCM_def sd = {
|
||
|
.name = "svctest",
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
int run = 1;
|
||
|
int svctest_main(int param1) {
|
||
|
int svctest_main(int argc, char **argv) {
|
||
|
while(run) {
|
||
|
1;
|
||
|
/* FIXME - sleep */
|
||
| ... | ... | |
|
return 0;
|
||
|
}
|
||
|
|
||
|
int svctest_stop(int param1) {
|
||
|
int svctest_stop(void *param1) {
|
||
|
run = 0;
|
||
|
trace("return 0");
|
||
|
return 0;
|
||
| wconsd.c | ||
|---|---|---|
|
struct cli_def *cli;
|
||
|
|
||
|
int wconsd_init(int argc, char **argv);
|
||
|
int wconsd_main(int param1);
|
||
|
int wconsd_stop(int param1);
|
||
|
int wconsd_main(int argc, char **argv);
|
||
|
int wconsd_stop(void *);
|
||
|
struct SCM_def sd = {
|
||
|
.name = "wconsd",
|
||
|
.desc = "wconsd - Telnet to Serial server",
|
||
| ... | ... | |
|
return 0;
|
||
|
}
|
||
|
|
||
|
int wconsd_stop(int param1) {
|
||
|
int wconsd_stop(void *param1) {
|
||
|
SetEvent(stopEvent);
|
||
|
return 0;
|
||
|
}
|
||
| ... | ... | |
|
return 0;
|
||
|
}
|
||
|
|
||
|
int wconsd_main(int param1)
|
||
|
int wconsd_main(int argc, char **argv)
|
||
|
{
|
||
|
HANDLE wait_array[2];
|
||
|
BOOL run=TRUE;
|
||
| win-scm.c | ||
|---|---|---|
|
if (opcode == SERVICE_CONTROL_STOP) {
|
||
|
svcStatus.dwCurrentState = SERVICE_STOP_PENDING;
|
||
|
SetServiceStatus( svcHandle, &svcStatus );
|
||
|
global_sd->stop(0);
|
||
|
global_sd->stop(NULL);
|
||
|
return;
|
||
|
}
|
||
|
SetServiceStatus( svcHandle, &svcStatus );
|
||
| ... | ... | |
|
svcHandle = RegisterServiceCtrlHandler(sd->name,ServiceCtrlHandler);
|
||
|
if (!svcHandle) {
|
||
|
/* FIXME - use SvcReportEvent() */
|
||
|
printf("RegisterServiceCtrlHandler failed %d\n", GetLastError());
|
||
|
printf("RegisterServiceCtrlHandler failed %u\n",
|
||
|
(unsigned int)GetLastError());
|
||
|
return;
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
sd->mode=SVC_OK;
|
||
|
if ((err=sd->init(argc,argv))!=0) {
|
||
|
svcStatus.dwCurrentState = SERVICE_STOPPED;
|
||
|
svcStatus.dwWin32ExitCode = err;
|
||
|
SetServiceStatus( svcHandle, &svcStatus );
|
||
|
return;
|
||
|
if (sd->init) {
|
||
|
if ((err=sd->init(argc,argv))!=0) {
|
||
|
svcStatus.dwCurrentState = SERVICE_STOPPED;
|
||
|
svcStatus.dwWin32ExitCode = err;
|
||
|
SetServiceStatus( svcHandle, &svcStatus );
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
svcStatus.dwCurrentState = SERVICE_RUNNING;
|
||
|
svcStatus.dwWin32ExitCode = NO_ERROR;
|
||
|
SetServiceStatus( svcHandle, &svcStatus );
|
||
|
|
||
|
err=sd->main(0);
|
||
|
err=sd->main(argc,argv);
|
||
|
|
||
|
svcStatus.dwCurrentState = SERVICE_STOPPED;
|
||
|
svcStatus.dwWin32ExitCode = NO_ERROR;
|
||
| ... | ... | |
|
int SCM_Start_Console(struct SCM_def *sd) {
|
||
|
|
||
|
sd->mode=SVC_CONSOLE;
|
||
|
int err = sd->init(sd->argc,sd->argv);
|
||
|
if (err!=0) {
|
||
|
return SVC_FAIL;
|
||
|
int err;
|
||
|
if (sd->init) {
|
||
|
err = sd->init(sd->argc,sd->argv);
|
||
|
if (err!=0) {
|
||
|
return SVC_FAIL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sd->main(0);
|
||
|
sd->main(sd->argc,sd->argv);
|
||
|
return SVC_OK;
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
*/
|
||
|
char buf[100];
|
||
|
if (getenv("USERNAME") && getenv("SESSIONNAME")
|
||
|
&& GetConsoleTitle(&buf,sizeof(buf))) {
|
||
|
&& GetConsoleTitle((LPTSTR)&buf,sizeof(buf))) {
|
||
|
return SCM_Start_Console(sd);
|
||
|
}
|
||
|
|
||
| ... | ... | |
|
static char path[MAX_PATH];
|
||
|
|
||
|
if( !GetModuleFileName( NULL, path, MAX_PATH ) ) {
|
||
|
printf("GetModuleFileName failed %d\n", GetLastError());
|
||
|
printf("GetModuleFileName failed %u\n",
|
||
|
(unsigned int)GetLastError());
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
Also available in: Unified diff
Pass the args to the main function and thus allow the init function to be optional.