Example #1
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;
 }
Example #2
1
function shm_put($shm, $data)
{
    if (!$shm) {
        return false;
    }
    return shmop_write($shm, serialize($data), 0);
}
Example #3
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);
 }
 public function write($data, $from = 0)
 {
     $count = false;
     if ($this->status == self::STATUS_OPENED) {
         $count = shmop_write($this->shmId, $data, $from);
     }
     return $count;
 }
 public function save($shmKey, $appVars)
 {
     $memBlobkId = @shmop_open($shmKey, "w", 0644, 1024);
     $result = shmop_write($memBlobkId, serialize($appVars), 0);
     shmop_close($memBlobkId);
     return $result;
 }
Example #6
0
 /**
  * Run some code in a thread.
  *
  * @param callable $func The function to execute
  * @param array|mixed $args The arguments (or a single argument) to pass to the function
  *
  * @return int The pid of the thread created to execute this code
  */
 public function call(callable $func, $args = null)
 {
     $pid = pcntl_fork();
     if ($pid == -1) {
         throw new \Exception("Failed to fork");
     }
     # If this is the child process, then run the requested function
     if (!$pid) {
         try {
             if ($args === null) {
                 $func();
             } else {
                 call_user_func_array($func, $args);
             }
         } catch (\Exception $e) {
             $memory = shmop_open($this->memoryKey, "c", 0644, static::SHARED_MEMORY_LIMIT);
             $errors = shmop_read($memory, 0, static::SHARED_MEMORY_LIMIT);
             $errors = trim($errors);
             if ($errors) {
                 $errors .= "\n";
             }
             $errors .= "Exception: " . $e->getMessage() . " (" . $e->getFile() . ":" . $e->getLine() . ")";
             shmop_write($memory, $errors, 0);
             shmop_close($memory);
             exit(1);
         }
         # Then we must exit or else we will end up the child process running the parent processes code
         die;
     }
     $this->threads[$pid] = $pid;
     return $pid;
 }
 private function Write($data)
 {
     $tmp = serialize($data);
     $this->ShmIsClean = false;
     //echo "writing $tmp<br/>";
     return shmop_write($this->mShmId, $tmp, 0) == strlen($tmp) ? true : false;
 }
Example #8
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']);
}
Example #9
0
 /**
  * 写入一个值到某shmkey对应的内存块
  *
  * @param int $shmKey 内存块标示key
  * @param string $value 值(必须是字符串,在外部序列化或者encode)
  */
 private function writeValueToSHM($shmKey, $value)
 {
     $data = $value;
     $size = mb_strlen($data, 'UTF-8');
     $shmId = shmop_open($shmKey, 'c', 0644, $size);
     shmop_write($shmId, $data, 0);
     shmop_close($shmId);
 }
 /**
  * Unlock process
  *
  * @return Mage_Index_Model_Process
  */
 public function unlock()
 {
     $shmId = shmop_open($this->getIndexerId(), 'w', self::PERM, self::LEN);
     shmop_write($shmId, str_repeat('0', $this->_getMicrotimeLen()), 0);
     // cannot delete because sometimes we're not the owner. so overwrite
     shmop_close($shmId);
     $this->_isLocked = false;
 }
Example #11
0
 /**
  * Add an exception the shared memory.
  *
  * @param \Throwable $exception The exception instance to add
  *
  * @return void
  */
 public function addException(\Throwable $exception)
 {
     $memory = shmop_open($this->key, "c", 0644, self::LIMIT);
     $exceptions = $this->unserialize($memory);
     $exceptions[] = get_class($exception) . ": " . $exception->getMessage() . " (" . $exception->getFile() . ":" . $exception->getLine() . ")";
     $data = serialize($exceptions);
     shmop_write($memory, $data, 0);
     shmop_close($memory);
 }
Example #12
0
 /**
  * Mark a shared memory block for deletion.
  *
  * @return bool
  */
 public function delete()
 {
     /*
      * Bug fix
      * @link https://bugs.php.net/bug.php?id=71921
      */
     shmop_write($this->shmid, str_pad('', shmop_size($this->shmid), ' '), 0);
     return shmop_delete($this->shmid);
 }
