reformat lib/daemon.php for phpcs

darcs-hash:20081221004607-84dde-d83d0661b29e532b8842b7ae45c961446669fb74.gz
This commit is contained in:
Evan Prodromou 2008-12-20 19:46:07 -05:00
parent 01a548a9fb
commit f3c0d31688
5 changed files with 451 additions and 431 deletions

View File

@ -26,3 +26,5 @@ Evan Prodromou <evan@prodromou.name>**20081221002332]
Evan Prodromou <evan@prodromou.name>**20081221003016] Evan Prodromou <evan@prodromou.name>**20081221003016]
[some modifications to assuage phpcs [some modifications to assuage phpcs
Evan Prodromou <evan@prodromou.name>**20081221003955] Evan Prodromou <evan@prodromou.name>**20081221003955]
[reformat lib/daemon.php for phpcs
Evan Prodromou <evan@prodromou.name>**20081221004607]

View File

@ -1,5 +1,5 @@
<?php <?php
/* /**
* Laconica - a distributed open-source microblogging tool * Laconica - a distributed open-source microblogging tool
* Copyright (C) 2008, Controlez-Vous, Inc. * Copyright (C) 2008, Controlez-Vous, Inc.
* *
@ -17,117 +17,128 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!defined('LACONICA')) { exit(1); } if (!defined('LACONICA')) {
exit(1);
class Daemon { }
function name() { class Daemon
return NULL; {
} function name()
{
function background() { return null;
$pid = pcntl_fork(); }
if ($pid < 0) { # error
common_log(LOG_ERR, "Could not fork."); function background()
return false; {
} else if ($pid > 0) { # parent $pid = pcntl_fork();
common_log(LOG_INFO, "Successfully forked."); if ($pid < 0) { // error
exit(0); common_log(LOG_ERR, "Could not fork.");
} else { # child return false;
return true; } else if ($pid > 0) { // parent
} common_log(LOG_INFO, "Successfully forked.");
} exit(0);
} else { // child
function alreadyRunning() { return true;
}
$pidfilename = $this->pidFilename(); }
if (!$pidfilename) { function alreadyRunning()
return false; {
} $pidfilename = $this->pidFilename();
if (!file_exists($pidfilename)) { if (!$pidfilename) {
return false; return false;
} }
$contents = file_get_contents($pidfilename);
if (posix_kill(trim($contents),0)) { if (!file_exists($pidfilename)) {
return true; return false;
} else { }
return false; $contents = file_get_contents($pidfilename);
} if (posix_kill(trim($contents), 0)) {
} return true;
} else {
function writePidFile() { return false;
$pidfilename = $this->pidFilename(); }
}
if (!$pidfilename) {
return false; function writePidFile()
} {
$pidfilename = $this->pidFilename();
return file_put_contents($pidfilename, posix_getpid() . "\n");
} if (!$pidfilename) {
return false;
function clearPidFile() { }
$pidfilename = $this->pidFilename();
if (!$pidfilename) { return file_put_contents($pidfilename, posix_getpid() . "\n");
return false; }
}
return unlink($pidfilename); function clearPidFile()
} {
$pidfilename = $this->pidFilename();
function pidFilename() { if (!$pidfilename) {
$piddir = common_config('daemon', 'piddir'); return false;
if (!$piddir) { }
return NULL; return unlink($pidfilename);
} }
$name = $this->name();
if (!$name) { function pidFilename()
return NULL; {
} $piddir = common_config('daemon', 'piddir');
return $piddir . '/' . $name . '.pid'; if (!$piddir) {
} return null;
}
function changeUser() { $name = $this->name();
if (!$name) {
$username = common_config('daemon', 'user'); return null;
}
if ($username) { return $piddir . '/' . $name . '.pid';
$user_info = posix_getpwnam($username); }
if (!$user_info) {
common_log(LOG_WARNING, 'Ignoring unknown user for daemon: ' . $username); function changeUser()
} else { {
common_log(LOG_INFO, "Setting user to " . $username); $username = common_config('daemon', 'user');
posix_setuid($user_info['uid']);
} if ($username) {
} $user_info = posix_getpwnam($username);
if (!$user_info) {
$groupname = common_config('daemon', 'group'); common_log(LOG_WARNING,
'Ignoring unknown user for daemon: ' . $username);
if ($groupname) { } else {
$group_info = posix_getgrnam($groupname); common_log(LOG_INFO, "Setting user to " . $username);
if (!$group_info) { posix_setuid($user_info['uid']);
common_log(LOG_WARNING, 'Ignoring unknown group for daemon: ' . $groupname); }
} else { }
common_log(LOG_INFO, "Setting group to " . $groupname);
posix_setgid($group_info['gid']); $groupname = common_config('daemon', 'group');
}
} if ($groupname) {
} $group_info = posix_getgrnam($groupname);
if (!$group_info) {
function runOnce() { common_log(LOG_WARNING,
if ($this->alreadyRunning()) { 'Ignoring unknown group for daemon: ' . $groupname);
common_log(LOG_INFO, $this->name() . ' already running. Exiting.'); } else {
exit(0); common_log(LOG_INFO, "Setting group to " . $groupname);
} posix_setgid($group_info['gid']);
if ($this->background()) { }
$this->writePidFile(); }
$this->changeUser(); }
$this->run();
$this->clearPidFile(); function runOnce()
} {
} if ($this->alreadyRunning()) {
common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
function run() { exit(0);
return true; }
} if ($this->background()) {
$this->writePidFile();
$this->changeUser();
$this->run();
$this->clearPidFile();
}
}
function run()
{
return true;
}
} }

View File

@ -1,236 +1,232 @@
hunk ./lib/action.php 2 hunk ./lib/daemon.php 2
-/* -/*
+/** +/**
hunk ./lib/action.php 20 hunk ./lib/daemon.php 20
-if (!defined('LACONICA')) { exit(1); } -if (!defined('LACONICA')) { exit(1); }
+if (!defined('LACONICA')) { +if (!defined('LACONICA')) {
+ exit(1); + exit(1);
+} +}
hunk ./lib/action.php 24 hunk ./lib/daemon.php 24
-class Action { // lawsuit -class Daemon {
+class Action // lawsuit +class Daemon
+{ +{
hunk ./lib/action.php 27 + function name()
- var $args;
+ var $args;
hunk ./lib/action.php 29
- function Action() {
- }
+ function Action()
+ {
+ }
hunk ./lib/action.php 33
- # For initializing members of the class
+ // For initializing members of the class
hunk ./lib/action.php 35
- function prepare($argarray) {
- $this->args =& common_copy_args($argarray);
- return true;
- }
+ function prepare($argarray)
+ {
+ $this->args =& common_copy_args($argarray);
+ return true;
+ }
hunk ./lib/action.php 41
- # For comparison with If-Last-Modified
- # If not applicable, return NULL
+ // For comparison with If-Last-Modified
+ // If not applicable, return null
hunk ./lib/action.php 44
- function last_modified() {
- return NULL;
- }
+ function last_modified()
+ { + {
+ return null; + return null;
+ } + }
hunk ./lib/action.php 49 hunk ./lib/daemon.php 31
- function etag() { - function name() {
- return NULL; - return NULL;
- } - }
+ function etag() -
+ { - function background() {
+ return null; - $pid = pcntl_fork();
+ } - if ($pid < 0) { # error
hunk ./lib/action.php 54 - common_log(LOG_ERR, "Could not fork.");
- function is_readonly() { - return false;
- return false; - } else if ($pid > 0) { # parent
- common_log(LOG_INFO, "Successfully forked.");
- exit(0);
- } else { # child
- return true;
- }
- } - }
+ function is_readonly() + function background()
+ { + {
+ return false; + $pid = pcntl_fork();
+ if ($pid < 0) { // error
+ common_log(LOG_ERR, "Could not fork.");
+ return false;
+ } else if ($pid > 0) { // parent
+ common_log(LOG_INFO, "Successfully forked.");
+ exit(0);
+ } else { // child
+ return true;
+ }
+ } + }
hunk ./lib/action.php 59 hunk ./lib/daemon.php 45
- function arg($key, $def=NULL) { - function alreadyRunning() {
- if (array_key_exists($key, $this->args)) { + function alreadyRunning()
- return $this->args[$key]; + {
+ $pidfilename = $this->pidFilename();
hunk ./lib/daemon.php 49
- $pidfilename = $this->pidFilename();
+ if (!$pidfilename) {
+ return false;
+ }
hunk ./lib/daemon.php 53
- if (!$pidfilename) {
- return false;
- }
-
- if (!file_exists($pidfilename)) {
- return false;
- }
- $contents = file_get_contents($pidfilename);
- if (posix_kill(trim($contents),0)) {
- return true;
- } else { - } else {
- return $def; - return false;
- } - }
- } - }
+ function arg($key, $def=null) -
+ { - function writePidFile() {
+ if (array_key_exists($key, $this->args)) { - $pidfilename = $this->pidFilename();
+ return $this->args[$key]; -
- if (!$pidfilename) {
- return false;
- }
-
- return file_put_contents($pidfilename, posix_getpid() . "\n");
- }
+ if (!file_exists($pidfilename)) {
+ return false;
+ }
+ $contents = file_get_contents($pidfilename);
+ if (posix_kill(trim($contents), 0)) {
+ return true;
+ } else { + } else {
+ return $def; + return false;
+ } + }
+ } + }
hunk ./lib/action.php 68 hunk ./lib/daemon.php 64
- function trimmed($key, $def=NULL) { - function clearPidFile() {
- $arg = $this->arg($key, $def); - $pidfilename = $this->pidFilename();
- return (is_string($arg)) ? trim($arg) : $arg; - if (!$pidfilename) {
- } - return false;
+ function trimmed($key, $def=null)
+ {
+ $arg = $this->arg($key, $def);
+ return (is_string($arg)) ? trim($arg) : $arg;
+ }
hunk ./lib/action.php 74
- # Note: argarray ignored, since it's now passed in in prepare()
+ // Note: argarray ignored, since it's now passed in in prepare()
hunk ./lib/action.php 76
- function handle($argarray=NULL) {
+ function handle($argarray=null)
+ {
hunk ./lib/action.php 79
- $lm = $this->last_modified();
- $etag = $this->etag();
+ $lm = $this->last_modified();
+ $etag = $this->etag();
hunk ./lib/action.php 82
- if ($etag) {
- header('ETag: ' . $etag);
- } - }
+ if ($etag) { - return unlink($pidfilename);
+ header('ETag: ' . $etag); - }
-
- function pidFilename() {
- $piddir = common_config('daemon', 'piddir');
- if (!$piddir) {
- return NULL;
- }
- $name = $this->name();
- if (!$name) {
- return NULL;
- }
- return $piddir . '/' . $name . '.pid';
- }
+ function writePidFile()
+ {
+ $pidfilename = $this->pidFilename();
hunk ./lib/daemon.php 68
- function changeUser() {
+ if (!$pidfilename) {
+ return false;
+ } + }
hunk ./lib/action.php 86 hunk ./lib/daemon.php 72
- if ($lm) { - $username = common_config('daemon', 'user');
- header('Last-Modified: ' . date(DATE_RFC1123, $lm)); -
- $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE']; - if ($username) {
- if ($if_modified_since) { - $user_info = posix_getpwnam($username);
- $ims = strtotime($if_modified_since); - if (!$user_info) {
- if ($lm <= $ims) { - common_log(LOG_WARNING, 'Ignoring unknown user for daemon: ' . $username);
- if (!$etag || $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) { - } else {
- header('HTTP/1.1 304 Not Modified'); - common_log(LOG_INFO, "Setting user to " . $username);
- # Better way to do this? - posix_setuid($user_info['uid']);
- exit(0); - }
- } - }
- } + return file_put_contents($pidfilename, posix_getpid() . "\n");
+ }
hunk ./lib/daemon.php 75
- $groupname = common_config('daemon', 'group');
-
- if ($groupname) {
- $group_info = posix_getgrnam($groupname);
- if (!$group_info) {
- common_log(LOG_WARNING, 'Ignoring unknown group for daemon: ' . $groupname);
- } else {
- common_log(LOG_INFO, "Setting group to " . $groupname);
- posix_setgid($group_info['gid']);
- } - }
- } - }
- } - }
+ if ($lm) { -
+ header('Last-Modified: ' . date(DATE_RFC1123, $lm)); - function runOnce() {
+ $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE']; - if ($this->alreadyRunning()) {
+ if ($if_modified_since) { - common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
+ $ims = strtotime($if_modified_since); - exit(0);
+ if ($lm <= $ims) { - }
+ if (!$etag || - if ($this->background()) {
+ $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) { - $this->writePidFile();
+ header('HTTP/1.1 304 Not Modified'); - $this->changeUser();
+ // Better way to do this? - $this->run();
+ exit(0); - $this->clearPidFile();
+ } - }
+ } - }
-
- function run() {
- return true;
- }
+ function clearPidFile()
+ {
+ $pidfilename = $this->pidFilename();
+ if (!$pidfilename) {
+ return false;
+ }
+ return unlink($pidfilename);
+ }
+
+ function pidFilename()
+ {
+ $piddir = common_config('daemon', 'piddir');
+ if (!$piddir) {
+ return null;
+ }
+ $name = $this->name();
+ if (!$name) {
+ return null;
+ }
+ return $piddir . '/' . $name . '.pid';
+ }
+
+ function changeUser()
+ {
+ $username = common_config('daemon', 'user');
+
+ if ($username) {
+ $user_info = posix_getpwnam($username);
+ if (!$user_info) {
+ common_log(LOG_WARNING,
+ 'Ignoring unknown user for daemon: ' . $username);
+ } else {
+ common_log(LOG_INFO, "Setting user to " . $username);
+ posix_setuid($user_info['uid']);
+ }
+ }
+
+ $groupname = common_config('daemon', 'group');
+
+ if ($groupname) {
+ $group_info = posix_getgrnam($groupname);
+ if (!$group_info) {
+ common_log(LOG_WARNING,
+ 'Ignoring unknown group for daemon: ' . $groupname);
+ } else {
+ common_log(LOG_INFO, "Setting group to " . $groupname);
+ posix_setgid($group_info['gid']);
+ } + }
+ } + }
+ } + }
hunk ./lib/action.php 103 +
- function _has_etag($etag, $if_none_match) { + function runOnce()
- return ($if_none_match) && in_array($etag, explode(',', $if_none_match));
- }
+ function _has_etag($etag, $if_none_match)
+ { + {
+ return ($if_none_match) && in_array($etag, explode(',', $if_none_match)); + if ($this->alreadyRunning()) {
+ } + common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
hunk ./lib/action.php 108 + exit(0);
- function boolean($key, $def=false) { + }
- $arg = strtolower($this->trimmed($key)); + if ($this->background()) {
+ function boolean($key, $def=false) + $this->writePidFile();
+ { + $this->changeUser();
+ $arg = strtolower($this->trimmed($key)); + $this->run();
hunk ./lib/action.php 112 + $this->clearPidFile();
- if (is_null($arg)) {
- return $def;
- } else if (in_array($arg, array('true', 'yes', '1'))) {
- return true;
- } else if (in_array($arg, array('false', 'no', '0'))) {
- return false;
- } else {
- return $def;
- }
- }
+ if (is_null($arg)) {
+ return $def;
+ } else if (in_array($arg, array('true', 'yes', '1'))) {
+ return true;
+ } else if (in_array($arg, array('false', 'no', '0'))) {
+ return false;
+ } else {
+ return $def;
+ } + }
+ } + }
hunk ./lib/action.php 123 +
- function server_error($msg, $code=500) { + function run()
- $action = $this->trimmed('action');
- common_debug("Server error '$code' on '$action': $msg", __FILE__);
- common_server_error($msg, $code);
- }
+ function server_error($msg, $code=500)
+ { + {
+ $action = $this->trimmed('action'); + return true;
+ common_debug("Server error '$code' on '$action': $msg", __FILE__);
+ common_server_error($msg, $code);
+ }
hunk ./lib/action.php 130
- function client_error($msg, $code=400) {
- $action = $this->trimmed('action');
- common_debug("User error '$code' on '$action': $msg", __FILE__);
- common_user_error($msg, $code);
- }
+ function client_error($msg, $code=400)
+ {
+ $action = $this->trimmed('action');
+ common_debug("User error '$code' on '$action': $msg", __FILE__);
+ common_user_error($msg, $code);
+ }
hunk ./lib/action.php 137
- function self_url() {
- $action = $this->trimmed('action');
- $args = $this->args;
- unset($args['action']);
- foreach (array_keys($_COOKIE) as $cookie) {
- unset($args[$cookie]);
- }
- return common_local_url($action, $args);
- }
+ function self_url()
+ {
+ $action = $this->trimmed('action');
+ $args = $this->args;
+ unset($args['action']);
+ foreach (array_keys($_COOKIE) as $cookie) {
+ unset($args[$cookie]);
+ }
+ return common_local_url($action, $args);
+ }
hunk ./lib/action.php 148
- function nav_menu($menu) {
+ function nav_menu($menu)
+ {
hunk ./lib/action.php 153
- common_menu_item(common_local_url($menuaction, isset($menudesc[2]) ? $menudesc[2] : NULL),
- $menudesc[0],
- $menudesc[1],
- $action == $menuaction);
+ common_menu_item(common_local_url($menuaction,
+ isset($menudesc[2]) ? $menudesc[2] : null),
+ $menudesc[0],
+ $menudesc[1],
+ $action == $menuaction);
hunk ./lib/action.php 160
- }
+ } + }

View File

@ -1,5 +1,5 @@
<?php <?php
/* /**
* Laconica - a distributed open-source microblogging tool * Laconica - a distributed open-source microblogging tool
* Copyright (C) 2008, Controlez-Vous, Inc. * Copyright (C) 2008, Controlez-Vous, Inc.
* *
@ -17,117 +17,128 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!defined('LACONICA')) { exit(1); } if (!defined('LACONICA')) {
exit(1);
class Daemon { }
function name() { class Daemon
return NULL; {
} function name()
{
function background() { return null;
$pid = pcntl_fork(); }
if ($pid < 0) { # error
common_log(LOG_ERR, "Could not fork."); function background()
return false; {
} else if ($pid > 0) { # parent $pid = pcntl_fork();
common_log(LOG_INFO, "Successfully forked."); if ($pid < 0) { // error
exit(0); common_log(LOG_ERR, "Could not fork.");
} else { # child return false;
return true; } else if ($pid > 0) { // parent
} common_log(LOG_INFO, "Successfully forked.");
} exit(0);
} else { // child
function alreadyRunning() { return true;
}
$pidfilename = $this->pidFilename(); }
if (!$pidfilename) { function alreadyRunning()
return false; {
} $pidfilename = $this->pidFilename();
if (!file_exists($pidfilename)) { if (!$pidfilename) {
return false; return false;
} }
$contents = file_get_contents($pidfilename);
if (posix_kill(trim($contents),0)) { if (!file_exists($pidfilename)) {
return true; return false;
} else { }
return false; $contents = file_get_contents($pidfilename);
} if (posix_kill(trim($contents), 0)) {
} return true;
} else {
function writePidFile() { return false;
$pidfilename = $this->pidFilename(); }
}
if (!$pidfilename) {
return false; function writePidFile()
} {
$pidfilename = $this->pidFilename();
return file_put_contents($pidfilename, posix_getpid() . "\n");
} if (!$pidfilename) {
return false;
function clearPidFile() { }
$pidfilename = $this->pidFilename();
if (!$pidfilename) { return file_put_contents($pidfilename, posix_getpid() . "\n");
return false; }
}
return unlink($pidfilename); function clearPidFile()
} {
$pidfilename = $this->pidFilename();
function pidFilename() { if (!$pidfilename) {
$piddir = common_config('daemon', 'piddir'); return false;
if (!$piddir) { }
return NULL; return unlink($pidfilename);
} }
$name = $this->name();
if (!$name) { function pidFilename()
return NULL; {
} $piddir = common_config('daemon', 'piddir');
return $piddir . '/' . $name . '.pid'; if (!$piddir) {
} return null;
}
function changeUser() { $name = $this->name();
if (!$name) {
$username = common_config('daemon', 'user'); return null;
}
if ($username) { return $piddir . '/' . $name . '.pid';
$user_info = posix_getpwnam($username); }
if (!$user_info) {
common_log(LOG_WARNING, 'Ignoring unknown user for daemon: ' . $username); function changeUser()
} else { {
common_log(LOG_INFO, "Setting user to " . $username); $username = common_config('daemon', 'user');
posix_setuid($user_info['uid']);
} if ($username) {
} $user_info = posix_getpwnam($username);
if (!$user_info) {
$groupname = common_config('daemon', 'group'); common_log(LOG_WARNING,
'Ignoring unknown user for daemon: ' . $username);
if ($groupname) { } else {
$group_info = posix_getgrnam($groupname); common_log(LOG_INFO, "Setting user to " . $username);
if (!$group_info) { posix_setuid($user_info['uid']);
common_log(LOG_WARNING, 'Ignoring unknown group for daemon: ' . $groupname); }
} else { }
common_log(LOG_INFO, "Setting group to " . $groupname);
posix_setgid($group_info['gid']); $groupname = common_config('daemon', 'group');
}
} if ($groupname) {
} $group_info = posix_getgrnam($groupname);
if (!$group_info) {
function runOnce() { common_log(LOG_WARNING,
if ($this->alreadyRunning()) { 'Ignoring unknown group for daemon: ' . $groupname);
common_log(LOG_INFO, $this->name() . ' already running. Exiting.'); } else {
exit(0); common_log(LOG_INFO, "Setting group to " . $groupname);
} posix_setgid($group_info['gid']);
if ($this->background()) { }
$this->writePidFile(); }
$this->changeUser(); }
$this->run();
$this->clearPidFile(); function runOnce()
} {
} if ($this->alreadyRunning()) {
common_log(LOG_INFO, $this->name() . ' already running. Exiting.');
function run() { exit(0);
return true; }
} if ($this->background()) {
$this->writePidFile();
$this->changeUser();
$this->run();
$this->clearPidFile();
}
}
function run()
{
return true;
}
} }