forked from pulb/mailnag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailnagger
executable file
·142 lines (110 loc) · 3.84 KB
/
mailnagger
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
#!/usr/bin/env python3
#
# Copyright 2024 Timo Kankare <[email protected]>
# Copyright 2011 - 2020 Patrick Ulbrich <[email protected]>
# Copyright 2011 Leighton Earl <[email protected]>
# Copyright 2011 Ralf Hersel <[email protected]>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import gi
gi.require_version('GLib', '2.0')
from gi.repository import GObject, GLib
from dbus.mainloop.glib import DBusGMainLoop
import threading
import argparse
import logging
import os
import signal
from Mailnag.common.utils import fix_cwd
fix_cwd()
from Mailnag.common.config import cfg_exists
from Mailnag.common.dist_cfg import APP_VERSION
from Mailnag.common.utils import set_procname, init_logging, shutdown_existing_instance
from Mailnag.common.subproc import terminate_subprocesses
from Mailnag.common.exceptions import InvalidOperationException
from Mailnag.daemon.mailnagdaemon import MailnagDaemon
PROGNAME = 'mailnagger'
LOG_LEVEL = logging.DEBUG
def cleanup(daemon):
event = threading.Event()
def thread():
if daemon != None:
daemon.dispose()
terminate_subprocesses(timeout = 3.0)
event.set()
threading.Thread(target = thread).start()
event.wait(10.0)
if not event.is_set():
logging.warning('Cleanup takes too long. Enforcing termination.')
os._exit(os.EX_SOFTWARE)
if threading.active_count() > 1:
logging.warning('There are still active threads. Enforcing termination.')
os._exit(os.EX_SOFTWARE)
def get_args():
parser = argparse.ArgumentParser(prog=PROGNAME)
parser.add_argument('-q', '--quiet', action = 'store_true',
help = "don't print log messages to stdout")
parser.add_argument('-v', '--version', action = 'version',
version = '%s %s' % (PROGNAME, APP_VERSION))
return parser.parse_args()
def sigterm_handler(mainloop):
if mainloop != None:
mainloop.quit()
def main():
mainloop = GLib.MainLoop()
daemon = None
set_procname(PROGNAME)
DBusGMainLoop(set_as_default = True)
GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM,
sigterm_handler, mainloop)
# Get commandline arguments
args = get_args()
# Shut down an (possibly) already running Mailnag daemon
# (must be called before instantiation of the DBUSService).
shutdown_existing_instance()
# Note: don't start logging before an existing Mailnag
# instance has been shut down completely (will corrupt logfile).
init_logging(enable_stdout = (not args.quiet), \
enable_syslog = True, log_level = LOG_LEVEL)
try:
if not cfg_exists():
logging.critical(
"Cannot find configuration file. " + \
"Please run mailnagger-config first.")
exit(1)
def fatal_error_hdlr(ex):
# Note: don't raise an exception
# (e.g InvalidOperationException)
# in the error handler.
mainloop.quit()
def shutdown_request_hdlr():
if not mainloop.is_running():
raise InvalidOperationException(
"Mainloop is not running")
mainloop.quit()
# Initialize mailnag daemon and start checking threads
daemon = MailnagDaemon(
fatal_error_hdlr,
shutdown_request_hdlr)
# start mainloop for DBus communication
mainloop.run()
except KeyboardInterrupt:
pass # ctrl+c pressed
finally:
logging.info('Shutting down...')
cleanup(daemon)
if __name__ == '__main__': main()