forked from OPCFoundation/UA-LDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
295 lines (273 loc) · 9.6 KB
/
main.c
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Copyright (c) 1996-2021, OPC Foundation. All rights reserved.
The source code in this file is covered under a dual-license scenario:
- RCL: for OPC Foundation members in good-standing
- GPL V2: everybody else
RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/
GNU General Public License as published by the Free Software Foundation;
version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2
This source code 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.
*/
/* system includes */
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <limits.h>
#include <string.h>
/* uastack includes */
#include <opcua_serverstub.h>
/* local includes */
#include "config.h"
#include "ualds.h"
#include "settings.h"
/* local platform includes */
#include <platform.h>
#include <daemon.h>
#include <log.h>
#ifdef _WIN32
#include <crtdbg.h>
#endif /* _WIN32 */
void version()
{
fprintf(stdout, "OPC UA Local Discovery Server %s\n", UALDS_CONF_VERSION_STRING);
fprintf(stdout, "Copyright (c) 1996-2021, OPC Foundation. All rights reserved.n");
fprintf(stdout, "Build date: %s\n", __DATE__);
}
void usage(const char *szAppName)
{
fprintf(stderr, "Usage: %s [-h] [-c configfile] [-d] [-v] ", szAppName);
#ifdef HAVE_SERVICE_REGISTER
fprintf(stderr, "[-i <account>] [-p <password>] ");
#endif
#ifdef HAVE_SERVICE_UNREGISTER
fprintf(stderr, "[-u] ");
#endif
#if defined(HAVE_SERVICE_START) || defined(HAVE_SERVICE_STOP) || defined(HAVE_SERVICE_STATUS)
fprintf(stderr,"<command>\n");
#endif
fprintf(stderr, "Options:\n");
fprintf(stderr, " -d: Debug mode. The server sends verbose output to standard error, "
"and does not go into background.\n");
fprintf(stderr, " -D: No detach. When this options is specified, ualds will not detach "
"and does not become a daemon (or service on Windows).\n");
fprintf(stderr, " -v: Prints version information.\n");
#ifdef HAVE_SERVICE_REGISTER
fprintf(stderr, " -i: Installs the UA LDS as service. As account you can either specify SYSTEM,\n"
" or any other user account. Use option -p to specify a password for the account.\n");
fprintf(stderr, " -p: Specifies password for service account.\n");
#endif
#ifdef HAVE_SERVICE_UNREGISTER
fprintf(stderr, " -u: Uninstalls the UA LDS service.\n");
#endif
fprintf(stderr, " -h: Prints the synopsis and a short description of the possible options.\n");
#if defined(HAVE_SERVICE_START) || defined(HAVE_SERVICE_STOP) || defined(HAVE_SERVICE_STATUS)
fprintf(stderr, "Commands:\n");
#ifdef HAVE_SERVICE_START
fprintf(stderr, " start: Starts the service.\n");
#endif
#ifdef HAVE_SERVICE_STOP
fprintf(stderr, " stop: Stops the service.\n");
#endif
#ifdef HAVE_SERVICE_STATUS
fprintf(stderr, " status: Prints status information about service.\n");
#endif
#endif
}
int main(int argc, char* argv[])
{
int ret = EXIT_SUCCESS;
int opt;
int debug = 0, nodetach = 0;
int install = 0;
int uninstall = 0;
int start = 0;
int stop = 0;
int status = 0;
char szConfigFile[PATH_MAX] = {0};
char szValue[20];
char szUser[50];
char szPass[50];
LogTarget logtarget = UALDS_CONF_LOG_TARGET;
LogLevel loglevel = UALDS_CONF_LOG_LEVEL;
#if defined(_WIN32) && defined(_DEBUG)
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//_CrtSetBreakAlloc(307); // Comment or un-comment on need basis
#endif
/* parse commandline arguments */
while ((opt = getopt(argc, argv, "dDvhc:i:p:u")) != -1) {
switch (opt) {
case 'd':
debug = 1;
nodetach = 1;
break;
case 'D':
nodetach = 1;
break;
case 'v':
version();
exit(EXIT_SUCCESS);
break;
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
break;
case 'c':
strlcpy(szConfigFile, optarg, PATH_MAX);
break;
case 'i':
install = 1;
strlcpy(szUser, optarg, sizeof(szUser));
break;
case 'p':
strlcpy(szPass, optarg, sizeof(szPass));
break;
case 'u':
uninstall = 1;
break;
default: /* '?' */
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
/* parse commands */
if (optind < argc) {
if (strcmp(argv[optind], "start") == 0) {
start = 1;
} else if (strcmp(argv[optind], "stop") == 0) {
stop = 1;
} else if (strcmp(argv[optind], "status") == 0) {
status = 1;
}
}
#if _DEBUG
debug = 1;
nodetach = 1;
/*ualds_platform_sleep(60);*/ /* give some time for attaching with debugger */
#endif
ualds_platform_initialize();
if (szConfigFile[0] == 0) {
/* if not config file was given as commandline argument use the default path */
ualds_platform_getconfigfile_path(szConfigFile, sizeof(szConfigFile));
}
if (debug == 1) {
/* in debug mode we trace to stderr */
logtarget = UALDS_LOG_STDERR;
/* with debug log level */
loglevel = UALDS_LOG_DEBUG;
/* for configuration errors we log to stderr, because we don't have
* the log settings until ualds_settings_open has been called.
*/
ualds_openlog(logtarget, loglevel);
ret = ualds_settings_open(szConfigFile);
if (ret != 0) {
fprintf(stderr, "Failed to open config file '%s'.\n", szConfigFile);
ualds_platform_cleanup();
exit(EXIT_FAILURE);
}
ualds_closelog();
} else {
ret = ualds_settings_open(szConfigFile);
if (ret != 0) {
fprintf(stderr, "Failed to open config file '%s'.\n", szConfigFile);
ualds_platform_cleanup();
exit(EXIT_FAILURE);
}
/* read log settings */
ualds_settings_begingroup("Log");
if (ualds_settings_readstring("LogSystem", szValue, sizeof(szValue)) == 0) {
if (strcmp(szValue, "syslog") == 0) {
logtarget = UALDS_LOG_SYSLOG;
} else if (strcmp(szValue, "file") == 0) {
logtarget = UALDS_LOG_FILE;
} else if (strcmp(szValue, "stderr") == 0) {
logtarget = UALDS_LOG_STDERR;
}
}
if (ualds_settings_readstring("LogLevel", szValue, sizeof(szValue)) == 0) {
if (strcmp(szValue, "emerg") == 0) {
loglevel = UALDS_LOG_EMERG;
} else if (strcmp(szValue, "alert") == 0) {
loglevel = UALDS_LOG_ALERT;
} else if (strcmp(szValue, "crit") == 0) {
loglevel = UALDS_LOG_CRIT;
} else if (strcmp(szValue, "error") == 0) {
loglevel = UALDS_LOG_ERR;
} else if (strcmp(szValue, "warn") == 0) {
loglevel = UALDS_LOG_WARNING;
} else if (strcmp(szValue, "info") == 0) {
loglevel = UALDS_LOG_INFO;
} else if (strcmp(szValue, "debug") == 0) {
loglevel = UALDS_LOG_DEBUG;
}
}
ualds_settings_endgroup();
}
/* initialize logger with correct settings */
ualds_openlog(logtarget, loglevel);
/* set readonly/write flag for the configuration file */
ualds_settings_begingroup("General");
int icfgReadOnly = 1; // deafult is ReadOnly
ualds_settings_readint("ReadOnlyCfg", &icfgReadOnly);
ualds_settings_setReadOnly(icfgReadOnly);
ualds_settings_endgroup();
if (install == 1) {
#ifdef HAVE_SERVICE_REGISTER
/* force an unregister first */
ret = ServiceUnregister();
ualds_platform_sleep(1);
/* no matter if the server was unregistered, go ahead and attempt a forced register */
ret = ServiceRegister(szUser, szPass);
#else
fprintf(stderr, "Service install option is not supported on this platform.\n");
ret = EXIT_FAILURE;
#endif
} else if (uninstall == 1) {
#ifdef HAVE_SERVICE_UNREGISTER
ret = ServiceUnregister();
#else
fprintf(stderr, "Service uninstall option is not supported on this platform.\n");
ret = EXIT_FAILURE;
#endif
} else if (start == 1) {
#ifdef HAVE_SERVICE_START
ret = ServiceStart();
#else
fprintf(stderr, "Service start command is not supported on this platform.\n");
ret = EXIT_FAILURE;
#endif
} else if (stop == 1) {
#ifdef HAVE_SERVICE_STOP
ret = ServiceStop();
#else
fprintf(stderr, "Service stop command is not supported on this platform.\n");
ret = EXIT_FAILURE;
#endif
} else if (status == 1) {
#ifdef HAVE_SERVICE_STATUS
ret = ServiceStatus();
#else
fprintf(stderr, "Service status command is not supported on this platform.\n");
ret = EXIT_FAILURE;
#endif
}
if (!install && !uninstall && !start && !stop && !status) {
/* drop privileges when started as root or with SETUID */
ret = ualds_platform_drop_privileges();
if (ret != 0) {
ualds_log(UALDS_LOG_ERR, "Dropping privileges failed.");
ret = EXIT_FAILURE;
}
else if (nodetach == 0) {
/* daemonize / start service */
ret = daemonize();
} else {
/* no detach, run as console application */
ret = run();
}
ualds_log(UALDS_LOG_NOTICE, "Terminating with exitcode=%i", ret);
}
ualds_closelog();
ualds_platform_cleanup();
return ret;
}