~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/org/jcsp/win32/svcclasses.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    //////////////////////////////////////////////////////////////////////
 
2
    //                                                                  //
 
3
    //  JCSP ("CSP for Java") Libraries                                 //
 
4
    //  Copyright (C) 1996-2006 Peter Welch and Paul Austin.            //
 
5
    //                2001-2004 Quickstone Technologies Limited.        //
 
6
    //                                                                  //
 
7
    //  This library is free software; you can redistribute it and/or   //
 
8
    //  modify it under the terms of the GNU Lesser General Public      //
 
9
    //  License as published by the Free Software Foundation; either    //
 
10
    //  version 2.1 of the License, or (at your option) any later       //
 
11
    //  version.                                                        //
 
12
    //                                                                  //
 
13
    //  This library is distributed in the hope that it will be         //
 
14
    //  useful, but WITHOUT ANY WARRANTY; without even the implied      //
 
15
    //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR         //
 
16
    //  PURPOSE. See the GNU Lesser General Public License for more     //
 
17
    //  details.                                                        //
 
18
    //                                                                  //
 
19
    //  You should have received a copy of the GNU Lesser General       //
 
20
    //  Public License along with this library; if not, write to the    //
 
21
    //  Free Software Foundation, Inc., 59 Temple Place, Suite 330,     //
 
22
    //  Boston, MA 02111-1307, USA.                                     //
 
23
    //                                                                  //
 
24
    //  Author contact: P.H.Welch@ukc.ac.uk                             //
 
25
    //                                                                  //
 
26
    //                                                                  //
 
27
    //////////////////////////////////////////////////////////////////////
 
28
 
 
29
#include "svcclasses.h"
 
30
#include <stdio.h>
 
31
#include <conio.h>
 
32
 
 
33
CServiceManager::CServiceManager(TCHAR *machine) {
 
34
        schSCManager = OpenSCManager(machine, NULL, SC_MANAGER_ALL_ACCESS);
 
35
}
 
36
 
 
37
CServiceManager::~CServiceManager(void) {
 
38
        if (schSCManager) CloseServiceHandle(schSCManager);
 
39
}
 
40
 
 
41
int CServiceManager::ok(void) {
 
42
        return (schSCManager != NULL);
 
43
}
 
44
 
 
45
CService::CService(CServiceManager *mgr, TCHAR *name) {
 
46
        schService = OpenService(schSCManager = mgr->schSCManager, name, SERVICE_ALL_ACCESS);
 
47
}
 
48
 
 
49
CService::~CService(void) {
 
50
        if (schService) CloseServiceHandle(schService);
 
51
}
 
52
 
 
53
int CService::ok(void) {
 
54
        return (schService != NULL);
 
55
}
 
56
 
 
57
void CService::install(TCHAR *name, TCHAR *disp, TCHAR *path) {
 
58
        if (schService) CloseServiceHandle(schService);
 
59
        schService = CreateService(
 
60
                schSCManager,               // SCManager database
 
61
                name,                       // name of service
 
62
                disp,                       // name to display
 
63
                SERVICE_ALL_ACCESS,         // desired access
 
64
                SERVICE_WIN32_OWN_PROCESS,  // service type
 
65
                SERVICE_AUTO_START,         // start type
 
66
                SERVICE_ERROR_NORMAL,       // error control type
 
67
                path,                       // service's binary
 
68
                NULL,                       // no load ordering group
 
69
                NULL,                       // no tag identifier
 
70
                NULL,                       // dependencies
 
71
                NULL,                       // LocalSystem account
 
72
                NULL);                      // no password
 
73
}
 
74
 
 
75
int CService::destroy(void) {
 
76
        return DeleteService(schService);
 
77
}
 
78
 
 
79
int CService::getState(void) {
 
80
        SERVICE_STATUS sta;
 
81
        if (!QueryServiceStatus(schService, &sta)) return 0;
 
82
        return sta.dwCurrentState;
 
83
}
 
84
 
 
85
int CService::isStarted(void) { return (getState() == SERVICE_RUNNING); }
 
86
 
 
87
int CService::isStopped(void) { return (getState() == SERVICE_STOPPED); }
 
88
 
 
89
void CService::start(void) {
 
90
        ChangeServiceConfig(
 
91
                schService,
 
92
                SERVICE_NO_CHANGE,
 
93
                SERVICE_AUTO_START,
 
94
                SERVICE_NO_CHANGE,
 
95
                NULL,
 
96
                NULL,
 
97
                NULL,
 
98
                NULL,
 
99
                NULL,
 
100
                NULL,
 
101
                NULL
 
102
        );
 
103
        StartService(schService, 0, NULL);
 
104
}
 
105
 
 
106
void CService::stop(void) {
 
107
        SERVICE_STATUS sta;
 
108
        ControlService(schService, SERVICE_CONTROL_STOP, &sta);
 
109
}
 
110
 
 
111
static SERVICE_STATUS          sta;
 
112
static SERVICE_STATUS_HANDLE   hsta;
 
113
static CMyService *service = NULL;
 
114
static TCHAR *service_name = NULL;
 
115
 
 
116
static void setstate(int s) {
 
117
        if ((s == SERVICE_RUNNING) || (s == SERVICE_STOPPED)) {
 
118
                sta.dwCheckPoint = 0;
 
119
        } else {
 
120
                sta.dwCheckPoint++;
 
121
        }
 
122
        sta.dwCurrentState = s;
 
123
        SetServiceStatus(hsta, &sta);
 
124
}
 
125
 
 
126
static void WINAPI ctrlproc(DWORD ctrl) {
 
127
    if (ctrl == SERVICE_CONTROL_STOP) {
 
128
            setstate(SERVICE_STOP_PENDING);
 
129
        service->stop();
 
130
    } else {
 
131
            setstate(sta.dwCurrentState);
 
132
    }
 
133
}
 
134
 
 
135
static void WINAPI startproc(int argc, TCHAR **argv) {
 
136
    hsta = RegisterServiceCtrlHandler(service_name, ctrlproc);
 
137
    sta.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
 
138
    sta.dwServiceSpecificExitCode = 0;
 
139
    sta.dwControlsAccepted = SERVICE_ACCEPT_STOP;
 
140
    sta.dwWin32ExitCode = 0;
 
141
    sta.dwWaitHint = 3000;
 
142
    sta.dwCheckPoint = 0;
 
143
    setstate(SERVICE_RUNNING);
 
144
    service->start(argc, argv);
 
145
    setstate(SERVICE_STOPPED);
 
146
}
 
147
 
 
148
static SERVICE_TABLE_ENTRY dispatch[2] = { { NULL, (LPSERVICE_MAIN_FUNCTION)startproc }, { NULL, NULL } };
 
149
 
 
150
void ntservice_schedule (TCHAR *name, CMyService *svc) {
 
151
        service_name = dispatch[0].lpServiceName = strdup(name);
 
152
        service = svc;
 
153
        StartServiceCtrlDispatcher (dispatch);
 
154
}