Example #13
0
 function MonWrite($data)
 {
     // Escritura de una cadena de texto de prueba en la memoria compartida
     $shm_bytes_written = shmop_write($this->{$shm_id}, $data, 0);
     if ($shm_bytes_written != strlen($data)) {
         debug("No se pudieron escribir todos los datos indicados\n", "red");
     } else {
         debug("Saved {$data}\n", "magenta");
     }
 }
Example #14
0
 function set($key, $val, $ttl = 600)
 {
     if (function_exists('gzcompress')) {
         $val = gzcompress($val, 3);
     }
     $this->shmop_key = ftok($this->pre . $key);
     $this->shmop_id = shmop_open($this->shmop_key, 'c', 0644, strlen($val));
     $result = shmop_write($this->shmop_id, $val, 0);
     shmop_close($this->shmop_id);
     return $result;
 }
Example #15
0
 public function write($id, $size, $data)
 {
     $shm = $this->open($id, $size);
     $written = shmop_write($shm, $data, 0);
     $this->close($shm);
     if ($written != strlen($data)) {
         trigger_error('pc_Shm: could not write entire length of data', E_USER_ERROR);
         return false;
     }
     return true;
 }
Example #16
0
 public function enQueue($value)
 {
     if ($this->ptrInc($this->rear) == $this->front) {
         // 队满
         return false;
     }
     $data = $this->encode($value);
     shmop_write($this->shmId, $data, $this->rear);
     $this->rear = $this->ptrInc($this->rear);
     return true;
 }
Example #17
0
 protected function tearDown()
 {
     if ($shmid = @shmop_open(self::SHMOP_ID, 'w', 0644, 0)) {
         /*
          * Fill memory block for fix bug
          * @link https://bugs.php.net/bug.php?id=71921
          */
         shmop_write($shmid, str_pad('', strlen(self::WORD), ' '), 0);
         shmop_delete($shmid);
         shmop_close($shmid);
     }
 }
Example #18
0
 public function __destruct()
 {
     if ($this->changed) {
         $serialized = serialize($this->data);
         if (strlen($serialized) > shmop_size($this->res)) {
             shmop_delete($this->res);
             $this->res = shmop_open($id, 'c', 0644, ceil(strlen($serialized) * 1.25));
         }
         shmop_write($this->res, $serialized, 0);
     }
     shmop_close($this->res);
 }
Example #19
0
 public function writeShared($data_arr)
 {
     $null_fill = str_repeat(str_repeat(' ', 10), $this->size / 10);
     shmop_write($this->shm_id, $null_fill, 0);
     $data = serialize($data_arr);
     if (strlen($data) > $this->size) {
         return false;
     }
     shmop_write($this->shm_id, $data, 0);
     sem_release($this->sem_id);
     return true;
 }
 private function _write(&$val, &$lh)
 {
     $id = shmop_open($this->handler, 'c', 0600, $this->options['size']);
     if ($id) {
         $ret = shmop_write($id, $val, 0) == strlen($val);
         shmop_close($id);
         $this->_unlock($lh);
         return $ret;
     }
     $this->_unlock($lh);
     return false;
 }
Example #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();
 }
Example #22
0
 private function _setData($data)
 {
     // serialize data before storing to shared memory
     $data = serialize($data);
     // get new data length
     $dataLength = mb_strlen($data, 'UTF8');
     // storee new data length
     $this->_setDataLength($dataLength);
     // store new data
     $dataShm = shmop_open($this->_dataShmKey, 'c', 0644, $dataLength);
     shmop_write($dataShm, $data, 0);
     shmop_close($dataShm);
 }
