Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[d6d138]: server / server.cpp Maximize Restore History

Download this file

server.cpp    482 lines (387 with data), 13.2 kB

  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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright (C) 2010 David Sugar, Tycho Softworks.
//
// 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/>.
#include "server.h"
using namespace BAYONNE_NAMESPACE;
using namespace UCOMMON_NAMESPACE;
#if defined(HAVE_SETRLIMIT) && defined(DEBUG)
#include <sys/time.h>
#include <sys/resource.h>
#endif
#ifdef HAVE_PWD_H
#include <pwd.h>
#include <grp.h>
#endif
static shell::flagopt helpflag('h',"--help", _TEXT("display this list"));
static shell::flagopt althelp('?', NULL, NULL);
static shell::flagopt background('b', "--background", _TEXT("run as daemon"));
static shell::flagopt altback('d', NULL, NULL);
static shell::numericopt concurrency('c', "--concurrency", _TEXT("process concurrency"), "level");
static shell::flagopt dump('C', "--dump", _TEXT("dump configuration"));
static shell::flagopt foreground('f', "--foreground", _TEXT("run in foreground"));
static shell::stringopt lang('L', "--lang", _TEXT("specify language"), "language", "C");
static shell::stringopt phrasebook('B', "--phrasebook", _TEXT("specify phrasebook directory"), "path", NULL);
static shell::stringopt prefix('P', "--prefix", _TEXT("specify alternate prefix path"), "path", NULL);
static shell::stringopt scripts('A', "--scripts", _TEXT("specify script directory"), "path", NULL);
static shell::stringopt definitions('I', "--definitions", _TEXT("specify script definitions"), "path", NULL);
static shell::stringopt prompts('D', "--prompts", _TEXT("specify prompt directory"), "path", NULL);
static shell::stringopt suffix('S', "--suffix", _TEXT("audio extension"), ".ext", ".au");
static shell::stringopt voice('V', "--voice", _TEXT("specify voice library"), "name", "default");
static shell::flagopt version(0, "--version", _TEXT("show version information"));
#ifdef HAVE_PWD_H
static shell::stringopt group('g', "--group", _TEXT("use specified group permissions"), "groupid", NULL);
#endif
static shell::stringopt loglevel('l', "--logging", _TEXT("set log level"), "level", "err");
static shell::counteropt priority('p', "--priority", _TEXT("set priority level"), "level");
static shell::flagopt restart('r', "--restartable", _TEXT("set to restartable process"));
#ifdef HAVE_PWD_H
static shell::stringopt user('u', "--user", _TEXT("user to run as"), "userid", NULL);
#endif
static shell::flagopt verbose('v', NULL, _TEXT("set verbosity, can be used multiple times"), false);
static shell::numericopt debuglevel('x', "--debug", _TEXT("set debug level directly"), "level", 0);
char *server::status = NULL;
#if defined(HAVE_SETRLIMIT) && defined(DEBUG)
static void corefiles(void)
{
struct rlimit core;
assert(getrlimit(RLIMIT_CORE, &core) == 0);
#ifdef MAX_CORE_SOFT
core.rlim_cur = MAX_CORE_SOFT;
#else
core.rlim_cur = RLIM_INFINITY;
#endif
#ifdef MAX_CORE_HARD
core.rlim_max = MAX_CORE_HARD;
#else
core.rlim_max = RLIM_INFINITY;
#endif
assert(setrlimit(RLIMIT_CORE, &core) == 0);
}
#else
static void corefiles(void)
{
}
#endif
static void dumpconfig(void)
{
#ifdef _MSWINDOWS_
printf("config = %s\n", BAYONNE_CFGPATH "/server.conf");
#else
printf("config = %s\n", getenv("BAYONNERC"));
#endif
printf("configs = %s\n", Env::get("configs"));
printf("controls = %s\n", Env::get("controls"));
printf("defines = %s\n", Env::get("definitions"));
printf("libexec = %s\n", Env::get("libexec"));
printf("scripts = %s\n", Env::get("scripts"));
printf("voices = %s\n", Env::get("voices"));
exit(0);
}
static void usage(void)
{
printf("%s\n", _TEXT("Usage: bayonne-server [options]"));
printf("%s\n\n", _TEXT("Start bayonne service"));
printf("%s\n", _TEXT("Options:"));
shell::help();
printf("\n%s\n", _TEXT("Report bugs to bayonne-devel@gnu.org"));
exit(0);
}
static void versioninfo(void)
{
printf("Bayonne " VERSION "\n%s", _TEXT(
"Copyright (C) 2007,2008,2009 David Sugar, Tycho Softworks\n"
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"));
exit(0);
}
static const char *userpath(const char *path)
{
if(!path)
return NULL;
#ifndef _MSWINDOWS_
if(eq(path, "~/", 2) && getuid()) {
const char *home = getenv("HOME");
if(home)
return strdup(str(home) + ++path);
}
#endif
return path;
}
static void dispatch(int slots)
{
int argc;
char *argv[65];
char *cp, *tokens;
DateTimeString dt;
const char *err;
Control::log("server starting %s", (const char *)dt);
if(slots > 0)
shell::log(shell::INFO, "started with %d timeslots active", slots);
else
shell::log(shell::ERR, "started with no timeslots active");
while(NULL != (cp = Control::receive())) {
shell::debug(9, "received request <%s>\n", cp);
dt.set();
if(eq(cp, "stop") || eq(cp, "down") || eq(cp, "exit"))
break;
if(eq(cp, "abort")) {
abort();
continue;
}
if(eq(cp, "reload")) {
Driver::reload();
continue;
}
if(eq(cp, "snapshot")) {
Driver::snapshot();
continue;
}
argc = 0;
tokens = NULL;
while(argc < 64 && NULL != (cp = const_cast<char *>(String::token(cp, &tokens, " \t", "{}")))) {
argv[argc++] = cp;
}
argv[argc] = NULL;
if(argc < 1)
continue;
if(eq(argv[0], "verbose")) {
if(argc != 2) {
// invalid:
Control::reply("missing or invalid argument");
continue;
}
verbose = shell::loglevel_t(atoi(argv[1]));
continue;
}
err = "unknown command";
//error:
Control::reply(err);
}
dt.set();
Control::log("server shutdown %s\n", (const char *)dt);
}
void server::start(int argc, char **argv, shell::mainproc_t svc)
{
const char *cp;
shell::bind("bayonne");
shell args(argc, argv);
bool detached = Env::init(&args);
if(!svc)
detached = false;
if((is(background) || is(altback)) && svc)
detached = true;
else if(is(foreground))
detached = false;
corefiles();
if(is(helpflag) || is(althelp) || args.argc() > 0)
usage();
if(is(version))
versioninfo();
#ifdef HAVE_PWD_H
cp = getenv("GROUP");
if(cp && *cp && !is(group))
group.set(cp);
cp = getenv("USER");
if(cp && *cp && !is(user))
user.set(cp);
#endif
// reset paths from config and env ...
keydata *paths = Driver::getPaths();
if(paths) {
cp = userpath(paths->get("phrasebook"));
if(!cp)
cp = userpath(paths->get("voices"));
if(cp && *cp && fsys::is_dir(cp))
Env::set("voices", cp);
cp = userpath(paths->get("scripts"));
if(cp && *cp && fsys::is_dir(cp))
Env::set("scripts", cp);
cp = userpath(paths->get("prompts"));
if(cp && *cp && fsys::is_dir(cp))
Env::set("prompts", cp);
cp = userpath(paths->get("definitions"));
if(cp && *cp && fsys::is_dir(cp))
Env::set("definitions", cp);
cp = userpath(paths->get("configs"));
if(cp && *cp && fsys::is_dir(cp))
Env::set("configs", cp);
cp = userpath(paths->get("libexec"));
if(cp && *cp && fsys::is_dir(cp))
Env::set("libexec", cp);
}
paths = Driver::getSystem();
if(paths) {
cp = paths->get("concurrency");
if(cp && *cp && !is(concurrency));
concurrency.set(atol(cp));
cp = paths->get("priority");
if(cp && *cp && !is(priority))
priority.set(atol(cp));
cp = paths->get("logging");
if(cp && *cp && !is(loglevel))
loglevel.set(strdup(cp));
}
#ifndef _MSWINDOWS_
// apply env overrides...
// these may come from the defaults file...
cp = getenv("VOICES");
if(cp && *cp)
Env::set("voices", cp);
cp = getenv("SCRIPTS");
if(cp && *cp)
Env::set("scripts", cp);
cp = getenv("CONCURRENCY");
if(cp && *cp && !is(concurrency))
concurrency.set(atol(cp));
cp = getenv("PRIORITY");
if(cp && *cp && !is(priority))
priority.set(atol(cp));
cp = getenv("LOGGING");
if(cp && *cp && !is(loglevel))
loglevel.set(strdup(cp));
#endif
// apply switch options if used...
if(is(phrasebook))
Env::set("voices", *phrasebook);
if(is(scripts))
Env::set("scripts", *scripts);
if(is(definitions))
Env::set("definitions", *definitions);
if(is(prompts))
Env::set("prompts", *prompts);
if(is(prefix))
Env::set("prefix", *prefix);
if(is(dump))
dumpconfig();
if(*concurrency < 0)
shell::errexit(1, "bayonne: concurrency: %ld: %s\n",
*concurrency, _TEXT("negative levels invalid"));
if(*concurrency > 0)
Thread::concurrency(*concurrency);
shell::priority(*priority);
#ifdef SCHED_RR
if(*priority > 0)
Thread::policy(SCHED_RR);
#endif
if(is(verbose))
verbose.set(*verbose + (unsigned)shell::INFO);
else {
if(atoi(*loglevel) > 0)
verbose.set(atoi(*loglevel));
else if(eq(*loglevel, "0") || eq(*loglevel, "no", 2) || eq(*loglevel, "fail", 4))
verbose.set((unsigned)shell::FAIL);
else if(eq(*loglevel, "err", 3))
verbose.set((unsigned)shell::ERR);
else if(eq(*loglevel, "warn", 4))
verbose.set((unsigned)shell::WARN);
else if(eq(*loglevel, "noti", 4))
verbose.set((unsigned)shell::NOTIFY);
else if(eq(*loglevel, "info"))
verbose.set((unsigned)shell::INFO);
else if(eq(*loglevel, "debug", 5))
verbose.set((unsigned)shell::DEBUG0 + atoi(*loglevel + 5));
}
if(is(debuglevel))
verbose.set((unsigned)shell::DEBUG0 + *debuglevel);
#ifdef HAVE_PWD_H
struct passwd *pwd = NULL;
struct group *grp = NULL;
// if root user, then see if we change permissions...
if(!getuid()) {
if(*user) {
if(atoi(*user))
pwd = getpwuid(atoi(*user));
else
pwd = getpwnam(*user);
if(!pwd)
shell::errexit(2, "*** apennine: %s: %s\n", *user,
_TEXT("unknown or invalid user id"));
}
}
if(*group) {
if(atoi(*group))
grp = getgrgid(atoi(*group));
else
grp = getgrnam(*group);
if(!grp)
shell::errexit(2, "*** bayonne: %s: %s\n", *group,
_TEXT("unknown or invalid group id"));
}
if(grp) {
umask(007);
setgid(grp->gr_gid);
}
int uid = 0;
if(pwd) {
umask(007);
if(!grp)
setgid(pwd->pw_gid);
uid = pwd->pw_uid;
}
endgrent();
endpwent();
#endif
const char *vardir = Env::get("prefix");
const char *rundir = Env::get("controls");
signals::setup();
fsys::createDir(rundir, 0770);
fsys::createDir(vardir, 0770);
if(fsys::changeDir(vardir))
shell::errexit(3, "*** bayonne: %s: %s\n",
vardir, _TEXT("data directory unavailable"));
shell::loglevel_t level = (shell::loglevel_t)*verbose;
shell::logmode_t logmode = shell::SYSTEM_LOG;
if(detached) {
args.detach(svc);
logmode = shell::CONSOLE_LOG;
}
shell::log("bayonne", level, logmode);
Script::init();
// Script::assign(keywords);
// load plugins?
if(!Control::create())
shell::errexit(1, "*** bayonne: %s\n",
_TEXT("no control file; exiting"));
// drop root privilege
#ifdef HAVE_PWD_H
if(uid)
setuid(uid);
#endif
if(is(restart))
args.restart();
status = new char[MAX_TIMESLOTS + 1];
memset(status, '?', MAX_TIMESLOTS);
const char *pidfile = args.getsym("pidfile");
if(pidfile) {
::remove(pidfile);
FILE *fp = fopen(pidfile, "w");
if(fp) {
fprintf(fp, " %ld\n", (long)getpid());
fclose(fp);
}
}
// create threads, etc...
signals::start();
notify::start();
unsigned tsc = Driver::startup();
if(tsc)
status[tsc] = 0;
dispatch(tsc);
if(pidfile)
::remove(pidfile);
Driver::shutdown();
notify::stop();
signals::stop();
}