From f3c0d31688d2f079d0b5cb1926eb5a67c5ecce67 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sat, 20 Dec 2008 19:46:07 -0500 Subject: [PATCH] reformat lib/daemon.php for phpcs darcs-hash:20081221004607-84dde-d83d0661b29e532b8842b7ae45c961446669fb74.gz --- _darcs/inventory | 4 +- ...83d0661b29e532b8842b7ae45c961446669fb74.gz | Bin 0 -> 1238 bytes _darcs/pristine/lib/daemon.php | 239 +++++------ _darcs/tentative_pristine | 384 +++++++++--------- lib/daemon.php | 239 +++++------ 5 files changed, 443 insertions(+), 423 deletions(-) create mode 100644 _darcs/patches/20081221004607-84dde-d83d0661b29e532b8842b7ae45c961446669fb74.gz diff --git a/_darcs/inventory b/_darcs/inventory index 0328b73d10..0ee79eeaa2 100644 --- a/_darcs/inventory +++ b/_darcs/inventory @@ -25,4 +25,6 @@ Evan Prodromou **20081221002332] [reformatting for phpcs in lib/util.php Evan Prodromou **20081221003016] [some modifications to assuage phpcs -Evan Prodromou **20081221003955] \ No newline at end of file +Evan Prodromou **20081221003955] +[reformat lib/daemon.php for phpcs +Evan Prodromou **20081221004607] \ No newline at end of file diff --git a/_darcs/patches/20081221004607-84dde-d83d0661b29e532b8842b7ae45c961446669fb74.gz b/_darcs/patches/20081221004607-84dde-d83d0661b29e532b8842b7ae45c961446669fb74.gz new file mode 100644 index 0000000000000000000000000000000000000000..9e51eb2f73b8ca873f7bf1b86ec59443af718d96 GIT binary patch literal 1238 zcmV;{1S$I;iwFP!000001GQLbZ`(E){%rjUCJP|hj%~%w(JfuDAWMq?fm38C_8}__ zRibUdGG$QG(xUKx-%C*kNs;n_Oh68i@5SeN585vj&tkgrQWS;XMuCS{F&VBGYXqwZ z{`!e^|F1{TXBr1IUd0*ufZ_d*b@@Id-U{FJdSlmpe>NVUx$gO;d-WClv=$jzqTxuf zJGKU+p4A`qdL{wa8iX_C90hn55*#@8`KCSF8S&E)1lWO{E?+G6*h6ueQP|C*=$fyZT)sQA z1|7v5SQ>OXg^kIV$H!}U-@uP3`pxr~a~fwPC>2iDVSs)|YoDai6ewFdU8dM!z~p-$ zkjp^(1z}2K%CugNMn-C-CrB=KDBFW`NI++zAdUH#tpiSM~cs1x`|u5v;ZhID+E>$TrVg5S3IbXjRv_Gmfw>U=DSy z)SlAk7h;}z5yf8c<2fTFBy*>#Mx%h1u2~piRvbXvNxBG=!3`{aEEXAkhERSj3uCJR ziT@)}r7H&0Mx!B!i+5!eHq7eFiv6lonD`WkOVY%V)Z6t>{FtPWI|&>(H!;WQv|N)7 zRZU_YC*k+$GK?Z8rQyn{Y;;du)m>eEMFyE;Q9IWw4O9G?qc8TZtrUr{?1iLA8pC3p z8L&7-90LFgRw1|{I{t_7&a_cdq?MT6@}*Akeb|!1XyFXG#!%FPE;-yIY*V&ho{L=j z5%%a|T({OEm}!PsFs{U(ktig~X3DE+DFskR8iW*rESfHWI}7KI&ChF}BAZLtbrBsE zN=j0eW4x$N`Rqc1a}%1ZmJJk(Wq?Cuk8JqO?ZVp-Hf$kl4aN7Cz4r3GMEt@dbNnU2 z)RB_#&AaQ>I0rNXPess1u9%E1_mN7kL_1%YhGZ7A8$#6#UO`3><~J37mP(;Cq277^ zzI~oNOg=#X4|5Vz7;=!4OA^0AZh~%pyXHauhH?@WH&TZ+U8r!H=7yQUG)Bi7I~{(U z1n`C4R>%Ef!@K>p^u}{cy~-ZiH(Ptf)kV%7yQr{Xf-efdg*zd6nMtHnhE zYn1~S%$1uIs-E`Ykee(?R4vsk&KZk!@GthfNn^9k(TqG1ziM$gjJ(R#i&%+0*jFnRuCZl4#4I;Dc9fzg? zD>2!cQ0+5+`pSn*aRx~z+I1L7N}8!8?X$=5$D16E zlp+F?`%NR2OKBp>WbQngG<3={>VM*MhgtR}jG70WnN#DiYavzQwmASBMZ9GeZvPf$ z&!M>OyBTd{>ZFu&Og}PpGV<)yNfbj=tE^1ZolrM_Pbz#I-B=szKh$P^e25kR084vT AzyJUM literal 0 HcmV?d00001 diff --git a/_darcs/pristine/lib/daemon.php b/_darcs/pristine/lib/daemon.php index 359a4343b5..9c1ae50a02 100644 --- a/_darcs/pristine/lib/daemon.php +++ b/_darcs/pristine/lib/daemon.php @@ -1,5 +1,5 @@ . */ -if (!defined('LACONICA')) { exit(1); } - -class Daemon { - - function name() { - return NULL; - } - - function background() { - $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; - } - } - - function alreadyRunning() { - - $pidfilename = $this->pidFilename(); - - if (!$pidfilename) { - return false; - } - - if (!file_exists($pidfilename)) { - return false; - } - $contents = file_get_contents($pidfilename); - if (posix_kill(trim($contents),0)) { - return true; - } else { - return false; - } - } - - function writePidFile() { - $pidfilename = $this->pidFilename(); - - if (!$pidfilename) { - return false; - } - - return file_put_contents($pidfilename, posix_getpid() . "\n"); - } - - 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']); - } - } - } - - function runOnce() { - if ($this->alreadyRunning()) { - common_log(LOG_INFO, $this->name() . ' already running. Exiting.'); - exit(0); - } - if ($this->background()) { - $this->writePidFile(); - $this->changeUser(); - $this->run(); - $this->clearPidFile(); - } - } - - function run() { - return true; - } +if (!defined('LACONICA')) { + exit(1); +} + +class Daemon +{ + function name() + { + return null; + } + + function background() + { + $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; + } + } + + function alreadyRunning() + { + $pidfilename = $this->pidFilename(); + + if (!$pidfilename) { + return false; + } + + if (!file_exists($pidfilename)) { + return false; + } + $contents = file_get_contents($pidfilename); + if (posix_kill(trim($contents), 0)) { + return true; + } else { + return false; + } + } + + function writePidFile() + { + $pidfilename = $this->pidFilename(); + + if (!$pidfilename) { + return false; + } + + return file_put_contents($pidfilename, posix_getpid() . "\n"); + } + + 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']); + } + } + } + + function runOnce() + { + if ($this->alreadyRunning()) { + common_log(LOG_INFO, $this->name() . ' already running. Exiting.'); + exit(0); + } + if ($this->background()) { + $this->writePidFile(); + $this->changeUser(); + $this->run(); + $this->clearPidFile(); + } + } + + function run() + { + return true; + } } diff --git a/_darcs/tentative_pristine b/_darcs/tentative_pristine index 529be5f2c5..73c44f1dc6 100644 --- a/_darcs/tentative_pristine +++ b/_darcs/tentative_pristine @@ -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); +} -hunk ./lib/action.php 24 --class Action { // lawsuit -+class Action // lawsuit +hunk ./lib/daemon.php 24 +-class Daemon { ++class Daemon +{ -hunk ./lib/action.php 27 -- 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() ++ function name() + { + return null; + } -hunk ./lib/action.php 49 -- function etag() { +hunk ./lib/daemon.php 31 +- function name() { - return NULL; - } -+ function etag() -+ { -+ return null; -+ } -hunk ./lib/action.php 54 -- function is_readonly() { -- return false; +- +- function background() { +- $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; +- } - } -+ 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 -- function arg($key, $def=NULL) { -- if (array_key_exists($key, $this->args)) { -- return $this->args[$key]; +hunk ./lib/daemon.php 45 +- function alreadyRunning() { ++ function alreadyRunning() ++ { ++ $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 { -- return $def; +- return false; - } - } -+ function arg($key, $def=null) -+ { -+ if (array_key_exists($key, $this->args)) { -+ return $this->args[$key]; +- +- function writePidFile() { +- $pidfilename = $this->pidFilename(); +- +- 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 { -+ return $def; ++ return false; + } + } -hunk ./lib/action.php 68 -- function trimmed($key, $def=NULL) { -- $arg = $this->arg($key, $def); -- return (is_string($arg)) ? trim($arg) : $arg; +hunk ./lib/daemon.php 64 +- function clearPidFile() { +- $pidfilename = $this->pidFilename(); +- if (!$pidfilename) { +- return false; +- } +- return unlink($pidfilename); - } -+ 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); +- +- function pidFilename() { +- $piddir = common_config('daemon', 'piddir'); +- if (!$piddir) { +- return NULL; - } -+ if ($etag) { -+ header('ETag: ' . $etag); -+ } -hunk ./lib/action.php 86 -- if ($lm) { -- header('Last-Modified: ' . date(DATE_RFC1123, $lm)); -- $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE']; -- if ($if_modified_since) { -- $ims = strtotime($if_modified_since); -- if ($lm <= $ims) { -- if (!$etag || $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) { -- header('HTTP/1.1 304 Not Modified'); -- # Better way to do this? -- exit(0); -- } -- } -- } +- $name = $this->name(); +- if (!$name) { +- return NULL; - } +- return $piddir . '/' . $name . '.pid'; - } -+ if ($lm) { -+ header('Last-Modified: ' . date(DATE_RFC1123, $lm)); -+ $if_modified_since = $_SERVER['HTTP_IF_MODIFIED_SINCE']; -+ if ($if_modified_since) { -+ $ims = strtotime($if_modified_since); -+ if ($lm <= $ims) { -+ if (!$etag || -+ $this->_has_etag($etag, $_SERVER['HTTP_IF_NONE_MATCH'])) { -+ header('HTTP/1.1 304 Not Modified'); -+ // Better way to do this? -+ exit(0); -+ } -+ } -+ } ++ function writePidFile() ++ { ++ $pidfilename = $this->pidFilename(); +hunk ./lib/daemon.php 68 +- function changeUser() { ++ if (!$pidfilename) { ++ return false; + } +hunk ./lib/daemon.php 72 +- $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']); +- } +- } ++ return file_put_contents($pidfilename, posix_getpid() . "\n"); + } -hunk ./lib/action.php 103 -- function _has_etag($etag, $if_none_match) { -- return ($if_none_match) && in_array($etag, explode(',', $if_none_match)); +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']); +- } +- } - } -+ function _has_etag($etag, $if_none_match) -+ { -+ return ($if_none_match) && in_array($etag, explode(',', $if_none_match)); -+ } -hunk ./lib/action.php 108 -- function boolean($key, $def=false) { -- $arg = strtolower($this->trimmed($key)); -+ function boolean($key, $def=false) -+ { -+ $arg = strtolower($this->trimmed($key)); -hunk ./lib/action.php 112 -- 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; +- +- function runOnce() { +- if ($this->alreadyRunning()) { +- common_log(LOG_INFO, $this->name() . ' already running. Exiting.'); +- exit(0); +- } +- if ($this->background()) { +- $this->writePidFile(); +- $this->changeUser(); +- $this->run(); +- $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'))) { +- +- function run() { +- return true; +- } ++ function clearPidFile() ++ { ++ $pidfilename = $this->pidFilename(); ++ if (!$pidfilename) { + return false; -+ } else { -+ return $def; + } ++ return unlink($pidfilename); + } -hunk ./lib/action.php 123 -- function server_error($msg, $code=500) { -- $action = $this->trimmed('action'); -- common_debug("Server error '$code' on '$action': $msg", __FILE__); -- common_server_error($msg, $code); -- } -+ function server_error($msg, $code=500) ++ ++ function pidFilename() + { -+ $action = $this->trimmed('action'); -+ common_debug("Server error '$code' on '$action': $msg", __FILE__); -+ common_server_error($msg, $code); ++ $piddir = common_config('daemon', 'piddir'); ++ if (!$piddir) { ++ return null; ++ } ++ $name = $this->name(); ++ if (!$name) { ++ return null; ++ } ++ return $piddir . '/' . $name . '.pid'; + } -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) ++ ++ function changeUser() + { -+ $action = $this->trimmed('action'); -+ common_debug("User error '$code' on '$action': $msg", __FILE__); -+ common_user_error($msg, $code); ++ $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 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() ++ ++ function runOnce() + { -+ $action = $this->trimmed('action'); -+ $args = $this->args; -+ unset($args['action']); -+ foreach (array_keys($_COOKIE) as $cookie) { -+ unset($args[$cookie]); ++ if ($this->alreadyRunning()) { ++ common_log(LOG_INFO, $this->name() . ' already running. Exiting.'); ++ exit(0); ++ } ++ if ($this->background()) { ++ $this->writePidFile(); ++ $this->changeUser(); ++ $this->run(); ++ $this->clearPidFile(); + } -+ return common_local_url($action, $args); + } -hunk ./lib/action.php 148 -- function nav_menu($menu) { -+ function nav_menu($menu) ++ ++ function run() + { -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 -- } ++ return true; + } diff --git a/lib/daemon.php b/lib/daemon.php index 359a4343b5..9c1ae50a02 100644 --- a/lib/daemon.php +++ b/lib/daemon.php @@ -1,5 +1,5 @@ . */ -if (!defined('LACONICA')) { exit(1); } - -class Daemon { - - function name() { - return NULL; - } - - function background() { - $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; - } - } - - function alreadyRunning() { - - $pidfilename = $this->pidFilename(); - - if (!$pidfilename) { - return false; - } - - if (!file_exists($pidfilename)) { - return false; - } - $contents = file_get_contents($pidfilename); - if (posix_kill(trim($contents),0)) { - return true; - } else { - return false; - } - } - - function writePidFile() { - $pidfilename = $this->pidFilename(); - - if (!$pidfilename) { - return false; - } - - return file_put_contents($pidfilename, posix_getpid() . "\n"); - } - - 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']); - } - } - } - - function runOnce() { - if ($this->alreadyRunning()) { - common_log(LOG_INFO, $this->name() . ' already running. Exiting.'); - exit(0); - } - if ($this->background()) { - $this->writePidFile(); - $this->changeUser(); - $this->run(); - $this->clearPidFile(); - } - } - - function run() { - return true; - } +if (!defined('LACONICA')) { + exit(1); +} + +class Daemon +{ + function name() + { + return null; + } + + function background() + { + $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; + } + } + + function alreadyRunning() + { + $pidfilename = $this->pidFilename(); + + if (!$pidfilename) { + return false; + } + + if (!file_exists($pidfilename)) { + return false; + } + $contents = file_get_contents($pidfilename); + if (posix_kill(trim($contents), 0)) { + return true; + } else { + return false; + } + } + + function writePidFile() + { + $pidfilename = $this->pidFilename(); + + if (!$pidfilename) { + return false; + } + + return file_put_contents($pidfilename, posix_getpid() . "\n"); + } + + 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']); + } + } + } + + function runOnce() + { + if ($this->alreadyRunning()) { + common_log(LOG_INFO, $this->name() . ' already running. Exiting.'); + exit(0); + } + if ($this->background()) { + $this->writePidFile(); + $this->changeUser(); + $this->run(); + $this->clearPidFile(); + } + } + + function run() + { + return true; + } } -- 2.39.5