2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <sys/types.h>
34 #include <glib-object.h>
38 #include <QCoreApplication>
46 #include "pluginloader.h"
54 GMainLoop* mainLoop = nullptr;
58 void interrupt(int sign)
60 signal(sign, SIG_IGN);
61 cout<<"Signal caught. Exiting gracefully.\n"<<endl;
64 QCoreApplication::exit(0);
66 g_main_loop_quit(mainLoop);
73 void printhelp(const char *argv0);
75 static const char shortopts[] = "hvDc:d:";
77 static const struct option longopts[] = {
78 { "help", no_argument, NULL, 'h' }, ///< Print the help text
79 { "version", no_argument, NULL, 'v' }, ///< Print the version text
80 { "daemonise", no_argument, NULL, 'D' }, ///< Daemonise
81 { "config", required_argument, NULL, 'c' },
82 { "debug", required_argument, NULL, 'd' },
83 { NULL, 0, NULL, 0 } ///< End
86 int main(int argc, char **argv)
89 bool isdeamonize=false;
92 string config="/etc/ambd/config";
94 while ((optc = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
103 cout<<PROJECT_NAME<<endl;
104 cout<<"Version: "<<PROJECT_VERSION<<endl;
108 cout<<"Config: "<<optarg<<endl;
113 DebugOut::setDebugThreshhold(th);
116 cerr<<"Unknown option "<<optc<<endl;
129 QCoreApplication app(argc,argv);
133 mainLoop = g_main_loop_new(NULL, false);
139 /* Register signal handler */
140 signal(SIGINT, interrupt);
141 signal(SIGTERM, interrupt);
143 PluginLoader loader(config, new Core());
145 if(!loader.sources().size())
147 throw std::runtime_error("No sources present. aborting");
158 g_main_loop_run(mainLoop);
170 return; // already a daemon
174 fprintf(stderr, "%s:%s(%d) - fork error: %s", __FILE__, __FUNCTION__, __LINE__, strerror(errno));
179 exit(0); // parent exits
180 } // child (daemon) continues
181 setsid(); // obtain a new process group
182 for(i = getdtablesize(); i >= 0; --i)
184 close(i); // close all descriptors
186 { // handle standard I/O
187 i = open("/dev/null", O_RDWR);
191 // first instance continues
194 void printhelp(const char *argv0)
196 printf("Usage: %s [args]\n"
197 " [-D|--daemonise]\n"
199 " [-c|--config </path/to/config> \t]\n"
200 " [-d|--debug <level (0-5)>\t]\n"