function start($aspid = false)
{
    $unix = new unix();
    $sock = new sockets();
    $Masterbin = $unix->find_program("syncthing");
    if (!is_file($Masterbin)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]}, syncthing not installed\n";
        }
        return;
    }
    if (!$aspid) {
        $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
        $pid = $unix->get_pid_from_file($pidfile);
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Already Artica task running PID {$pid} since {$time}mn\n";
            }
            return;
        }
        @file_put_contents($pidfile, getmypid());
    }
    $pid = PID_NUM();
    if ($unix->process_exists($pid)) {
        $timepid = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Service already started {$pid} since {$timepid}Mn...\n";
        }
        if ($GLOBALS["MONIT"]) {
            @file_put_contents($GLOBALS["PID_PATH"], $pid);
        }
        return;
    }
    $EnableSyncThing = intval($sock->GET_INFO("EnableSyncThing"));
    $SyncThingPort = intval($sock->GET_INFO("SyncThingPort"));
    if ($SyncThingPort == 0) {
        $SyncThingPort = 8000;
    }
    if ($EnableSyncThing == 0) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} service disabled (see EnableSyncThing)\n";
        }
        stop();
        return;
    }
    $php5 = $unix->LOCATE_PHP5_BIN();
    $sysctl = $unix->find_program("sysctl");
    $echo = $unix->find_program("echo");
    $nohup = $unix->find_program("nohup");
    $kill = $unix->find_program("kill");
    @mkdir("/etc/syncthing", 0755, true);
    $RESTART = false;
    if (!is_file("/etc/syncthing/config.xml")) {
        $RESTART = true;
    }
    if (is_file("/etc/syncthing/config.xml")) {
        change_port();
    }
    $syncthing_version = syncthing_version();
    $tmpf = "/var/log/syncthing.log";
    $cmd = "{$nohup} {$Masterbin} -home /etc/syncthing -no-browser >{$tmpf} 2>&1 &";
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} service v{$syncthing_version}\n";
    }
    shell_exec($cmd);
    for ($i = 1; $i < 5; $i++) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} waiting {$i}/5\n";
        }
        sleep(1);
        $pid = PID_NUM();
        if ($unix->process_exists($pid)) {
            break;
        }
    }
    $pid = PID_NUM();
    if ($unix->process_exists($pid)) {
        if ($RESTART) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Must restart\n";
            }
            $unix->THREAD_COMMAND_SET("/etc/init.d/syncthing restart");
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Success PID {$pid}\n";
        }
        @unlink($tmpf);
        return true;
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} Failed\n";
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]} {$cmd}\n";
    }
    $f = explode("\n", @file_get_contents($tmpf));
    while (list($index, $line) = each($f)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: {$line}\n";
        }
    }
    system("printenv");
    @unlink($tmpf);
}
Exemple #2
0
function syncthing()
{
    if (!is_file("/etc/init.d/artica-postfix")) {
        return;
    }
    $bin = $GLOBALS["CLASS_UNIX"]->find_program("syncthing");
    if (!is_file($bin)) {
        if ($GLOBALS["VERBOSE"]) {
            echo __FUNCTION__ . " SYNCTHING_INSTALLED = FALSE\n";
        }
        $l[] = "[APP_SYNCTHING]";
        $l[] = "service_name=APP_SYNCTHING";
        $l[] = "installed=0";
        $l[] = "service_disabled=0";
        return @implode("\n", $l);
    }
    $EnableSyncThing = intval($GLOBALS["CLASS_SOCKETS"]->GET_INFO("EnableSyncThing"));
    $master_pid = syncthing_pid();
    if ($EnableSyncThing == 0) {
        if ($GLOBALS["CLASS_UNIX"]->process_exists($master_pid)) {
            shell_exec2("/etc/init.d/syncthing stop");
        }
    }
    $l[] = "[APP_SYNCTHING]";
    $l[] = "service_name=APP_SYNCTHING";
    $l[] = "master_version=" . syncthing_version();
    $l[] = "service_disabled={$EnableSyncThing}";
    $l[] = "watchdog_features=1";
    $l[] = "installed=1";
    $l[] = "family=system";
    $l[] = "service_cmd=/etc/init.d/syncthing";
    if ($EnableSyncThing == 0) {
        if ($GLOBALS["CLASS_UNIX"]->process_exists($master_pid)) {
            $cmd = "{$GLOBALS["nohup"]} {$GLOBALS["NICE"]}/etc/init.d/syncthing stop >/dev/null 2>&1 &";
            events("{$cmd}", __FUNCTION__, __LINE__);
            shell_exec2($cmd);
        }
        $l[] = "";
        return implode("\n", $l);
        return;
    }
    if (!$GLOBALS["CLASS_UNIX"]->process_exists($master_pid)) {
        if (!$GLOBALS["DISABLE_WATCHDOG"]) {
            $cmd = trim("{$GLOBALS["NICE"]} {$GLOBALS["PHP5"]} " . dirname(__FILE__) . "/exec.syncthing.php --start >/dev/null 2>&1 &");
            shell_exec2($cmd);
        }
        return implode("\n", $l);
    } else {
        if ($EnableSyncThing == 0) {
            shell_exec2("{$GLOBALS["KILLBIN"]} -9 {$master_pid} >/dev/null 2>&1");
        }
    }
    $l[] = "running=1";
    $l[] = GetMemoriesOf($master_pid);
    $l[] = "";
    return implode("\n", $l);
}