Ejemplo n.º 1
1
 /**
  * Writes a message to the shared memory.
  *
  * @param mixed   $message The message to send
  * @param integer $signal  The signal to send afterward
  * @param integer $pause   The number of microseconds to pause after signalling
  */
 public function send($message, $signal = null, $pause = 500)
 {
     $messageArray = array();
     if (($shmId = @shmop_open($this->pid, 'a', 0, 0)) > 0) {
         // Read any existing messages in shared memory
         $readMessage = shmop_read($shmId, 0, shmop_size($shmId));
         $messageArray[] = unserialize($readMessage);
         shmop_delete($shmId);
         shmop_close($shmId);
     }
     // Add the current message to the end of the array, and serialize it
     $messageArray[] = $message;
     $serializedMessage = serialize($messageArray);
     // Write new serialized message to shared memory
     $shmId = shmop_open($this->pid, 'c', 0644, strlen($serializedMessage));
     if (!$shmId) {
         throw new ProcessControlException(sprintf('Not able to create shared memory segment for PID: %s', $this->pid));
     } else {
         if (shmop_write($shmId, $serializedMessage, 0) !== strlen($serializedMessage)) {
             throw new ProcessControlException(sprintf('Not able to write message to shared memory segment for segment ID: %s', $shmId));
         }
     }
     if (false === $signal) {
         return;
     }
     $this->signal($signal ?: $this->signal);
     usleep($pause);
 }
Ejemplo n.º 2
1
 function shm_remove_var($id, $key)
 {
     global $_shm_size;
     if (($dat = @shmop_read($id, 0, 4)) === false) {
         $dat = array();
     } else {
         $len = unpack('L', $dat);
         $len = array_shift($len);
         $dat = unserialize(shmop_read($id, 4, $len));
     }
     if (isset($dat[$key])) {
         unset($dat[$key]);
     } else {
         return false;
     }
     $dat = serialize($dat);
     $l = strlen($dat);
     if ($l + 4 > $_shm_size) {
         return false;
     }
     $dat = pack('L', strlen($dat)) . $dat;
     if (@shmop_write($id, $dat, 0) !== false) {
         return true;
     }
     return false;
 }
Ejemplo n.º 3
0
 /**
  * This function implements the reading mechanism for a shared memory
  * segment following a specific layout.
  * @param $index position that should be read regarding the layout.
  *               if $index is left -1 then the upcoming parameters will
  *               be used to perform a search otherwise they are used to
  *               perform checks on the elment addressed.
  * @param $skey string key for the element's $kpos'th node
  * @param $kpos $skey position regarding the segment layout
  * @param $val value of the last serialized element stored
  *             if it is a string the type of the objects will be compared
  *             otherwise the obejects will be compared
  * @return trimmed string for the whole contents of the segment if 
  *         $index is -1, if $index is >= 0 and valid the complete 
  *         element at this position, otherwise an empty string
  */
 public function read($index = -1, $skey = "", $kpos = -1, $val = null, $full = true)
 {
     $rs = "";
     $this->log(__METHOD__ . ": idx=%, skey=%, kpos=%, val=%, full=%", array($index, $skey, $kpos, StringUtil::get_object_string($val), $full));
     // reading the whole contents if index is not set and the upcoming
     // parameters either
     if (strncmp(gettype($index), "integer", 7) == 0 && $index == -1 && strncmp(gettype($skey), "string", 6) == 0 && strlen($skey) == 0 && strncmp(gettype($kpos), "integer", 7) == 0 && $kpos == -1) {
         $s = shmop_read($this->element->get_shm_seg_id(), 0, $this->element->get_shm_seg_size());
         if ($s) {
             $rs = trim($s);
         }
         // searching if the index is not set but parameters 2 to 5 are
         // but not with default values
     } else {
         if (strncmp(gettype($index), "integer", 7) == 0 && $index == -1 && strncmp(gettype($skey), "string", 6) == 0 && strlen($skey) > 0 && strncmp(gettype($kpos), "integer", 7) == 0 && $kpos > -1) {
             $rs = $this->search($index, $skey, $kpos, $val, $this->get_search_type());
             // reading an index is a bit more complex
         } else {
             if (strncmp(gettype($index), "integer", 7) == 0 && $index > -1) {
                 $xcontents = $this->get();
                 $xcli = count($xcontents) - 1;
                 // now lets get the requested element if it exist
                 // and complete its layout
                 if ($index <= $xcli) {
                     $rs = trim($xcontents[$index]) . $this->element->get_shm_seg_var_eright() . $this->element->get_shm_seg_var_delimiter();
                 }
             }
         }
     }
     if ($full) {
         return $rs;
     } else {
         return unserialize(substr($rs, strrpos($rs, $this->element->get_shm_seg_var_eleft()) + 1, strlen($rs) - strlen($this->element->get_shm_seg_var_eright() . $this->element->get_shm_seg_var_delimiter()) - strrpos($rs, $this->element->get_shm_seg_var_eleft()) - 1));
     }
 }
