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
|
/*
* Copyright (c) 2012 Novell, Inc.
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* 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, contact Novell, Inc.
*
* To contact Novell about this file by physical or electronic mail, you may
* find current contact information at www.novell.com.
*/
#include <stdlib.h>
#include <getopt.h>
#include <iostream>
#include <string>
#include <snapper/Log.h>
#include <dbus/DBusMainLoop.h>
#include "MetaSnapper.h"
#include "Client.h"
#include "Background.h"
#include "Types.h"
using namespace std;
const int idle_time = 60;
const int snapper_cleanup_time = 30;
bool log_stdout = false;
bool log_debug = false;
class MyMainLoop : public DBus::MainLoop
{
public:
MyMainLoop(DBusBusType type);
~MyMainLoop();
void method_call(DBus::Message& message);
void signal(DBus::Message& message);
void client_disconnected(const string& name);
void periodic();
int periodic_timeout();
};
MyMainLoop::MyMainLoop(DBusBusType type)
: MainLoop(type)
{
}
MyMainLoop::~MyMainLoop()
{
}
void
MyMainLoop::method_call(DBus::Message& msg)
{
y2deb("method call sender:'" << msg.get_sender() << "' path:'" << msg.get_path() <<
"' interface:'" << msg.get_interface() << "' member:'" << msg.get_member() << "'");
reset_idle_count();
if (msg.is_method_call(DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
{
Client::introspect(*this, msg);
}
else
{
boost::unique_lock<boost::shared_mutex> lock(big_mutex);
Clients::iterator client = clients.find(msg.get_sender());
if (client == clients.end())
{
y2deb("client connected invisible '" << msg.get_sender() << "'");
add_client_match(msg.get_sender());
client = clients.add(msg.get_sender());
set_idle_timeout(-1);
}
client->add_task(*this, msg);
}
}
void
MyMainLoop::signal(DBus::Message& msg)
{
y2deb("signal sender:'" << msg.get_sender() << "' path:'" << msg.get_path() <<
"' interface:'" << msg.get_interface() << "' member:'" << msg.get_member() << "'");
}
void
MyMainLoop::client_disconnected(const string& name)
{
y2deb("client disconnected '" << name << "'");
remove_client_match(name);
boost::unique_lock<boost::shared_mutex> lock(big_mutex);
Clients::iterator client = clients.find(name);
if (client != clients.end())
{
client->zombie = true;
client->thread.interrupt();
}
reset_idle_count();
}
void
MyMainLoop::periodic()
{
boost::unique_lock<boost::shared_mutex> lock(big_mutex);
clients.remove_zombies();
if (clients.empty() && backgrounds.empty())
set_idle_timeout(idle_time);
for (MetaSnappers::iterator it = meta_snappers.begin(); it != meta_snappers.end(); ++it)
{
if (it->is_loaded() && it->unused_for() > snapper_cleanup_time)
it->unload();
}
}
int
MyMainLoop::periodic_timeout()
{
boost::unique_lock<boost::shared_mutex> lock(big_mutex);
if (clients.has_zombies())
return 1000;
if (!backgrounds.empty())
return 1000;
for (MetaSnappers::const_iterator it = meta_snappers.begin(); it != meta_snappers.end(); ++it)
if (it->is_loaded() && it->use_count() == 0)
return 1000;
return -1;
}
void
log_do(LogLevel level, const string& component, const char* file, const int line, const char* func,
const string& text)
{
cout << text << endl;
}
bool
log_query(LogLevel level, const string& component)
{
return log_debug || level != DEBUG;
}
void usage() __attribute__ ((__noreturn__));
void
usage()
{
cerr << "Try 'snapperd --help' for more information." << endl;
exit(EXIT_FAILURE);
}
void help() __attribute__ ((__noreturn__));
void
help()
{
cout << "usage: snapperd [--options]" << endl
<< endl;
cout << " Options:" << endl
<< "\t--stdout, -s\t\t\tLog to stdout." << endl
<< "\t--debug, -d\t\t\tTurn on debugging." << endl
<< endl;
exit(EXIT_SUCCESS);
}
int
main(int argc, char** argv)
{
const struct option options[] = {
{ "stdout", no_argument, 0, 's' },
{ "debug", no_argument, 0, 'd' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }
};
while (true)
{
int option_index = 0;
int c = getopt_long(argc, argv, "+sdh", options, &option_index);
if (c == -1)
break;
switch (c)
{
case 's':
log_stdout = true;
break;
case 'd':
log_debug = true;
break;
case 'h':
help();
default:
usage();
}
}
if (optind < argc)
{
cerr << "snapperd: unrecognized option '" << argv[optind] << "'" << endl;
usage();
}
umask(0027);
if (!log_stdout)
{
initDefaultLogger();
setLogQuery(&log_query);
}
else
{
setLogDo(&log_do);
setLogQuery(&log_query);
}
dbus_threads_init_default();
MyMainLoop mainloop(DBUS_BUS_SYSTEM);
mainloop.set_idle_timeout(idle_time);
y2mil("Requesting DBus name");
mainloop.request_name(SERVICE, DBUS_NAME_FLAG_REPLACE_EXISTING);
y2mil("Loading snapper configs");
meta_snappers.init();
y2mil("Listening for method calls and signals");
mainloop.run();
y2mil("Exiting");
return 0;
}
|