summaryrefslogtreecommitdiff
path: root/include/wolf/service/service.h
blob: 04b505719a524ab3e854f023f1dd13231efb67ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
    Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef WOLF_SERVICE_H
#define WOLF_SERVICE_H

/**
 * @addtogroup wolf_win32 Windows-specific
 * @{
 */

/**
 * @addtogroup wolf_service Windows Service
 * @{
 */

/**
 * @file service.h
 * @brief helper to write a Windows service
 * @author Andreas Baumann <abaumann@yahoo.com>
 */

#define WIND32_LEAN_AND_MEAN
#include <windows.h>

#include "errors.h"

#ifdef __cplusplus
extern "C" {
#endif

extern SERVICE_STATUS_HANDLE service_status_handle;
extern SERVICE_STATUS service_status;
extern HANDLE service_stop_event;

/**
 * @brief structure to fill out when creating a Windows service.
 */
typedef struct wolf_service_params_t {
	LPCTSTR service_name;			/**< internal name of the service (for instance for
						     'net start <service_name>') */
	LPCTSTR service_display_name;		/**< the displayed name of the service (for instance
						     shown in the event log viewer application) */
	LPCTSTR service_description;		/**< the description (as shown in the service control */
} wolf_service_params_t;

/**
 * Installs the current binary as a Windows service.
 *
 * @param params   steers how the service should be created
 *
 * @return         WOLF_OK if the service was created, or an WOLF_ERR_INTERNAL
 *                 (which also gets logged with wolf_log)
 */
wolf_error_t wolf_service_install(	wolf_service_params_t params );

/**
 * Uninstalls the current binary as a Windows service.
 *
 * @param service_name the name of the service to remove from the service infrastructure
 *
 * @return         WOLF_OK if the service was removed, or an WOLF_ERR_INTERNAL
 *                 (which also gets logged with wolf_log)
 */
wolf_error_t wolf_service_remove(	LPCTSTR service_name );

/**
 * Start the service
 *
 * @param service_name the name of the service previously registered with
 *                     wolf_service_install
 * @param service_main the main function (which should implement startup/shutdown
 *                     and the main processing loop)
 *
 * @return             Doesn't return till the service terminates, then it
 *                     returns WOLF_OK.
 *                     On the other hand if we don't get called as service
 *                     we get a WOLF_ERR_INVALID_STATE indicating that the
 *                     service is run in the console, in this case you can
 *                     call wolf_service_start_console instead of this function.
 */
wolf_error_t wolf_service_start( 	LPTSTR service_name,
					LPSERVICE_MAIN_FUNCTION service_main );

wolf_error_t wolf_service_start_console( 	LPTSTR service_name,
						LPSERVICE_MAIN_FUNCTION service_main,
						DWORD argc,
						LPTSTR *argv );

/**
 * @brief events which can happen in a service
 */					
typedef enum wolf_service_event_t {
	WOLF_SERVICE_NO_EVENT,		/**< no event happened */
	WOLF_SERVICE_EVENT_TERMINATE	/**< terminate the service now */
} wolf_service_event_t;

/**
 * Suspend the current thread till an event happens. At the moment
 * we only get the SERVICE_EVENT_TERMINATE event.
 *
 * @param timeout   wait at most timeout seconds, then return also
 *                  without an event happening, error is set to
 *                  WOLF_ERR_TIMEOUT in this case
 * @param error     WOLF_OK if an event occured, WOLF_ERR_TIMEOUT if
 *                  no signal happened after timeout seconds
 *
 * @return          the event which happened
 */
typedef wolf_service_event_t (*LPWOLF_SERVICE_SUSPEND_FUNCTION)( int timeout, wolf_error_t *error );

/**
 * The current version of the suspend function, depends whether we started the service
 * in the console or really as service
 */
extern LPWOLF_SERVICE_SUSPEND_FUNCTION wolf_service_events_suspend;

#ifdef __cplusplus
}
#endif

/** @} */ /* @addtogroup wolf_service */

/** @} */ /* @addtogroup wolf_win32 */

#endif /* ifndef WOLF_SERVICE_H */