Ejemplo n.º 4
0
function session_value($name, $index)
{
    global $shm_key, $shm_var, $sem_id;
    switch ($index) {
        case 'config':
            $shm_size = 859;
            break;
        case 'ipdata':
            $shm_size = 30050;
            break;
        default:
            $shm_size = 0;
    }
    sem_acquire($sem_id['shlock']);
    $shm_id = shmop_open($shm_key[$index], 'c', 0644, $shm_size);
    if ($name == 'update') {
        $shm_data = serialize($shm_var[$index]);
        shmop_write($shm_id, str_pad($shm_data, $shm_size, "", STR_PAD_RIGHT), 0);
    } else {
        $shm_data = shmop_read($shm_id, 0, $shm_size);
        $shm_var[$index] = @unserialize($shm_data);
    }
    shmop_close($shm_id);
    sem_release($sem_id['shlock']);
}
Ejemplo n.º 5
0
 /**
  * 读取缓存
  * 
  * @access public
  * @param string $name
  *            缓存变量名
  * @return mixed
  */
 public function get($name = false)
 {
     N('cache_read', 1);
     $id = shmop_open($this->handler, 'c', 0600, 0);
     if ($id !== false) {
         $ret = unserialize(shmop_read($id, 0, shmop_size($id)));
         shmop_close($id);
         if ($name === false) {
             return $ret;
         }
         $name = $this->options['prefix'] . $name;
         if (isset($ret[$name])) {
             $content = $ret[$name];
             if (C('DATA_CACHE_COMPRESS') && function_exists('gzcompress')) {
                 // 启用数据压缩
                 $content = gzuncompress($content);
             }
             return $content;
         } else {
             return null;
         }
     } else {
         return false;
     }
 }
Ejemplo n.º 6
0
 /**
  * @param $key
  * @param int $mode
  * @param int $size
  * @return mixed|null
  * @throws OpenSharedMemoryException
  * @throws ReadSharedMemoryException
  * @throws SupportSharedMemoryException
  */
 public static function read($key, $mode = 0644, $size = 100)
 {
     if (!self::checkSHMOPSupport()) {
         return null;
     }
     @($shm_id = shmop_open($key, "a", $mode, $size));
     //read only
     if (!$shm_id) {
         throw new OpenSharedMemoryException("The shared memory block could not be opened");
     }
     @($cached_string = shmop_read($shm_id, 0, $size));
     if (!$cached_string) {
         shmop_delete($shm_id);
         shmop_close($shm_id);
         throw new ReadSharedMemoryException("The shared memory block could not be read");
     }
     $data = json_decode($cached_string, true);
     if (isset($data['expiration']) && time() > $data['expiration'] || !isset($data['expiration'])) {
         shmop_delete($shm_id);
         shmop_close($shm_id);
         return null;
     }
     shmop_close($shm_id);
     return unserialize($data['value']);
 }
Ejemplo n.º 7
0
 public function read()
 {
     if (!empty($this->id) && !isset($this->shmSize)) {
         $header = shmop_read($this->id, 0, 16);
         $aHeader = unpack('lcrcHeader/lshmSize/ldataSize/lcrcData', $header);
         $crcHeader = crc32(substr($header, 1));
         $crcHeader = 0;
         if ($crcHeader == $aHeader['crcHeader']) {
             $this->shmSize = $aHeader['shmSize'];
             if ($aHeader['dataSize'] > 0) {
                 $data = shmop_read($this->id, 16, $aHeader['dataSize']);
                 $crcData = crc32($data);
                 $crcData = 0;
                 if ($crcData == $aHeader['crcData']) {
                     $this->aBucket = unserialize($data);
                     return true;
                 }
             }
         } else {
             // Damaged!
             $this->recreate();
         }
     }
     return false;
 }
