function SingleInstance_start($nopid = false)
{
    $sock = new sockets();
    $unix = new unix();
    if (!$nopid) {
        $unix = new unix();
        $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . "pid";
        $pid = @file_get_contents($pidfile);
        if ($unix->process_exists($pid)) {
            echo "{$GLOBALS["deflog_start"]} already Artica Starting process exists {$pid}\n";
            return;
        }
        @file_put_contents($pidfile, getmypid());
    }
    $miltergreybin = $unix->find_program("milter-greylist");
    $MilterGreyListEnabled = $sock->GET_INFO("MilterGreyListEnabled");
    if (!is_numeric($MilterGreyListEnabled)) {
        $MilterGreyListEnabled = 0;
    }
    $pid = SingleInstance_pid();
    if ($unix->process_exists($pid)) {
        $timepid = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "{$GLOBALS["deflog_start"]} Service already started {$pid} since {$timepid}Mn...\n";
        }
        return;
    }
    if ($MilterGreyListEnabled == 0) {
        echo "{$GLOBALS["deflog_start"]} is not enabled ( see MilterGreyListEnabled)\n";
        return;
    }
    $dirs[] = "/var/run/milter-greylist";
    $dirs[] = "/var/spool/postfix/var/run/milter-greylist";
    $dirs[] = "/var/milter-greylist";
    $dirs[] = "/var/lib/milter-greylist";
    $dirs[] = "/usr/local/var/milter-greylist/";
    while (list($num, $directory) = each($dirs)) {
        @mkdir($directory, 0755, true);
        @chown($directory, "postfix");
        @chgrp($directory, "postfix");
        @chmod($directory, 0755);
    }
    $FullSocketPath = "/var/run/milter-greylist/milter-greylist.sock";
    $pidpath = "/var/run/milter-greylist/milter-greylist.pid";
    $dbpath = "/var/milter-greylist/greylist.db";
    $confpath = SingleInstanceConfPath();
    $files[] = "/var/milter-greylist/greylist.db";
    $files[] = "/usr/local/var/milter-greylist/greylist.db";
    while (list($num, $filename) = each($files)) {
        if (!is_file($filename)) {
            @touch($filename);
        }
        @chown($filename, "postfix");
        @chgrp($filename, "postfix");
    }
    $MilterGreyListUseTCPPort = $sock->GET_INFO("MilterGreyListUseTCPPort");
    $MilterGeryListTCPPort = $sock->GET_INFO("MilterGeryListTCPPort");
    if (!is_numeric($MilterGeryListTCPPort)) {
        $MilterGeryListTCPPort = 0;
    }
    if (!is_numeric($MilterGreyListUseTCPPort)) {
        $MilterGreyListUseTCPPort = 0;
    }
    if ($MilterGeryListTCPPort == 0) {
        $MilterGreyListUseTCPPort = 0;
    }
    if (!is_file($confpath)) {
        SingleInstance();
    }
    if (!is_file($dbpath)) {
        @touch($dbpath);
    }
    @chown($dbpath, "postfix");
    @chgrp($dbpath, "postfix");
    if ($MilterGreyListUseTCPPort == 1) {
        $FullSocketPath = "inet:{$MilterGeryListTCPPort}";
    }
    $tmpfile = $unix->FILE_TEMP();
    $nohup = $unix->find_program("nohup");
    if ($GLOBALS["OUTPUT"]) {
        echo "{$GLOBALS["deflog_start"]} running daemon {$miltergreybin}\n";
    }
    $cmd = "{$nohup} {$miltergreybin} -u postfix -P {$pidpath} -p {$FullSocketPath} -f {$confpath} -d {$dbpath} >{$tmpfile} 2>&1 &";
    if ($GLOBALS["VERBOSE"]) {
        echo "**** \n {$cmd} \n ************\n";
    }
    shell_exec($cmd);
    $f = explode("\n", @file_get_contents("{$tmpfile}"));
    while (list($num, $ligne) = each($f)) {
        $ligne = trim($ligne);
        if ($ligne == null) {
            continue;
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "{$GLOBALS["deflog_start"]} {$ligne}\n";
        }
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "{$GLOBALS["deflog_start"]} waiting 5s\n";
    }
    for ($i = 1; $i < 6; $i++) {
        if ($GLOBALS["OUTPUT"]) {
            echo "{$GLOBALS["deflog_start"]} waiting {$i}/5\n";
        }
        sleep(1);
        $pid = SingleInstance_pid();
        if ($unix->process_exists($pid)) {
            break;
        }
    }
    $pid = SingleInstance_pid();
    if ($unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "{$GLOBALS["deflog_start"]} Success PID {$pid}\n";
        }
    } else {
        if ($GLOBALS["OUTPUT"]) {
            echo "{$GLOBALS["deflog_start"]} Failed\n";
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "{$cmd}\n";
        }
    }
}
Beispiel #2
0
    if ($GLOBALS["STOP_ONLY"] == 1) {
        MultiplesInstances_stop($GLOBALS["hostname"], $GLOBALS["ou"]);
        die;
    }
    if ($argv[1] == "--database") {
        parse_multi_databases();
        die;
    }
    MultiplesInstances($GLOBALS["hostname"], $GLOBALS["ou"]);
    exit;
}
if ($argv[1] == "--database") {
    parse_database("/var/milter-greylist/greylist.db", "master");
    die;
}
SingleInstance();
function parsecmdlines($argv)
{
    $GLOBALS["COMMANDLINE"] = implode(" ", $argv);
    if (strpos($GLOBALS["COMMANDLINE"], "--verbose") > 0) {
        $GLOBALS["VERBOSE"] = true;
        $GLOBALS["debug"] = true;
        $GLOBALS["DEBUG"] = true;
    }
    while (list($num, $ligne) = each($argv)) {
        if (preg_match("#--verbose#", $ligne)) {
            $GLOBALS["DEBUG"] = true;
            $GLOBALS["VERBOSE"] = true;
            ini_set('display_errors', 1);
            ini_set('error_reporting', E_ALL);
            ini_set('error_prepend_string', null);