PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LTP GCOV extension - code coverage report
Current view: directory - sysvsem - sysvsem.c
Test: PHP Code Coverage
Date: 2009-11-23 Instrumented lines: 95
Code covered: 66.3 % Executed lines: 63
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 6                                                        |
       4                 :    +----------------------------------------------------------------------+
       5                 :    | Copyright (c) 1997-2009 The PHP Group                                |
       6                 :    +----------------------------------------------------------------------+
       7                 :    | This source file is subject to version 3.01 of the PHP license,      |
       8                 :    | that is bundled with this package in the file LICENSE, and is        |
       9                 :    | available through the world-wide-web at the following url:           |
      10                 :    | http://www.php.net/license/3_01.txt                                  |
      11                 :    | If you did not receive a copy of the PHP license and are unable to   |
      12                 :    | obtain it through the world-wide-web, please send a note to          |
      13                 :    | license@php.net so we can mail you a copy immediately.               |
      14                 :    +----------------------------------------------------------------------+
      15                 :    | Authors: Tom May <tom@go2net.com>                                    |
      16                 :    |          Gavin Sherry <gavin@linuxworld.com.au>                      |
      17                 :    +----------------------------------------------------------------------+
      18                 :  */
      19                 :  
      20                 : /* $Id: sysvsem.c 276986 2009-03-10 23:40:06Z helly $ */
      21                 : 
      22                 : /* Latest update build anc tested on Linux 2.2.14
      23                 :  *
      24                 :  * This has been built and tested on Solaris 2.6 and Linux 2.1.122.
      25                 :  * It may not compile or execute correctly on other systems.
      26                 :  *
      27                 :  * sas: Works for me on Linux 2.0.36 and FreeBSD 3.0-current
      28                 :  */
      29                 : 
      30                 : #ifdef HAVE_CONFIG_H
      31                 : #include "config.h"
      32                 : #endif
      33                 : 
      34                 : #include "php.h"
      35                 : 
      36                 : #if HAVE_SYSVSEM
      37                 : 
      38                 : #include <sys/types.h>
      39                 : #include <sys/ipc.h>
      40                 : #include <sys/sem.h>
      41                 : #include <errno.h>
      42                 : 
      43                 : #include "php_sysvsem.h"
      44                 : 
      45                 : #if !HAVE_SEMUN
      46                 : 
      47                 : union semun {
      48                 :         int val;                    /* value for SETVAL */
      49                 :         struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
      50                 :         unsigned short int *array;  /* array for GETALL, SETALL */
      51                 :         struct seminfo *__buf;      /* buffer for IPC_INFO */
      52                 : };
      53                 : 
      54                 : #undef HAVE_SEMUN
      55                 : #define HAVE_SEMUN 1
      56                 : 
      57                 : #endif
      58                 : 
      59                 : /* {{{ sysvsem_functions[]
      60                 :  */
      61                 : const zend_function_entry sysvsem_functions[] = {
      62                 :         PHP_FE(sem_get,                 NULL)
      63                 :         PHP_FE(sem_acquire,             NULL)
      64                 :         PHP_FE(sem_release,             NULL)
      65                 :         PHP_FE(sem_remove,              NULL)
      66                 :         {NULL, NULL, NULL}
      67                 : };
      68                 : /* }}} */
      69                 : 
      70                 : /* {{{ sysvsem_module_entry
      71                 :  */
      72                 : zend_module_entry sysvsem_module_entry = {
      73                 :         STANDARD_MODULE_HEADER,
      74                 :         "sysvsem",
      75                 :         sysvsem_functions,
      76                 :         PHP_MINIT(sysvsem),
      77                 :         NULL,
      78                 :         NULL,
      79                 :         NULL,
      80                 :         NULL,
      81                 :         NO_VERSION_YET,
      82                 :         STANDARD_MODULE_PROPERTIES
      83                 : };
      84                 : /* }}} */
      85                 : 
      86                 : #ifdef COMPILE_DL_SYSVSEM
      87                 : ZEND_GET_MODULE(sysvsem)
      88                 : #endif
      89                 : 
      90                 : 
      91                 : THREAD_LS sysvsem_module php_sysvsem_module;
      92                 : 
      93                 : #define SEM_FETCH_RESOURCE(sem_ptr, z_ptr) ZEND_FETCH_RESOURCE(sem_ptr, sysvsem_sem *, &z_ptr, -1, PHP_SEM_RSRC_NAME, php_sysvsem_module.le_sem)
      94                 : 
      95                 : /* Semaphore functions using System V semaphores.  Each semaphore
      96                 :  * actually consists of three semaphores allocated as a unit under the
      97                 :  * same key.  Semaphore 0 (SYSVSEM_SEM) is the actual semaphore, it is
      98                 :  * initialized to max_acquire and decremented as processes acquire it.
      99                 :  * The value of semaphore 1 (SYSVSEM_USAGE) is a count of the number
     100                 :  * of processes using the semaphore.  After calling semget(), if a
     101                 :  * process finds that the usage count is 1, it will set the value of
     102                 :  * SYSVSEM_SEM to max_acquire.  This allows max_acquire to be set and
     103                 :  * track the PHP code without having a global init routine or external
     104                 :  * semaphore init code.  Except see the bug regarding a race condition
     105                 :  * php_sysvsem_get().  Semaphore 2 (SYSVSEM_SETVAL) serializes the
     106                 :  * calls to GETVAL SYSVSEM_USAGE and SETVAL SYSVSEM_SEM.  It can be
     107                 :  * acquired only when it is zero.
     108                 :  */
     109                 : 
     110                 : #define SYSVSEM_SEM             0
     111                 : #define SYSVSEM_USAGE   1
     112                 : #define SYSVSEM_SETVAL  2
     113                 : 
     114                 : /* {{{ release_sysvsem_sem
     115                 :  */
     116                 : static void release_sysvsem_sem(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     117               1 : {
     118               1 :         sysvsem_sem *sem_ptr = (sysvsem_sem *)rsrc->ptr;
     119                 :         struct sembuf sop[2];
     120               1 :         int opcount = 1;
     121                 : /*
     122                 :  * if count == -1, semaphore has been removed
     123                 :  * Need better way to handle this
     124                 :  */
     125                 : 
     126               1 :         if (sem_ptr->count == -1 || !sem_ptr->auto_release) {
     127               1 :                 efree(sem_ptr);
     128               1 :                 return;
     129                 :         }
     130                 :         /* Decrement the usage count. */
     131                 : 
     132               0 :         sop[0].sem_num = SYSVSEM_USAGE;
     133               0 :         sop[0].sem_op  = -1;
     134               0 :         sop[0].sem_flg = SEM_UNDO;
     135                 : 
     136                 :         /* Release the semaphore if it has been acquired but not released. */
     137                 : 
     138               0 :         if (sem_ptr->count) {
     139                 : 
     140               0 :                 sop[1].sem_num = SYSVSEM_SEM;
     141               0 :                 sop[1].sem_op  = sem_ptr->count;
     142               0 :                 sop[1].sem_flg = SEM_UNDO;
     143                 : 
     144               0 :                 opcount++;
     145                 :         }
     146                 : 
     147               0 :         semop(sem_ptr->semid, sop, opcount);
     148               0 :         efree(sem_ptr);
     149                 : }
     150                 : /* }}} */
     151                 : 
     152                 : /* {{{ PHP_MINIT_FUNCTION
     153                 :  */
     154                 : PHP_MINIT_FUNCTION(sysvsem)
     155           17007 : {
     156           17007 :         php_sysvsem_module.le_sem = zend_register_list_destructors_ex(release_sysvsem_sem, NULL, PHP_SEM_RSRC_NAME, module_number);
     157           17007 :         return SUCCESS;
     158                 : }
     159                 : /* }}} */
     160                 : 
     161                 : #define SETVAL_WANTS_PTR
     162                 : 
     163                 : #if defined(_AIX)
     164                 : #undef SETVAL_WANTS_PTR
     165                 : #endif
     166                 : 
     167                 : /* {{{ proto resource sem_get(int key [, int max_acquire [, int perm [, int auto_release]]) U
     168                 :    Return an id for the semaphore with the given key, and allow max_acquire (default 1) processes to acquire it simultaneously */
     169                 : PHP_FUNCTION(sem_get)
     170               1 : {
     171               1 :         long key, max_acquire = 1, perm = 0666, auto_release = 1;
     172                 :         int semid;
     173                 :         struct sembuf sop[3];
     174                 :         int count;
     175                 :         sysvsem_sem *sem_ptr;
     176                 : 
     177               1 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|lll", &key, &max_acquire, &perm, &auto_release)) {
     178               0 :                 RETURN_FALSE;
     179                 :         }
     180                 : 
     181                 :         /* Get/create the semaphore.  Note that we rely on the semaphores
     182                 :          * being zeroed when they are created.  Despite the fact that
     183                 :          * the(?)  Linux semget() man page says they are not initialized,
     184                 :          * the kernel versions 2.0.x and 2.1.z do in fact zero them.
     185                 :          */
     186                 : 
     187               1 :         semid = semget(key, 3, perm|IPC_CREAT);
     188               1 :         if (semid == -1) {
     189               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     190               0 :                 RETURN_FALSE;
     191                 :         }
     192                 : 
     193                 :         /* Find out how many processes are using this semaphore.  Note
     194                 :          * that on Linux (at least) there is a race condition here because
     195                 :          * semaphore undo on process exit is not atomic, so we could
     196                 :          * acquire SYSVSEM_SETVAL before a crashed process has decremented
     197                 :          * SYSVSEM_USAGE in which case count will be greater than it
     198                 :          * should be and we won't set max_acquire.  Fortunately this
     199                 :          * doesn't actually matter in practice.
     200                 :          */
     201                 : 
     202                 :         /* Wait for sem 1 to be zero . . . */
     203                 : 
     204               1 :         sop[0].sem_num = SYSVSEM_SETVAL;
     205               1 :         sop[0].sem_op  = 0;
     206               1 :         sop[0].sem_flg = 0;
     207                 : 
     208                 :         /* . . . and increment it so it becomes non-zero . . . */
     209                 : 
     210               1 :         sop[1].sem_num = SYSVSEM_SETVAL;
     211               1 :         sop[1].sem_op  = 1;
     212               1 :         sop[1].sem_flg = SEM_UNDO;
     213                 : 
     214                 :         /* . . . and increment the usage count. */
     215                 : 
     216               1 :         sop[2].sem_num = SYSVSEM_USAGE;
     217               1 :         sop[2].sem_op  = 1;
     218               1 :         sop[2].sem_flg = SEM_UNDO;
     219               2 :         while (semop(semid, sop, 3) == -1) {
     220               0 :                 if (errno != EINTR) {
     221               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed acquiring SYSVSEM_SETVAL for key 0x%lx: %s", key, strerror(errno));
     222               0 :                         break;
     223                 :                 }
     224                 :         }
     225                 : 
     226                 :         /* Get the usage count. */
     227               1 :         count = semctl(semid, SYSVSEM_USAGE, GETVAL, NULL);
     228               1 :         if (count == -1) {
     229               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     230                 :         }
     231                 : 
     232                 :         /* If we are the only user, then take this opportunity to set the max. */
     233                 : 
     234               1 :         if (count == 1) {
     235                 : #if HAVE_SEMUN
     236                 :                 /* This is correct for Linux which has union semun. */
     237                 :                 union semun semarg;
     238               1 :                 semarg.val = max_acquire;
     239               1 :                 if (semctl(semid, SYSVSEM_SEM, SETVAL, semarg) == -1) {
     240               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     241                 :                 }
     242                 : #elif defined(SETVAL_WANTS_PTR)
     243                 :                 /* This is correct for Solaris 2.6 which does not have union semun. */
     244                 :                 if (semctl(semid, SYSVSEM_SEM, SETVAL, &max_acquire) == -1) {
     245                 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     246                 :                 }
     247                 : #else
     248                 :                 /* This works for i.e. AIX */
     249                 :                 if (semctl(semid, SYSVSEM_SEM, SETVAL, max_acquire) == -1) {
     250                 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     251                 :                 }
     252                 : #endif
     253                 :         }
     254                 : 
     255                 :         /* Set semaphore 1 back to zero. */
     256                 : 
     257               1 :         sop[0].sem_num = SYSVSEM_SETVAL;
     258               1 :         sop[0].sem_op  = -1;
     259               1 :         sop[0].sem_flg = SEM_UNDO;
     260               2 :         while (semop(semid, sop, 1) == -1) {
     261               0 :                 if (errno != EINTR) {
     262               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed releasing SYSVSEM_SETVAL for key 0x%lx: %s", key, strerror(errno));
     263               0 :                         break;
     264                 :                 }
     265                 :         }
     266                 : 
     267               1 :         sem_ptr = (sysvsem_sem *) emalloc(sizeof(sysvsem_sem));
     268               1 :         sem_ptr->key   = key;
     269               1 :         sem_ptr->semid = semid;
     270               1 :         sem_ptr->count = 0;
     271               1 :         sem_ptr->auto_release = auto_release;
     272                 : 
     273               1 :         sem_ptr->id = ZEND_REGISTER_RESOURCE(return_value, sem_ptr, php_sysvsem_module.le_sem);
     274                 : }
     275                 : /* }}} */
     276                 : 
     277                 : /* {{{ php_sysvsem_semop
     278                 :  */
     279                 : static void php_sysvsem_semop(INTERNAL_FUNCTION_PARAMETERS, int acquire)
     280               2 : {
     281                 :         zval *arg_id;
     282                 :         sysvsem_sem *sem_ptr;
     283                 :         struct sembuf sop;
     284                 : 
     285               2 :         if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg_id)) {
     286               0 :                 RETURN_FALSE;
     287                 :         }
     288               2 :         SEM_FETCH_RESOURCE(sem_ptr, arg_id);
     289                 : 
     290               2 :         if (!acquire && sem_ptr->count == 0) {
     291               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "SysV semaphore %ld (key 0x%x) is not currently acquired", Z_LVAL_P(arg_id), sem_ptr->key);
     292               0 :                 RETURN_FALSE;
     293                 :         }
     294                 : 
     295               2 :         sop.sem_num = SYSVSEM_SEM;
     296               2 :         sop.sem_op  = acquire ? -1 : 1;
     297               2 :         sop.sem_flg = SEM_UNDO;
     298                 : 
     299               4 :         while (semop(sem_ptr->semid, &sop, 1) == -1) {
     300               0 :                 if (errno != EINTR) {
     301               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to %s key 0x%x: %s", acquire ? "acquire" : "release", sem_ptr->key, strerror(errno));
     302               0 :                         RETURN_FALSE;
     303                 :                 }
     304                 :         }
     305                 : 
     306               2 :         sem_ptr->count -= acquire ? -1 : 1;
     307               2 :         RETURN_TRUE;
     308                 : }
     309                 : /* }}} */
     310                 : 
     311                 : /* {{{ proto bool sem_acquire(resource id) U
     312                 :    Acquires the semaphore with the given id, blocking if necessary */
     313                 : PHP_FUNCTION(sem_acquire)
     314               1 : {
     315               1 :         php_sysvsem_semop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     316               1 : }
     317                 : /* }}} */
     318                 : 
     319                 : /* {{{ proto bool sem_release(resource id) U
     320                 :    Releases the semaphore with the given id */
     321                 : PHP_FUNCTION(sem_release)
     322               1 : {
     323               1 :         php_sysvsem_semop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     324               1 : }
     325                 : /* }}} */
     326                 : 
     327                 : /* {{{ proto bool sem_remove(resource id) U
     328                 :    Removes semaphore from Unix systems */
     329                 : 
     330                 : /*
     331                 :  * contributed by Gavin Sherry gavin@linuxworld.com.au
     332                 :  * Fri Mar 16 00:50:13 EST 2001
     333                 :  */
     334                 : 
     335                 : PHP_FUNCTION(sem_remove)
     336               1 : {
     337                 :         zval *arg_id;
     338                 :         sysvsem_sem *sem_ptr;
     339                 : #if HAVE_SEMUN
     340                 :         union semun un;
     341                 :         struct semid_ds buf;
     342                 : #endif
     343                 : 
     344               1 :         if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg_id)) {
     345               0 :                 RETURN_FALSE
     346                 :         }
     347               1 :         SEM_FETCH_RESOURCE(sem_ptr, arg_id);
     348                 : 
     349                 : #if HAVE_SEMUN
     350               1 :         un.buf = &buf;
     351               1 :         if (semctl(sem_ptr->semid, 0, IPC_STAT, un) < 0) {
     352                 : #else
     353                 :         if (semctl(sem_ptr->semid, 0, IPC_STAT, NULL) < 0) {
     354                 : #endif
     355               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "SysV semaphore %ld does not (any longer) exist", Z_LVAL_P(arg_id));
     356               0 :                 RETURN_FALSE;
     357                 :         }
     358                 : 
     359                 : #if HAVE_SEMUN
     360               1 :         if (semctl(sem_ptr->semid, 0, IPC_RMID, un) < 0) {
     361                 : #else
     362                 :         if (semctl(sem_ptr->semid, 0, IPC_RMID, NULL) < 0) {
     363                 : #endif
     364               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for SysV semaphore %ld: %s", Z_LVAL_P(arg_id), strerror(errno));
     365               0 :                 RETURN_FALSE;
     366                 :         }
     367                 :         
     368                 :         /* let release_sysvsem_sem know we have removed
     369                 :          * the semaphore to avoid issues with releasing.
     370                 :          */ 
     371                 : 
     372               1 :         sem_ptr->count = -1;
     373               1 :         RETURN_TRUE;
     374                 : }
     375                 : 
     376                 : /* }}} */
     377                 : 
     378                 : #endif /* HAVE_SYSVSEM */
     379                 : 
     380                 : /*
     381                 :  * Local variables:
     382                 :  * tab-width: 4
     383                 :  * c-basic-offset: 4
     384                 :  * End:
     385                 :  * vim600: sw=4 ts=4 fdm=marker
     386                 :  * vim<600: sw=4 ts=4
     387                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Mon, 23 Nov 2009 17:39:43 +0000 (35 hours ago)

Copyright © 2005-2009 The PHP Group
All rights reserved.