Ejemplo n.º 8
0
 public function readShared()
 {
     sem_acquire($this->sem_id);
     $data = shmop_read($this->shm_id, 0, $this->size);
     $data = trim($data);
     $data = unserialize($data);
     return $data;
 }
 public function read($length = 0, $from = 0)
 {
     $data = null;
     if ($this->status == self::STATUS_OPENED) {
         $data = shmop_read($this->shmId, $from, $length);
     }
     return $data;
 }
Ejemplo n.º 10
0
 public function read($start, $length = null)
 {
     $offset = $start >= 0 ? $start : $this->shmSize + $start;
     if (null === $length) {
         $length = $start >= 0 ? $this->shmSize - $start : -$start;
     }
     return shmop_read($this->shmID, $offset, $length);
 }
Ejemplo n.º 11
0
 function MonRead()
 {
     $my_string = shmop_read($this->{$shm_id}, 0, $shm_size);
     if (!$my_string) {
         debug("No se pudo leer el segmento de memoria compartida\n", "red");
     }
     return $my_string;
 }
Ejemplo n.º 12
0
function shm_get($shm)
{
    if (!$shm) {
        return array();
    }
    $data = shmop_read($shm, 0, SFC_SHM_DATASIZE);
    return $data === false ? array() : unserialize($data);
}
Ejemplo n.º 13
0
 /**
  * Get the exception details out of shared memory.
  *
  * @param resource $memory The shmop resource from shmop_open()
  *
  * @return \Throwable[]
  */
 private function unserialize($memory) : array
 {
     $data = shmop_read($memory, 0, self::LIMIT);
     $exceptions = unserialize($data);
     if (!is_array($exceptions)) {
         $exceptions = [];
     }
     return $exceptions;
 }
Ejemplo n.º 14
0
 public function deQueue()
 {
     if ($this->front == $this->rear) {
         // 队空
         return false;
     }
     $value = shmop_read($this->shmId, $this->front, $this->blockSize - 1);
     $this->front = $this->ptrInc($this->front);
     return $this->decode($value);
 }
Ejemplo n.º 15
0
 protected function wait(Thread $job)
 {
     while (true) {
         $data = shmop_read($job->sid, 0, 1);
         if (strcmp($data, self::UNLOCK) === 0) {
             break;
         }
         $this->sleep(1);
     }
 }
Ejemplo n.º 16
0
 public function read($id, $size)
 {
     $shm = $this->open($id, $size);
     $data = shmop_read($shm, 0, $size);
     $this->close($shm);
     if (!$data) {
         trigger_error('pc_Shm: could not read from shared memory block', E_USER_ERROR);
         return false;
     }
     return $data;
 }
Ejemplo n.º 17
0
 public function getCallbackParams()
 {
     $shmId = @shmop_open($this->pid, 'a', 0644, 0);
     if (empty($shmId)) {
         return false;
     }
     $datas = unserialize(shmop_read($shmId, 0, shmop_size($shmId)));
     shmop_delete($shmId);
     shmop_close($shmId);
     return $datas;
 }
Ejemplo n.º 18
0
 function get($key)
 {
     $this->shmop_key = ftok($this->pre . $key);
     //Linux/Unix Only
     $this->shmop_id = shmop_open($this->shmop_key, 'c', 0644, 0);
     if ($this->shmop_id === false) {
         return false;
     }
     $data = shmop_read($this->shmop_id, 0, shmop_size($this->shmop_id));
     shmop_close($this->shmop_id);
     return function_exists('gzuncompress') ? gzuncompress($data) : $data;
 }
Ejemplo n.º 19
0
 public function readmem($shmkey, $shm_size)
 {
     $my_string = shmop_read($shmkey, 0, $shm_size);
     if ($this->MEMCOMPRESS && function_exists('gzuncompress')) {
         $my_string = gzuncompress($my_string);
     }
     if (!$my_string) {
         return false;
     } else {
         return $my_string;
     }
 }
