Example #1
0
def info():
    from csl import serviceType
    from csl import serviceDesc
    state = is_on()
    try:
        from csl import status
        if status():
            if state == "off":
                state = "started"
        else:
            if state == "on":
                state = "stopped"
    except:
        pass
    return "\n".join([serviceType, state, serviceDesc])
Example #2
0
def info():
    from csl import serviceType
    from csl import serviceDesc
    state = is_on()
    try:
        from csl import status
        if status():
            if state == "off":
                state = "started"
            elif state == "conditional":
                state = "conditional_started"
        else:
            if state == "on":
                state = "stopped"
            elif state == "conditional":
                state = "conditional_stopped"
    except:
        pass
    return serviceType, serviceDesc, state
Example #3
0
def info():
    from csl import serviceType
    from csl import serviceDesc
    state = is_on()
    try:
        from csl import status
        if status():
            if state == "off":
                state = "started"
            elif state == "conditional":
                state = "conditional_started"
        else:
            if state == "on":
                state = "stopped"
            elif state == "conditional":
                state = "conditional_stopped"
    except:
        pass
    return serviceType, serviceDesc, state
Example #4
0
def startService(command,
                 args=None,
                 pidfile=None,
                 makepid=False,
                 nice=None,
                 detach=False,
                 chuid=None,
                 donotify=False):
    """Start given service.

    command:  Path to the service executable.
    args:     Optional arguments to the service executable.
    pidfile:  Process ID of the service is kept in this file when running.
    nice:     This value is added to the service process' niceness value, which
              decreases its scheduling priority.
    chuid:    Change to this user:group before starting the service.
    detach:   If the service doesn't detach on its own, this option will fork
              and run it in the background.
    makepid:  Write the pid file if service does not create on its own. Mostly useful
              with the detach option.
    donotify: If True, automatically make Comar notification when service is run.
              Also automatically fail() if something goes wrong.
    """
    cmd = [command]
    if args:
        if isinstance(args, basestring):
            args = shlex.split(args)
        cmd.extend(args)

    try:
        from csl import status
        if status():
            # Already running, no need to send notification, just return OK
            return None
    except:
        pass

    if pidfile:
        pid = _getPid(pidfile)
        if _checkPid(pid, command=command):
            # Already running, no need to send notification, just return OK
            return None

    def fork_handler():
        if detach:
            # Set umask to a sane value
            # (other and group has no write permission by default)
            os.umask(022)
            # Detach from controlling terminal
            try:
                tty_fd = os.open("/dev/tty", os.O_RDWR)
                fcntl.ioctl(tty_fd, termios.TIOCNOTTY)
                os.close(tty_fd)
            except OSError:
                pass
            # Close IO channels
            devnull_fd = os.open("/dev/null", os.O_RDWR)
            os.dup2(devnull_fd, 0)
            os.dup2(devnull_fd, 1)
            os.dup2(devnull_fd, 2)
            # Detach from process group
            os.setsid()
        if nice is not None:
            os.nice(nice)
        if makepid and pidfile:
            file(pidfile, "w").write("%d\n" % os.getpid())
        if chuid:
            changeUID(chuid)

    popen = subprocess.Popen(cmd,
                             close_fds=True,
                             preexec_fn=fork_handler,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    if detach:
        if donotify:
            # We blindly send this, cause there is no way to track detached
            # process' return code.
            notify("System.Service", "Changed", (script(), "started"))
        return execReply(0)
    else:
        ret = execReply(popen.wait())
        if donotify:
            if ret == 0:
                notify("System.Service", "Changed", (script(), "started"))
            else:
                ret.stdout, ret.stderr = popen.communicate()
                err = "Unable to start service."
                if ret.stderr != "":
                    err = "Unable to start: " + str(ret.stderr)
                fail(err)
        return ret
Example #5
0
def startService(command, args=None, pidfile=None, makepid=False, nice=None, detach=False, chuid=None, donotify=False):
    """Start given service.

    command:  Path to the service executable.
    args:     Optional arguments to the service executable.
    pidfile:  Process ID of the service is kept in this file when running.
    nice:     This value is added to the service process' niceness value, which
              decreases its scheduling priority.
    chuid:    Change to this user:group before starting the service.
    detach:   If the service doesn't detach on its own, this option will fork
              and run it in the background.
    makepid:  Write the pid file if service does not create on its own. Mostly useful
              with the detach option.
    donotify: If True, automatically make Comar notification when service is run.
              Also automatically fail() if something goes wrong.
    """
    cmd = [ command ]
    if args:
        if isinstance(args, basestring):
            args = shlex.split(args)
        cmd.extend(args)

    try:
        from csl import status
        if status():
            # Already running, no need to send notification, just return OK
            return None
    except:
        pass

    if pidfile:
        pid = _getPid(pidfile)
        if _checkPid(pid, command=command):
            # Already running, no need to send notification, just return OK
            return None

    def fork_handler():
        if detach:
            # Set umask to a sane value
            # (other and group has no write permission by default)
            os.umask(022)
            # Detach from controlling terminal
            try:
                tty_fd = os.open("/dev/tty", os.O_RDWR)
                fcntl.ioctl(tty_fd, termios.TIOCNOTTY)
                os.close(tty_fd)
            except OSError:
                pass
            # Close IO channels
            devnull_fd = os.open("/dev/null", os.O_RDWR)
            os.dup2(devnull_fd, 0)
            os.dup2(devnull_fd, 1)
            os.dup2(devnull_fd, 2)
            # Detach from process group
            os.setsid()
        if nice is not None:
            os.nice(nice)
        if makepid and pidfile:
            file(pidfile, "w").write("%d\n" % os.getpid())
        if chuid:
            _changeUID(chuid)

    popen = subprocess.Popen(cmd, close_fds=True, preexec_fn=fork_handler, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if detach:
        if donotify:
            # We blindly send this, cause there is no way to track detached
            # process' return code.
            notify("System.Service", "Changed", (script(), "started"))
        return execReply(0)
    else:
        ret = execReply(popen.wait())
        if donotify:
            if ret == 0:
                notify("System.Service", "Changed", (script(), "started"))
            else:
                ret.stdout, ret.stderr = popen.communicate()
                err = "Unable to start service."
                if ret.stderr != "":
                    err = "Unable to start: " + str(ret.stderr)
                fail(err)
        return ret