130 lines
3.7 KiB
C
130 lines
3.7 KiB
C
/*
|
|
* vim:sw=4:ts=4:et
|
|
*/
|
|
|
|
|
|
#define _DEFAULT_SOURCE
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <errno.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
#include <getopt.h>
|
|
#include <pwd.h>
|
|
#include <libconfig.h>
|
|
#include <logging.h>
|
|
#include <config.h>
|
|
#include <dbhandler.h>
|
|
|
|
|
|
extern bool verbose;
|
|
extern bool debug;
|
|
const char DEFAULT_CONFIG_FILENAME[] = "./iiotfeeder.cfg";
|
|
|
|
|
|
void usage() {
|
|
printf("sinkserver for mainsfrequency counter implementations\n");
|
|
printf("https://home.hottis.de/gitlab/wolutator/mains-frequency-counter-stm32,\n");
|
|
printf("https://home.hottis.de/gitlab/wolutator/mains-frequency-counter-esp32,\n");
|
|
printf("https://home.hottis.de/gitlab/wolutator/mains-frequency-counter-rpi,\n");
|
|
printf("https://github.com/wollud1969/sinkConvert1\n");
|
|
printf("Repo: https://home.hottis.de/gitlab/wolutator/sinkserver\n");
|
|
printf("Version: " VERSION "\n");
|
|
printf("\nUsage\n");
|
|
printf(" -f FILENAME ...... Config file to be used\n");
|
|
printf(" -v ............... Verbose, writes all logging on stdout too\n");
|
|
printf(" -d ............... Also log debug output\n");
|
|
printf(" -s FACILITY ...... Sets syslog facility, only LOCAL[0..7]\n");
|
|
printf(" USER and DAEMON are supported\n");
|
|
printf(" -n USER .......... If started as root drop privileges and become\n");
|
|
printf(" USER\n");
|
|
printf(" -b ............... fork into background\n");
|
|
printf(" -h ............... This help\n");
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
const char *configFilename = DEFAULT_CONFIG_FILENAME;
|
|
|
|
const char *dropPrivilegesToUser = NULL;
|
|
bool doFork = false;
|
|
|
|
int c;
|
|
while ((c = getopt(argc, argv, "f:vds:hn:b")) != -1) {
|
|
switch (c) {
|
|
case 'f':
|
|
configFilename = strdup(optarg);
|
|
break;
|
|
case 'v':
|
|
verbose = true;
|
|
break;
|
|
case 'd':
|
|
debug = true;
|
|
break;
|
|
case 's':
|
|
setfacility(optarg);
|
|
break;
|
|
case 'n':
|
|
dropPrivilegesToUser = strdup(optarg);
|
|
break;
|
|
case 'b':
|
|
doFork = true;
|
|
break;
|
|
case 'h':
|
|
usage();
|
|
exit(0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ((getuid() == 0) && (dropPrivilegesToUser != NULL)) {
|
|
logmsg(LOG_INFO, "dropping root privileges, become %s", dropPrivilegesToUser);
|
|
struct passwd *userEntry = getpwnam(dropPrivilegesToUser);
|
|
if (userEntry == NULL) {
|
|
logmsg(LOG_ERR, "can not find entry for user %s", dropPrivilegesToUser);
|
|
exit(1);
|
|
}
|
|
|
|
if (setuid(userEntry->pw_uid) != 0) {
|
|
logmsg(LOG_ERR, "unable to drop root privileges to %d", userEntry->pw_uid);
|
|
exit(2);
|
|
}
|
|
}
|
|
|
|
logmsg(LOG_INFO, "Version: " VERSION);
|
|
|
|
if (doFork) {
|
|
int pid = fork();
|
|
if (pid == -1) {
|
|
logmsg(LOG_ERR, "error when forking into background: %d", errno);
|
|
exit(4);
|
|
}
|
|
if (pid != 0) {
|
|
logmsg(LOG_INFO, "successfully forking into background, child's pid is %d", pid);
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
t_configHandle configHandle;
|
|
if (0 != initConfig(configFilename, &configHandle)) {
|
|
logmsg(LOG_ERR, "error when reading configuration");
|
|
exit(3);
|
|
}
|
|
|
|
t_dbHandle *dbHandle = initDatabaseHandler(&configHandle);
|
|
if (! dbHandle) {
|
|
logmsg(LOG_ERR, "error when trying to initialize database connection");
|
|
exit(5);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
deinitDatabaseHandler(dbHandle);
|
|
deinitConfig(&configHandle);
|
|
}
|