Ejemplo n.º 20
0
function dmx_read_shared_memory($shm_id, &$dmx_inputs, &$dmx_outputs1, &$dmx_outputs2)
{
    global $debug;
    if ($s = shmop_read($shm_id, 0, 128 * 2 * 3)) {
        $dmx_inputs = substr($s, 0, 256);
        $dmx_outputs1 = substr($s, 256, 256);
        $dmx_outputs2 = substr($s, 512, 256);
        return 1;
    } else {
        return 0;
    }
}
Ejemplo n.º 21
0
 function execute()
 {
     set_error_handler('fragtable_error_handler');
     define('FRAGTABLE_ROOT', dirname(__FILE__));
     $HTTP_HOST = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'NULL';
     $HTTP_USER_AGENT = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'NULL';
     if (!isset($_SERVER['REQUEST_URI'])) {
         trigger_error("'REQUEST_URI' not found", E_USER_WARNING);
     }
     $salt = $HTTP_USER_AGENT . $_SERVER['REQUEST_URI'];
     $hashi = hexdec(substr(hash('crc32b', $salt), 0, 2));
     $subject = "[{$HTTP_HOST}] DDoS WARNING!!";
     $accucnt = 0;
     $logfile = FRAGTABLE_ROOT . '/secure.log';
     $shm_key['data'] = ftok(__FILE__, 't');
     $shm_key['lock'] = ftok(__FILE__, 'u');
     $sem_id = sem_get($shm_key['lock']);
     $shm_size = 9626;
     sem_acquire($sem_id);
     $shm_id = shmop_open($shm_key['data'], 'c', 0644, $shm_size);
     $shm_data = shmop_read($shm_id, 0, $shm_size);
     $fragtable = @unserialize($shm_data);
     if (!$fragtable) {
         $fragtable = array();
     }
     $nowtime = time();
     if (isset($fragtable[$hashi]) && $nowtime < $fragtable[$hashi][0] + self::$intervsl) {
         $accucnt = $fragtable[$hashi][1] < 99 ? $fragtable[$hashi][1] + 1 : $fragtable[$hashi][1];
         $acctime = $fragtable[$hashi][0];
     } else {
         $accucnt = 1;
         $acctime = $nowtime;
     }
     $fragtable[$hashi] = array($acctime, $accucnt);
     $shm_data = serialize($fragtable);
     shmop_write($shm_id, str_pad($shm_data, $shm_size, "", STR_PAD_RIGHT), 0);
     shmop_close($shm_id);
     sem_release($sem_id);
     $fragtable = $shm_data = NULL;
     if ($accucnt > self::$threshold) {
         if (!file_exists($logfile) || filesize($logfile) < 10 * 1024 * 1024) {
             $message = sprintf("%s | %d | %d | %s | %s | %s\n", gmdate('Y-m-d H:i:s', $nowtime + self::$timezone * 3600), $acctime, $hashi, str_pad($_SERVER["REMOTE_ADDR"], 15, ' ', STR_PAD_RIGHT), "{$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']} {$_SERVER['SERVER_PROTOCOL']}", $HTTP_USER_AGENT);
             if (!file_exists($logfile) || $nowtime > filemtime($logfile) + 3600) {
                 @mail(self::$mailto, $subject, $message);
             }
             file_put_contents($logfile, $message, FILE_APPEND | LOCK_EX);
         }
         header('HTTP/1.1 503 Service Temporarily Unavailable');
         die('<h1>Service Temporarily Unavailable</h1>');
     }
     restore_error_handler();
 }
Ejemplo n.º 22
0
 public function __construct($id, $startSize = 10240)
 {
     $this->id = $id;
     $this->res = shmop_open($id, 'c', 0644, $startSize);
     $data = trim(shmop_read($this->res, 0, shmop_size($this->res)));
     if (empty($data)) {
         $this->data = array();
     } else {
         if (!is_array($this->data = unserialize($data))) {
             $this->data = array();
         }
     }
 }
