Esempio n. 1
0
def read_require_fields_from_target_file(fil):
    config, prog, is_service = create_config(fil)

    if config == 1:
        wyslog("Error initialising target file: " + fil)
        return [1, 1]

    options = ["After", "Requires"]

    requires = set()

    for option in options:
        if config.has_option("Unit", option):
            after_services_str = config.get("Unit", option)[0]

            for unit in after_services_str.split(" "):
                if (
                    unit != "rescue.target"
                    and unit != "rescue.service"
                    and unit != "emergency.target"
                    and unit != "emergency.service"
                ):
                    requires.add(unit)
                    # required_str += unit + " "

    # print prog + ": "
    return requires, is_service
Esempio n. 2
0
def read_wants_from_target_file(fil):
    config, prog, is_service = create_config(fil)

    if config == 1:
        wyslog('Error initialising target file: ' + fil)
        return 1

    wants_path = '/etc/systemd/system/' + prog + '.target.wants/'

    options = ['Wants']

    wants = set()

    for option in options:
        if config.has_option("Unit", option):
            after_services_str = config.get("Unit", option)[0]

            for unit in after_services_str.split(" "):
                # ignore any references to rescue or emergency
                if unit != "rescue.target" and unit != "rescue.service" and unit != "emergency.target" and unit != "emergency.service":
                    wants.add(unit)
        else:
            break

    if not is_service and os.path.isdir(wants_path):
        wantsfiles = [
            f for f in os.listdir(wants_path)
            if os.path.isfile(os.path.join(wants_path, f))
        ]
        # add list to set
        wants |= set(wantsfiles)

    # special case for "default.target"
    if not is_service and prog == "default":
        # get the real name of the target, i.e. graphical
        real_target = os.path.basename(
            os.readlink('/etc/systemd/system/default.target')).replace(
                ".target", "")

        # create the wants path for the real target, create the list of files and append them to the wants set
        real_wants_path = '/etc/systemd/system/' + real_target + '.target.wants/'

        if os.path.isdir(real_wants_path):
            real_wantsfiles = [
                f for f in os.listdir(real_wants_path)
                if os.path.isfile(os.path.join(real_wants_path, f))
            ]
            wants |= set(real_wantsfiles)

    return wants
Esempio n. 3
0
def read_wants_from_target_file(fil):
    config, prog, is_service = create_config(fil)

    if config == 1:
        wyslog("Error initialising target file: " + fil)
        return 1

    wants_path = "/etc/systemd/system/" + prog + ".target.wants/"

    options = ["Wants"]

    wants = set()

    for option in options:
        if config.has_option("Unit", option):
            after_services_str = config.get("Unit", option)[0]

            for unit in after_services_str.split(" "):
                # ignore any references to rescue or emergency
                if (
                    unit != "rescue.target"
                    and unit != "rescue.service"
                    and unit != "emergency.target"
                    and unit != "emergency.service"
                ):
                    wants.add(unit)
        else:
            break

    if not is_service and os.path.isdir(wants_path):
        wantsfiles = [f for f in os.listdir(wants_path) if os.path.isfile(os.path.join(wants_path, f))]
        # add list to set
        wants |= set(wantsfiles)

    # special case for "default.target"
    if not is_service and prog == "default":
        # get the real name of the target, i.e. graphical
        real_target = os.path.basename(os.readlink("/etc/systemd/system/default.target")).replace(".target", "")

        # create the wants path for the real target, create the list of files and append them to the wants set
        real_wants_path = "/etc/systemd/system/" + real_target + ".target.wants/"

        if os.path.isdir(real_wants_path):
            real_wantsfiles = [
                f for f in os.listdir(real_wants_path) if os.path.isfile(os.path.join(real_wants_path, f))
            ]
            wants |= set(real_wantsfiles)

    return wants
Esempio n. 4
0
def read_require_fields_from_target_file(fil):
    config, prog, is_service = create_config(fil)

    if config == 1:
        wyslog('Error initialising target file: ' + fil)
        return [1, 1]

    options = ['After', 'Requires']

    requires = set()

    for option in options:
        if config.has_option("Unit", option):
            after_services_str = config.get("Unit", option)[0]

            for unit in after_services_str.split(" "):
                if unit != "rescue.target" and unit != "rescue.service" and unit != "emergency.target" and unit != "emergency.service":
                    requires.add(unit)
                    # required_str += unit + " "

    # print prog + ": "
    return requires, is_service
Esempio n. 5
0
def start_service(fil):
    config, prog, is_service = create_config(fil)

    unitname = os.path.splitext(os.path.basename(fil))[0]

    if config == 1:
        wyslog('Error initialising target file: ' + fil)
        return [1, 1]

    print bcolors.HEADER + "[WARLOCK]" + bcolors.BOLD + " Starting " + unitname + "..." + bcolors.ENDC
    actions = ['ExecStartPre', 'ExecStart', 'ExecStartPost']
    for action in actions:
        if config.has_option("Service", action):
            print "starting action " + action
            if _start(action, config, fil):
                print action + " completed successfully for " + unitname
            else:
                print action + " failed for " + unitname

    print bcolors.HEADER + "[WARLOCK]" + bcolors.BOLD + " Started: " + unitname + bcolors.ENDC
    wyslog("Started: " + unitname)
    return
