Пример #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);
 }
Пример #2
0
 function MonClose()
 {
     if (!shmop_delete($this->{$shm_id})) {
         debug("No se pudo borrar el segmento de memoria compartida.", "red");
     }
     shmop_close($this->{$shm_id});
 }
Пример #3
0
 function close()
 {
     if (isset($this->segment)) {
         shmop_close($this->segment);
         $this->segment = null;
     }
 }
 public function close()
 {
     if ($this->status == self::STATUS_OPENED) {
         shmop_close($this->shmId);
         $this->status = self::STATUS_CLOSED;
     }
 }
Пример #5
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']);
}
Пример #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']);
 }
Пример #7
0
 public function __destruct()
 {
     if ($this->tempFilename) {
         unlink($this->tempFilename);
     }
     shmop_close($this->shmID);
 }
 public function save($shmKey, $appVars)
 {
     $memBlobkId = @shmop_open($shmKey, "w", 0644, 1024);
     $result = shmop_write($memBlobkId, serialize($appVars), 0);
     shmop_close($memBlobkId);
     return $result;
 }
Пример #9
0
 /**
  * Resize memory block
  * @param int $size
  * @return bool
  */
 protected function resize($size)
 {
     if ($size > $this->max_size) {
         return false;
     }
     //should be called AFTER reading memory (to not loose changing of variables)
     if (empty($this->mem)) {
         return false;
     }
     ignore_user_abort(true);
     set_time_limit(180);
     if (is_array($this->mem)) {
         $this->mem[self::map_info][self::map_info_resized] = $this->mem[self::map_info][self::map_info_resized] + 1;
         $this->mem[self::map_info][self::map_info_resizetime] = time();
     }
     shmop_delete($this->shm);
     shmop_close($this->shm);
     $t = serialize($this->mem);
     $memsize = strlen($t);
     if ($memsize > $size) {
         $size = $memsize + 1000;
     }
     $this->shm = shmop_open($this->shmkey, "n", 0777, $size);
     if (!$this->shm) {
         return false;
     }
     //mmm... oops.
     unset($this->mem);
     $w = shmop_write($this->shm, str_pad($t, shmop_size($this->shm), ' ', STR_PAD_RIGHT), 0);
     if (!$w) {
         return false;
     }
     return true;
 }
Пример #10
0
 function rm($key)
 {
     $this->shmop_key = ftok($this->pre . $key);
     $this->shmop_id = shmop_open($this->shmop_key, 'c', 0644, 0);
     $result = shmop_delete($this->shmop_id);
     shmop_close($this->shmop_id);
     return $result;
 }
Пример #11
0
function __unlock()
{
    if (isset($_SERVER['sync'])) {
        shmop_delete($_SERVER['sync']);
        shmop_close($_SERVER['sync']);
        unset($_SERVER['sync']);
    }
}
Пример #12
0
 /**
  * Get all the exceptions added to the shared memory.
  *
  * @return \Throwable[]
  */
 public function getExceptions() : array
 {
     $memory = shmop_open($this->key, "a", 0, 0);
     $exceptions = $this->unserialize($memory);
     shmop_delete($memory);
     shmop_close($memory);
     return $exceptions;
 }
Пример #13
0
 /**
  * @return boolean
  */
 public function delete()
 {
     if (!$this->exists()) {
         return true;
     }
     $shmId = shmop_open($this->shmKey, 'w', 0, 0);
     $result = (bool) shmop_delete($shmId);
     shmop_close($shmId);
     return $result;
 }
Пример #14
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;
 }
 public function close()
 {
     if (false === $this->m_segmentId) {
         return false;
     }
     // Ensure correct object state for the case shmop_close fails in FATAL.
     $segmentId = $this->m_segmentId;
     $this->m_segmentId = false;
     shmop_close($segmentId);
     return true;
 }
Пример #16
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);
     }
 }
Пример #17
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);
 }
Пример #18
0
 /**
  * delete shared memory
  *
  * @return  void
  * @throws  RuntimeException
  */
 public function delete()
 {
     $s = @shmop_open($this->genKey(), 'a', 0, 0);
     if ($s === false) {
         return;
     }
     if (!shmop_delete($s)) {
         throw new RuntimeException('could not delete shared memory');
     }
     shmop_close($s);
     unlink('/tmp/' . sha1($this->pid));
 }
 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;
 }
Пример #20
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();
 }
Пример #21
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);
 }
Пример #22
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);
 }
