Skip to content
Browse files

Docs, refactoring, bugfixes

  • Loading branch information...
1 parent 8f923c3 commit d1498a8aceb47ca23336035e3d633c6cbe37837e @kakserpom committed Mar 21, 2013
Showing with 64 additions and 18 deletions.
  1. +10 −2 lib/BoundSocket.php
  2. +13 −1 lib/BoundTCPSocket.php
  3. +38 −12 lib/BoundUNIXSocket.php
  4. +1 −1 lib/DNSClient.php
  5. +2 −2 lib/IOStream.php
View
12 lib/BoundSocket.php
@@ -250,7 +250,7 @@ public function enable() {
-1,
$this->fd
);
- if ($this->ev) {
+ if ($this->ev and is_callable([$this->ev, 'getSocketName'])) {
$this->ev->getSocketName($this->locHost, $this->locPort);
}
} else {
@@ -302,7 +302,7 @@ public function disable() {
* @return void
*/
public function close() {
- if ($this->pid != posix_getpid()) {
+ if ($this->pid !== posix_getpid()) {
return;
}
if ($this->ev instanceof Event) {
@@ -331,6 +331,14 @@ public function finish() {
$this->close();
$this->pool->detachBound($this);
}
+
+ /**
+ * Destructor
+ * @return void
+ */
+ public function __destruct() {
+ $this->close();
+ }
/**
* Bind given addreess
View
14 lib/BoundTCPSocket.php
@@ -42,6 +42,15 @@ public function setDefaultPort($port) {
}
/**
+ * toString handler
+ * @return string
+ */
+ public function __toString() {
+ $port = isset($this->uri['port']) ? $this->uri['port'] : $this->defaultPort;
+ return $this->uri['host'] . ':' . $port;
+ }
+
+ /**
* Bind the socket
* @return boolean Success.
*/
@@ -50,6 +59,9 @@ public function bindSocket() {
return false;
}
$port = isset($this->uri['port']) ? $this->uri['port'] : $this->defaultPort;
+ if (($port < 1024) && Daemon::$config->user !== 'root') {
+ $this->listenerMode = false;
+ }
if ($this->listenerMode) {
$this->setFd($this->uri['host'] . ':' . $port);
return true;
@@ -60,7 +72,7 @@ public function bindSocket() {
Daemon::$process->log(get_class($this->pool) . ': Couldn\'t create TCP-socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
return false;
}
- if (isset($this->uri['reuse']) && $this->uri['reuse']) {
+ if ($this->reuse) {
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
$errno = socket_last_error();
Daemon::$process->log(get_class($this->pool) . ': Couldn\'t set option REUSEADDR to socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
View
50 lib/BoundUNIXSocket.php
@@ -8,10 +8,32 @@
* @author Zorin Vasily <kak.serpom.po.yaitsam@gmail.com>
*/
class BoundUNIXSocket extends BoundSocket {
+ /**
+ * Group
+ * @var string
+ */
protected $group;
+
+ /**
+ * User
+ * @var string
+ */
protected $user;
/**
+ * Path
+ * @var string
+ */
+ protected $path;
+
+ /**
+ * toString handler
+ * @return string
+ */
+ public function __toString() {
+ return $this->path;
+ }
+ /**
* Bind socket
* @return boolean Success.
*/
@@ -20,13 +42,17 @@ public function bindSocket() {
return false;
}
- if (pathinfo($this->uri['path'], PATHINFO_EXTENSION) !== 'sock') {
- Daemon::$process->log('Unix-socket \'' . $this->uri['path'] . '\' must has \'.sock\' extension.');
- return;
+ if ($this->path === null && isset($this->uri['path'])) {
+ $this->path = $this->uri['path'];
}
- if (file_exists($this->uri['path'])) {
- unlink($this->uri['path']);
+ if (pathinfo($this->path, PATHINFO_EXTENSION) !== 'sock') {
+ Daemon::$process->log('Unix-socket \'' . $this->path . '\' must has \'.sock\' extension.');
+ return;
+ }
+
+ if (file_exists($this->path)) {
+ unlink($this->path);
}
$sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
@@ -37,17 +63,17 @@ public function bindSocket() {
}
// SO_REUSEADDR is meaningless in AF_UNIX context
- if (!@socket_bind($sock, $this->uri['path'])) {
+ if (!@socket_bind($sock, $this->path)) {
if (isset($this->config->maxboundsockets->value)) { // no error-messages when maxboundsockets defined
return false;
}
$errno = socket_last_error();
- Daemon::$process->log(get_class($this) . ': Couldn\'t bind Unix-socket \'' . $this->uri['path'] . '\' (' . $errno . ' - ' . socket_strerror($errno) . ').');
+ Daemon::$process->log(get_class($this) . ': Couldn\'t bind Unix-socket \'' . $this->path . '\' (' . $errno . ' - ' . socket_strerror($errno) . ').');
return;
}
if (!socket_listen($sock, SOMAXCONN)) {
$errno = socket_last_error();
- Daemon::$process->log(get_class($this) . ': Couldn\'t listen UNIX-socket \'' . $this->uri['path'] . '\' (' . $errno . ' - ' . socket_strerror($errno) . ')');
+ Daemon::$process->log(get_class($this) . ': Couldn\'t listen UNIX-socket \'' . $this->path . '\' (' . $errno . ' - ' . socket_strerror($errno) . ')');
}
socket_set_nonblock($sock);
chmod($this->uri['path'], 0770);
@@ -58,9 +84,9 @@ public function bindSocket() {
$this->group = Daemon::$config->group->value;
}
if ($this->group !== null) {
- if (!@chgrp($this->uri['path'], $this->group)) {
- unlink($this->uri['path']);
- Daemon::log('Couldn\'t change group of the socket \'' . $this->uri['path'] . '\' to \'' . $this->group . '\'.');
+ if (!@chgrp($this->path, $this->group)) {
+ unlink($this->path);
+ Daemon::log('Couldn\'t change group of the socket \'' . $this->path . '\' to \'' . $this->group . '\'.');
return false;
}
}
@@ -73,7 +99,7 @@ public function bindSocket() {
if ($this->user !== null) {
if (!@chown($this->uri['path'], $this->user)) {
unlink($this->uri['path']);
- Daemon::log('Couldn\'t change owner of the socket \'' . $this->uri['path'] . '\' to \'' . $this->user . '\'.');
+ Daemon::log('Couldn\'t change owner of the socket \'' . $this->path . '\' to \'' . $this->user . '\'.');
return false;
}
}
View
2 lib/DNSClient.php
@@ -204,7 +204,7 @@ public function get($hostname, $cb, $noncache = false) {
return;
}
$this->getConnectionByKey($hostname, function($conn) use ($cb, $hostname) {
- if (!$conn->isConnected()) {
+ if (!$conn || !$conn->isConnected()) {
call_user_func($cb, false);
return false;
}
View
4 lib/IOStream.php
@@ -787,8 +787,8 @@ public function onStateEv($bev, $events) {
if ($errno !== 0) {
trigger_error('Socket error #' . $errno . ':' . EventUtil::getLastSocketError(), E_USER_NOTICE);
}
- if ($this->ssl) {
- while ($err = $bev->sslError()) {
+ if ($this->ssl && $this->bev) {
+ while ($err = $this->bev->sslError()) {
trigger_error('EventBufferEvent SSL error: ' . $err . PHP_EOL, E_USER_NOTICE);
}
}

0 comments on commit d1498a8

Please sign in to comment.
Something went wrong with that request. Please try again.