Esempio n. 6
0
def start_service(fil):
    config, prog, is_service = create_config(fil)

    unitname = os.path.splitext(os.path.basename(fil))[0]

    if config == 1:
        wyslog("Error initialising target file: " + fil)
        return [1, 1]

    print bcolors.HEADER + "[WARLOCK]" + bcolors.BOLD + " Starting " + unitname + "..." + bcolors.ENDC
    actions = ["ExecStartPre", "ExecStart", "ExecStartPost"]
    for action in actions:
        if config.has_option("Service", action):
            print "starting action " + action
            if _start(action, config, fil):
                print action + " completed successfully for " + unitname
            else:
                print action + " failed for " + unitname

    print bcolors.HEADER + "[WARLOCK]" + bcolors.BOLD + " Started: " + unitname + bcolors.ENDC
    wyslog("Started: " + unitname)
    return
Esempio n. 7
0
def _start(action, config, fil):
    environc = os.environ.copy()

    unitname = os.path.splitext(os.path.basename(fil))[0]

    # create a path to a PID file, in case we need it
    pid_fname = "/var/run/{0}.pid".format(unitname)
    ignore_errors = False

    # check that the option for this action exists
    if config.has_option("Service", action):

        # set any environment variables as per the config
        if config.has_option("Service", "Environment"):
            for setenviron in config.get("Service", "Environment"):
                environc[setenviron.split("=")[0]] = setenviron.split("=")[1]

        # if there is an environment file, parse any variables from it into the environment
        if config.has_option("Service", "EnvironmentFile"):
            for setenvfile in config.get("Service", "EnvironmentFile"):
                setenvfile = re.sub("^-", "", setenvfile)
                environc = parse_envfile(setenvfile, environc)

        for start in config.get("Service", action):
            # start = config.get("Service", action)[0]

            # if the action starts with a "-", we need to ignore errors
            if start.startswith("-"):
                ignore_errors = True
                start = re.sub("^-", "", start)

            startaction = os.path.expandvars(start)
            startaction = shlex.split(startaction, comments=True)

            try:
                proc = subprocess.Popen(startaction, env=environc, preexec_fn=set_usr_grp(config, environc))
            except:
                if ignore_errors == False:
                    wyslog(start + " encountered an error - we will bail out")
                    return False
                else:
                    wyslog(start + " encountered an error - but we will ignore it")

            # the actual start action may need us to set a PID file, or wait for completion
            if action == "ExecStart":
                if not config.has_option("Service", "PidFile"):
                    with open(pid_fname, "w") as pid_file:
                        pid_file.write(str(proc.pid))

                # oneshot and forking both expect the process that gets called to
                # finish before we continue. We can do that for them.
                if config.has_option("Service", "Type"):
                    if config.get("Service", "Type")[0] in ("oneshot", "forking"):
                        proc.wait()
    else:
        wyslog("Action " + action + " does not exist for " + unitname)
        return False
    return True
Esempio n. 8
0
def _start(action, config, fil):
    environc = os.environ.copy()

    unitname = os.path.splitext(os.path.basename(fil))[0]

    # create a path to a PID file, in case we need it
    pid_fname = '/var/run/{0}.pid'.format(unitname)
    ignore_errors = False

    # check that the option for this action exists
    if config.has_option("Service", action):

        # set any environment variables as per the config
        if config.has_option("Service", "Environment"):
            for setenviron in config.get("Service", "Environment"):
                environc[setenviron.split('=')[0]] = setenviron.split('=')[1]

        # if there is an environment file, parse any variables from it into the environment
        if config.has_option("Service", "EnvironmentFile"):
            for setenvfile in config.get("Service", "EnvironmentFile"):
                setenvfile = re.sub("^-", "", setenvfile)
                environc = parse_envfile(setenvfile, environc)

        for start in config.get("Service", action):
            #start = config.get("Service", action)[0]

            # if the action starts with a "-", we need to ignore errors
            if start.startswith("-"):
                ignore_errors = True
                start = re.sub("^-", "", start)

            startaction = os.path.expandvars(start)
            startaction = shlex.split(startaction, comments=True)

            try:
                proc = subprocess.Popen(startaction,
                                        env=environc,
                                        preexec_fn=set_usr_grp(
                                            config, environc))
            except:
                if ignore_errors == False:
                    wyslog(start + " encountered an error - we will bail out")
                    return False
                else:
                    wyslog(start +
                           " encountered an error - but we will ignore it")

            # the actual start action may need us to set a PID file, or wait for completion
            if action == "ExecStart":
                if not config.has_option('Service', 'PidFile'):
                    with open(pid_fname, 'w') as pid_file:
                        pid_file.write(str(proc.pid))

                # oneshot and forking both expect the process that gets called to
                # finish before we continue. We can do that for them.
                if config.has_option("Service", "Type"):
                    if config.get("Service",
                                  "Type")[0] in ('oneshot', 'forking'):
                        proc.wait()
    else:
        wyslog("Action " + action + " does not exist for " + unitname)
        return False
    return True