Ejemplo n.º 23
0
function io_read_shared_memory($shm_id, &$out_states, &$in_states, &$in_programs, &$in_levels)
{
    global $debug;
    if ($s = shmop_read($shm_id, 0, 64 * 2 * 4)) {
        $out_states = substr($s, 0, 128);
        $in_states = substr($s, 128, 128);
        $in_programs = substr($s, 256, 128);
        $in_levels = substr($s, 384, 128);
        return 1;
    } else {
        return 0;
    }
}
 private function Read()
 {
     if ($this->ShmIsClean) {
         //echo "reading cache: ".print_r($this->mShmCache, true)."<br/>";
         return $this->mShmCache;
     } else {
         $this->ShmIsClean = true;
         $this->mShmCache = unserialize(shmop_read($this->mShmId, 0, $this->_SHM_SIZE_));
         //echo "reading: ".print_r($this->mShmCache, true)."<br/>";
         if ($this->mShmCache == false) {
             $this->mShmCache = array('gtfw' => '');
         }
         return $this->mShmCache;
     }
 }
Ejemplo n.º 25
0
 function stream_read($count)
 {
     if ($this->body === NULL) {
         $this->id = ftok($this->path, 'M');
         $shm = @shmop_open($this->id, 'a', 0, 0);
         if ($shm) {
             $this->body = shmop_read($shm, 0, shmop_size($shm));
         } else {
             $this->body = file_get_contents($this->path);
         }
     }
     $ret = substr($this->body, $this->position, $count);
     $this->position += strlen($ret);
     return $ret;
 }
Ejemplo n.º 26
0
 /**
  * read data and delete shared memory
  *
  * @return  mix
  * @throws  RuntimeException
  */
 public function readAndDelete()
 {
     $s = shmop_open($this->genKey(), 'a', 0, 0);
     if ($s === false) {
         throw new RuntimeException('could not open shared memory');
     }
     $data = shmop_read($s, 0, shmop_size($s));
     shmop_close($s);
     try {
         $this->delete();
     } catch (RuntimeException $e) {
         throw $e;
     }
     $unserialized = unserialize($data);
     return $unserialized['data'];
 }
Ejemplo n.º 27
0
 public function load($shmKey)
 {
     $memBlobkId = @shmop_open($shmKey, "w", 0644, 1024);
     $appVars = array();
     if ($memBlobkId === false) {
         //create new memory block
         $memBlobkId = shmop_open($shmKey, "c", 0644, 1024);
         shmop_write($memBlobkId, serialize($appVars), 0);
     } else {
         //load app vars to varialbe
         $shm_data = shmop_read($memBlobkId, 0, 1024);
         $appVars = unserialize($shm_data);
     }
     shmop_close($memBlobkId);
     return $appVars;
 }
Ejemplo n.º 28
0
 /**
  * Read datas with $uid key
  * @param mixed $uid
  * @return mixed
  */
 public function read($uid)
 {
     if (!is_int($uid) && !is_numeric($uid)) {
         throw new Exception\RuntimeException('Segment type key must integer or numeric.');
     }
     if ($uid * $this->blocSize >= $this->segmentSize) {
         throw new Exception\RuntimeException('Invalid access bloc. Only ' . floor($this->segmentSize / $this->blocSize) . ' blocs are allowed.');
     }
     $this->alloc();
     $str = shmop_read($this->memory, $uid * $this->blocSize, $this->blocSize);
     $str = trim($str);
     if (!$str) {
         return false;
     }
     return $str;
 }
Ejemplo n.º 29
0
 /**
  * @todo fix mixed return types!
  * @return string|null
  */
 public function read()
 {
     if (!$this->exists()) {
         return null;
     }
     $shmId = @shmop_open($this->shmKey, 'w', 0, 0);
     if (!$shmId) {
         return null;
     }
     $size = @shmop_size($shmId);
     if (!$size) {
         return null;
     }
     $data = @shmop_read($shmId, 0, $size);
     @shmop_close($shmId);
     return (string) $data;
 }
Ejemplo n.º 30
0
 /**
  * Check if process is locked
  *
  * @return bool
  */
 public function isLocked()
 {
     if (0 === $this->getIndexerId()) {
         Mage::throwException('FastIndexer: IndexerId cannot be 0');
     }
     if (null !== $this->_isLocked) {
         return $this->_isLocked;
     }
     $shmId = @shmop_open($this->getIndexerId(), 'a', self::PERM, self::LEN);
     if (false === $shmId) {
         $this->_isLocked = false;
         return $this->_isLocked;
     }
     $size = shmop_size($shmId);
     $startTime = shmop_read($shmId, 0, $size);
     shmop_close($shmId);
     $this->_isLocked = $this->_isLockedByTtl((double) $startTime);
     return $this->_isLocked;
 }