Пример #23
0
function geoip_load_shared_mem($file)
{
    $fp = fopen($file, "rb");
    if (!$fp) {
        print "error opening {$file}: {$php_errormsg}\n";
        exit;
    }
    $s_array = fstat($fp);
    $size = $s_array['size'];
    if ($shmid = @shmop_open(GEOIP_SHM_KEY, "w", 0, 0)) {
        shmop_delete($shmid);
        shmop_close($shmid);
    }
    $shmid = shmop_open(GEOIP_SHM_KEY, "c", 0644, $size);
    shmop_write($shmid, fread($fp, $size), 0);
    shmop_close($shmid);
}
 /**
  * Creates new shared memory block and reurns the key used to create it.
  * 
  * @return int The key / system's id for the memory block.
  * @throws \Exception
  */
 private function createSharedMemoryBlock()
 {
     $attempts = 0;
     $shm = false;
     while ($attempts < 100) {
         $key = $this->generateKey();
         $shm = shmop_open($key, 'n', 0644, $this->blockSize);
         if ($shm !== false) {
             break;
         }
         $attempts++;
     }
     if ($shm === false) {
         throw new \Exception('Cannot create new shared memory block');
     }
     shmop_close($shm);
     return $key;
 }
Пример #25
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;
 }
Пример #26
0
 /**
  * Opens segment of shared memory
  * @param  integer $segno Segment number
  * @param  boolean $create Create
  * @return integer         Segment number
  */
 public function open($segno = 0, $create = false)
 {
     if (isset($this->segments[$segno])) {
         return $this->segments[$segno];
     }
     $key = $this->key + $segno;
     if (!$create) {
         $shm = @shmop_open($key, 'w', 0, 0);
     } else {
         $shm = @shmop_open($key, 'w', 0, 0);
         if ($shm) {
             shmop_delete($shm);
             shmop_close($shm);
         }
         $shm = shmop_open($key, 'c', 0755, $this->segsize);
     }
     if (!$shm) {
         return false;
     }
     $this->segments[$segno] = $shm;
     return $shm;
 }
Пример #27
0
 /**
  * @depends testGetTubes
  */
 public function testDoWork()
 {
     $expected = ['test-tube-1' => '1', 'test-tube-2' => '2'];
     foreach ($expected as $tube => $value) {
         $this->client->addWorker($tube, function (Job $job) {
             // Store string «test-tube-%JOB_BODY%» in shared memory
             $memory = shmop_open(self::$shmKey, 'c', 0644, self::$shmLimit);
             $output = trim(shmop_read($memory, 0, self::$shmLimit));
             $output .= sprintf("\ntest-tube-%s", $job->getBody());
             shmop_write($memory, $output, 0);
             shmop_close($memory);
             exit(1);
         });
         $this->client->putInTube($tube, $value);
     }
     $this->client->doWork();
     $memory = shmop_open(self::$shmKey, 'a', 0, 0);
     $output = shmop_read($memory, 0, self::$shmLimit);
     $this->assertTrue(shmop_delete($memory));
     shmop_close($memory);
     $this->assertNotEmpty($output);
     // Compare number of items in expected list with lines in shared memory
     $this->assertEquals(count($expected), count(array_unique(explode("\n", trim($output)))));
 }
Пример #28
0
 /**
  * Close a memory allocation - this does not delete it the allocation - call shm::delete_mem first
  *
  * @param int $id the memory allocation id
  */
 function close_mem($id)
 {
     if ($id != 0) {
         shmop_close($id);
     }
 }
Пример #29
0
        }
        if ($r2[$i] or $g2[$i] or $b2[$i]) {
            $done = 0;
        }
        $s[$i * 4] = chr($c);
        $s[$i * 4 + 1] = chr($r2[$i]);
        $s[$i * 4 + 2] = chr($g2[$i]);
        $s[$i * 4 + 3] = chr($b2[$i]);
    }
    shmop_write_with_lock($shm_id, $sem_id, $s, 0);
    if ($debug & 2) {
        hex_print($s, 1);
    }
    usleep(10000);
}
shmop_close($shm_id);
//sem_remove($sem_id);
function shmop_write_with_lock($shm_id, $sem_id, $s, $offset = 0)
{
    //Acquire the semaphore
    if (!sem_acquire($sem_id)) {
        //If not available this will stall until the semaphore is released by the other process
        logformat("Failed to acquire shared memory semaphore!\n");
        sem_remove($sem_id);
        //Use even if we didn't create the semaphore as something has gone wrong and its usually debugging so lets no lock up this semaphore key
        exit(1);
    }
    shmop_write($shm_id, $s, $offset);
    //Release the semaphore
    if (!sem_release($sem_id)) {
        //Must be called after sem_acquire() so that another process can acquire the semaphore
Пример #30
0
 /**
  * Closes the shared memory block and stops manipulation
  *
  * @access public
  */
 public function __destruct()
 {
     $exists = $this->exists($this->id);
     if ($exists) {
         shmop_close($this->shmId);
     }
 }