File: //sbin/firewalld
#!/usr/bin/python -Es
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010-2012 Red Hat, Inc.
# Authors:
# Thomas Woerner <twoerner@redhat.com>
#
# 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, see <http://www.gnu.org/licenses/>.
#
# python fork magic derived from setroubleshoot
# Copyright (C) 2006,2007,2008,2009 Red Hat, Inc.
# Authors:
# John Dennis <jdennis@redhat.com>
# Dan Walsh <dwalsh@redhat.com>
import os
import sys
import dbus
import syslog
import traceback
import argparse
from firewall import config
from firewall.errors import *
from firewall.functions import firewalld_is_active
from firewall.core.logger import log, FileLog
parser = argparse.ArgumentParser()
parser.add_argument('--debug',
nargs='?', const=1, default=0, type=int,
choices=range(1, log.DEBUG_MAX+1),
help="""Enable logging of debug messages.
Additional argument in range 1..%s can be used
to specify log level.""" % log.DEBUG_MAX,
metavar="level")
parser.add_argument('--debug-gc',
help="""Turn on garbage collector leak information.
The collector runs every 10 seconds and if there are
leaks, it prints information about the leaks.""",
action="store_true")
parser.add_argument('--nofork',
help="""Turn off daemon forking,
run as a foreground process.""",
action="store_true")
parser.add_argument('--nopid',
help="""Disable writing pid file and don't check
for existing server process.""",
action="store_true")
args = parser.parse_args()
# check for root user
if os.getuid() != 0:
print(_("You need to be root to run %s.") % sys.argv[0])
sys.exit(-1)
log_file = FileLog(config.FIREWALLD_LOGFILE, "a")
log.setDateFormat("%Y-%m-%d %H:%M:%S")
log.setFormat("%(date)s %(label)s%(message)s")
log.setInfoLogging("*", log.syslog, [ log.FATAL, log.ERROR ])
log.addInfoLogging("*", log_file, [ log.FATAL, log.ERROR ])
log.setInfoLogging("*", log_file, [ log.WARNING ])
log.setDebugLogLevel(log.NO_INFO)
log.setDebugLogLevel(log.NO_DEBUG)
log.setDebugLogging("*", log_file, [ i for i in range(1, log.DEBUG_MAX+1) ])
if args.debug:
log.setInfoLogLevel(log.INFO_MAX)
log.setDebugLogLevel(args.debug)
log.addInfoLogging("*", log_file)
log.addDebugLogging("*", log_file)
if args.nofork:
log.addInfoLogging("*", log.stdout)
log.addDebugLogging("*", log.stdout)
if not args.nopid and firewalld_is_active():
log.fatal(_("Not starting FirewallD, already running."))
sys.exit(1)
try:
if not args.nofork:
# do the UNIX double-fork magic, see Stevens' "Advanced
# Programming in the UNIX Environment" for details (ISBN 0201563177)
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(os.umask(0o077) | 0o022)
import resource # Resource usage information.
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if (maxfd == resource.RLIM_INFINITY):
maxfd = 1024
# Iterate through and close all file descriptors.
for fd in range(0, maxfd):
try:
os.close(fd)
except OSError: # ERROR, fd wasn't open to begin with (ignored)
pass
# Redirect the standard I/O file descriptors to /dev/null
if (hasattr(os, "devnull")):
REDIRECT_TO = os.devnull
else:
REDIRECT_TO = "/dev/null"
os.open(REDIRECT_TO, os.O_RDWR) # standard input (0)
os.dup2(0, 1) # standard output (1)
os.dup2(0, 2) # standard error (2)
if not args.nopid:
# write the pid file
pid_file = "/var/run/firewalld.pid"
with open(pid_file, "w") as f:
f.write(str(os.getpid()))
# import here
from firewall.server import server
server.run_server(args.debug_gc)
if not args.nopid:
os.remove(pid_file)
except OSError as e:
log.fatal(_("Fork #1 failed: %d (%s)") % (e.errno, e.strerror))
log.error(traceback.format_exc())
if not args.nopid:
os.remove(pid_file)
sys.exit(1)
except dbus.exceptions.DBusException as e:
log.fatal(str(e))
log.error(traceback.format_exc())
if not args.nopid:
os.remove(pid_file)
sys.exit(1)
except IOError as e:
log.fatal(str(e))
log.error(traceback.format_exc())
if not args.nopid:
os.remove(pid_file)
sys.exit(1)
sys.exit(0)