/** * 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); }
private function __construct() { /* if wndows */ /* function ftok($pathname, $proj_id) { $st = @stat($pathname); if (!$st) { return -1; } $key = sprintf("%u", (($st['ino'] & 0xffff) | (($st['dev'] & 0xff) << 16) | (($proj_id & 0xff) << 24))); return $key; */ $shm_key = ftok(__FILE__, 't'); $this->mShmId = shmop_open($shm_key, 'ac', 0, 0); if ($this->mShmId) { #it is already created //echo '#it is already created'; } else { #you need to create it with shmop_open using "c" only //echo '#you need to create it with shmop_open using "c" only'; $this->mShmId = shmop_open($shm_key, 'c', $this->_SHM_AC_, $this->_SHM_SIZE_); } $this->mShmId = shmop_open($shm_key, 'w', 0, 0); //echo 'ShmId:'.$this->mShmId; $this->ShmIsClean = false; }
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']); }
public function save($shmKey, $appVars) { $memBlobkId = @shmop_open($shmKey, "w", 0644, 1024); $result = shmop_write($memBlobkId, serialize($appVars), 0); shmop_close($memBlobkId); return $result; }
/** * @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']); }
/** * Memory alloc */ protected function alloc() { if (null !== $this->memory) { return; } $this->memory = shmop_open(ftok(__FILE__, $this->identifier), "c", 0644, $this->segmentSize); }
public function init() { $project = chr(getmypid() % 26 + 65); $dir = Yii::getPathOfAlias('application.runtime.cache'); if (!is_dir($dir)) { mkdir($dir); } $this->fileName = $dir . DIRECTORY_SEPARATOR . 'cache_' . $project . ".dump"; try { $shmKey = ftok(__FILE__, $project); if ($shmKey < 0) { throw new CException('Bad ftok'); } $this->shmId = @shmop_open($shmKey, "c", 0644, $this->maxSize); if ($this->shmId == 0) { throw new CException('Bad shmop'); } $try = @unserialize(shmop_read($this->shmId, 0, 0)); $this->offsetWrite = (int) $try; if ($this->offsetWrite == 0) { $this->saveOffsetWrite(true); } else { $this->detectStart(); } } catch (Exception $e) { Yii::log('Unable to init shared memory', CLogger::LEVEL_ERROR, 'sharedMemory'); } }
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; }
/** * 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; }
/** * 写入一个值到某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); }
/** * @param config * $systemId = 864; * $mode = "c"; // Access mode * $permissions = 0755; // Permissions for the shared memory segment * $size = 1024; // Size, in bytes, of the segment * $shmid = shmop_open($systemid, $mode, $permissions, $size); */ private function open($config) { $shmid = shmop_open($config['systemId'], $config['mode'], $config['permissions'], $config['size']); if ($shmid) { return $shmid; } else { throw new PiiExcepiton('apply for share memory failed'); } }
private function setupSegment() { $id = ftok($this->file, 't'); if ($id === -1) { throw new Exception('could not creating semaphore segment (ftok)'); } $this->id = $id; $this->sid = shmop_open($id, 'c', 0644, 100); }
function MonInit() { $this->{$shm_id} = shmop_open(0xff3, "c", 0644, 1024); if (!$this->{$shm_id}) { debug("No se pudo crear el segmento de memoria compartida\n", "red"); } // Obtencion del tamaño del segmento de memoria compartida $shm_size = shmop_size($this->{$shm_id}); debug("Segmento de memoria: se han reservado " . $shm_size . " bytes.\n", "blue"); }
protected function open($id, $size) { $key = $this->getKey($id); $shm = shmop_open($key, 'c', 0644, $size); if (!$shm) { trigger_error('pc_Shm: could not create shared memory segment', E_USER_ERROR); return false; } return $shm; }
/** * @return boolean */ public function delete() : bool { if (!$this->exists()) { return true; } $shmId = @shmop_open($this->shmKey, 'w', 0, 0); $result = (bool) @shmop_delete($shmId); @shmop_close($shmId); return $result; }
function shm_attach($key, $size = Null, $perm = 0666) { global $_shm_size; if ($size == Null) { $size = $_shm_size; } else { $_shm_size = $size; } return shmop_open($key, 'c', $perm, $size); }
public function open() { if (false !== $this->m_segmentId) { return $this->m_segmentId; } $segmentId = shmop_open($this->m_id); if (false === $segmentId) { return false; } return $this->m_segmentId = $segmentId; }
public function __construct() { $shmkey = ftok(__FILE__, 't'); $this->shmId = shmop_open($shmkey, "c", 0644, $this->memSize); $this->maxQSize = $this->memSize / $this->blockSize; // 申請一个信号量 $this->semId = sem_get($shmkey, 1); sem_acquire($this->semId); // 申请进入临界区 $this->init(); }
/** * 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)); }
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); }
private function createSegment() { $file = self::PID_STORE_DIR . '/' . $this->name . '$' . $this->threadCode . self::PID_STORE_EXT; $this->pidFile = $file; touch($file); $shm_key = ftok($file, "t"); if ($shm_key === -1) { throw new Exception("Fatal exception creating SHM segment (ftok)"); } $this->sid = shmop_open($shm_key, "c", 0644, 10); $this->unlock(); }
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; }
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); } }
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(); }
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); }
public function get_key($fsize, $file) { $filename = $this->TMPDIR . $this->TMPPRE . $file; if (!file_exists($filename)) { touch($filename); } $shmkey = shmop_open(ftok($filename, 'R'), "c", 0777, $fsize); if (!$shmkey) { return FALSE; } else { return $shmkey; } }
public function __construct($fpath, $proj, $size = 1024) { $key = ftok($fpath, $proj); $this->sem_id = sem_get($key); if (!$this->sem_id) { exit("Cant create sem...\n"); } $this->shm_id = shmop_open($key, 'c', 0666, $size); if (!$this->shm_id) { exit("Cant create shm...\n"); } $this->size = $size; }
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); }
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); } }
public function geoip_open($filename, $flags) { $this->flags = $flags; if ($this->flags & self::GEOIP_SHARED_MEMORY) { $this->shmid = @shmop_open(self::GEOIP_SHM_KEY, "a", 0, 0); } else { $this->filehandle = fopen($filename, 'rb') or trigger_error("GeoIP API: Can not open {$filename}\n", E_USER_ERROR); if ($this->flags & self::GEOIP_MEMORY_CACHE) { $s_array = fstat($this->filehandle); $this->memory_buffer = fread($this->filehandle, $s_array['size']); } } $this->_setup_segments(); }