Example #23
0
 function stream_write($data)
 {
     $p = $this->path;
     $fp = fopen($p, 'w');
     fwrite($fp, $data);
     fclose($fp);
     $this->id = ftok($this->path, 'M');
     if ($t = @shmop_open($this->id, 'a', 0, 0)) {
         shmop_delete($t);
     }
     $t = shmop_open($this->id, 'c', 0755, strlen($data));
     shmop_write($t, $data, 0);
 }
function dmx_write_shared_memory($shm_id, $di, $do1, $do2)
{
    global $debug;
    if ($di) {
        shmop_write($shm_id, $di, 0);
    }
    if ($do1) {
        shmop_write($shm_id, $do1, 256);
    }
    if ($do2) {
        shmop_write($shm_id, $do2, 512);
    }
    return 1;
}
Example #25
0
 public function writemem($shmkey, $fdata)
 {
     if ($this->MEMCOMPRESS && function_exists('gzcompress')) {
         $fdata = gzcompress($fdata, $this->MEMCOMPRESSLVL);
     }
     $fsize = strlen($fdata);
     $shm_bytes_written = shmop_write($shmkey, $fdata, 0);
     $this->updatestats($shm_bytes_written, "add");
     if ($shm_bytes_written != $fsize) {
         return false;
     } else {
         return $shm_bytes_written;
     }
 }
Example #26
0
 public function put($data)
 {
     $size = self::generateSize($data);
     $exists = $this->exists($this->id);
     if ($exists) {
         shmop_delete($this->shmId);
         shmop_close($this->shmId);
         $this->shmId = shmop_open($this->id, "c", $this->perms, $size);
         shmop_write($this->shmId, serialize($data), 0);
     } else {
         $this->shmId = shmop_open($this->id, "c", $this->perms, $size);
         shmop_write($this->shmId, serialize($data), 0);
     }
 }
Example #27
0
 /**
  * @param string $data
  * @return boolean
  */
 public function write($data)
 {
     $data = (string) $data;
     if ($this->exists($this->shmKey)) {
         $this->delete();
     }
     $size = mb_strlen($data, 'UTF-8');
     $shmId = shmop_open($this->shmKey, 'c', 666, $size);
     if (!$shmId) {
         return false;
     }
     shmop_write($shmId, $data, 0);
     shmop_close($shmId);
     return true;
 }
 static function clear($changed_tables)
 {
     $cache_data = shmop_read(self::$shm, self::$_id, self::$_size);
     $cache_data = @unserialize($cache_data);
     foreach ($changed_tables as $table => $flag) {
         if (!empty(self::$_cache_handlers[$table])) {
             foreach (self::$_cache_handlers[$table] as $cache_name => $_d) {
                 unset($cache_data[$cache_name]);
             }
         }
     }
     $cache_data['cache_handlers'] = self::$_cache_handlers;
     shmop_write(self::$shm, serialize($cache_data), self::$_id);
     return true;
 }
Example #29
0
 /**
  * write data
  *
  * @param   mix     $data
  * @return  void
  * @throws  RuntimeException
  */
 public function write($data)
 {
     $serializedData = serialize(array('pid' => $this->pid, 'data' => $data));
     $s = shmop_open($this->genKey(), 'n', 0666, strlen($serializedData));
     if ($s === false) {
         throw new RuntimeException('could not open shared memory');
     }
     $writtenSize = shmop_write($s, $serializedData, 0);
     if ($writtenSize === false) {
         shmop_delete($s);
         shmop_close($s);
         throw new RuntimeException('could not write the data to shared memory');
     }
     shmop_close($s);
 }
Example #30
0
 private function write($data = null)
 {
     if (is_null($data)) {
         $db = STORAGE_PATH . DS . 'memory' . sha1($this->ns . $this->entity) . '.db';
         $data = $this->data();
         File::delete($db);
         File::put($db, $data);
     }
     shmop_delete($this->db);
     shmop_close($this->db);
     $size = $this->size > strlen($data) ? $this->size : strlen($data);
     $this->size = $size;
     $this->db = shmop_open($this->key, 'c', 0755, $size);
     return shmop_write($this->db, $data, 0);
 }