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-19 Instrumented lines: 97
Code covered: 66.0 % Executed lines: 64
Legend: not executed executed

       1                 : /*
       2                 :    +----------------------------------------------------------------------+
       3                 :    | PHP Version 5                                                        |
       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 272374 2008-12-31 11:17:49Z sebastian $ */
      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                 : 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                 : /* Semaphore functions using System V semaphores.  Each semaphore
      94                 :  * actually consists of three semaphores allocated as a unit under the
      95                 :  * same key.  Semaphore 0 (SYSVSEM_SEM) is the actual semaphore, it is
      96                 :  * initialized to max_acquire and decremented as processes acquire it.
      97                 :  * The value of semaphore 1 (SYSVSEM_USAGE) is a count of the number
      98                 :  * of processes using the semaphore.  After calling semget(), if a
      99                 :  * process finds that the usage count is 1, it will set the value of
     100                 :  * SYSVSEM_SEM to max_acquire.  This allows max_acquire to be set and
     101                 :  * track the PHP code without having a global init routine or external
     102                 :  * semaphore init code.  Except see the bug regarding a race condition
     103                 :  * php_sysvsem_get().  Semaphore 2 (SYSVSEM_SETVAL) serializes the
     104                 :  * calls to GETVAL SYSVSEM_USAGE and SETVAL SYSVSEM_SEM.  It can be
     105                 :  * acquired only when it is zero.
     106                 :  */
     107                 : 
     108                 : #define SYSVSEM_SEM             0
     109                 : #define SYSVSEM_USAGE   1
     110                 : #define SYSVSEM_SETVAL  2
     111                 : 
     112                 : /* {{{ release_sysvsem_sem
     113                 :  */
     114                 : static void release_sysvsem_sem(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     115               1 : {
     116               1 :         sysvsem_sem *sem_ptr = (sysvsem_sem *)rsrc->ptr;
     117                 :         struct sembuf sop[2];
     118               1 :         int opcount = 1;
     119                 : /*
     120                 :  * if count == -1, semaphore has been removed
     121                 :  * Need better way to handle this
     122                 :  */
     123                 : 
     124               1 :         if (sem_ptr->count == -1 || !sem_ptr->auto_release) {
     125               1 :                 efree(sem_ptr);
     126               1 :                 return;
     127                 :         }
     128                 :         /* Decrement the usage count. */
     129                 : 
     130               0 :         sop[0].sem_num = SYSVSEM_USAGE;
     131               0 :         sop[0].sem_op  = -1;
     132               0 :         sop[0].sem_flg = SEM_UNDO;
     133                 : 
     134                 :         /* Release the semaphore if it has been acquired but not released. */
     135                 : 
     136               0 :         if (sem_ptr->count) {
     137                 : 
     138               0 :                 sop[1].sem_num = SYSVSEM_SEM;
     139               0 :                 sop[1].sem_op  = sem_ptr->count;
     140               0 :                 sop[1].sem_flg = SEM_UNDO;
     141                 : 
     142               0 :                 opcount++;
     143                 :         }
     144                 : 
     145               0 :         semop(sem_ptr->semid, sop, opcount);
     146               0 :         efree(sem_ptr);
     147                 : }
     148                 : /* }}} */
     149                 : 
     150                 : /* {{{ PHP_MINIT_FUNCTION
     151                 :  */
     152                 : PHP_MINIT_FUNCTION(sysvsem)
     153           13565 : {
     154           13565 :         php_sysvsem_module.le_sem = zend_register_list_destructors_ex(release_sysvsem_sem, NULL, "sysvsem", module_number);
     155           13565 :         return SUCCESS;
     156                 : }
     157                 : /* }}} */
     158                 : 
     159                 : #define SETVAL_WANTS_PTR
     160                 : 
     161                 : #if defined(_AIX)
     162                 : #undef SETVAL_WANTS_PTR
     163                 : #endif
     164                 : 
     165                 : /* {{{ proto resource sem_get(int key [, int max_acquire [, int perm [, int auto_release]])
     166                 :    Return an id for the semaphore with the given key, and allow max_acquire (default 1) processes to acquire it simultaneously */
     167                 : PHP_FUNCTION(sem_get)
     168               1 : {
     169               1 :         long key, max_acquire = 1, perm = 0666, auto_release = 1;
     170                 :         int semid;
     171                 :         struct sembuf sop[3];
     172                 :         int count;
     173                 :         sysvsem_sem *sem_ptr;
     174                 : 
     175               1 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|lll", &key, &max_acquire, &perm, &auto_release)) {
     176               0 :                 RETURN_FALSE;
     177                 :         }
     178                 : 
     179                 :         /* Get/create the semaphore.  Note that we rely on the semaphores
     180                 :          * being zeroed when they are created.  Despite the fact that
     181                 :          * the(?)  Linux semget() man page says they are not initialized,
     182                 :          * the kernel versions 2.0.x and 2.1.z do in fact zero them.
     183                 :          */
     184                 : 
     185               1 :         semid = semget(key, 3, perm|IPC_CREAT);
     186               1 :         if (semid == -1) {
     187               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     188               0 :                 RETURN_FALSE;
     189                 :         }
     190                 : 
     191                 :         /* Find out how many processes are using this semaphore.  Note
     192                 :          * that on Linux (at least) there is a race condition here because
     193                 :          * semaphore undo on process exit is not atomic, so we could
     194                 :          * acquire SYSVSEM_SETVAL before a crashed process has decremented
     195                 :          * SYSVSEM_USAGE in which case count will be greater than it
     196                 :          * should be and we won't set max_acquire.  Fortunately this
     197                 :          * doesn't actually matter in practice.
     198                 :          */
     199                 : 
     200                 :         /* Wait for sem 1 to be zero . . . */
     201                 : 
     202               1 :         sop[0].sem_num = SYSVSEM_SETVAL;
     203               1 :         sop[0].sem_op  = 0;
     204               1 :         sop[0].sem_flg = 0;
     205                 : 
     206                 :         /* . . . and increment it so it becomes non-zero . . . */
     207                 : 
     208               1 :         sop[1].sem_num = SYSVSEM_SETVAL;
     209               1 :         sop[1].sem_op  = 1;
     210               1 :         sop[1].sem_flg = SEM_UNDO;
     211                 : 
     212                 :         /* . . . and increment the usage count. */
     213                 : 
     214               1 :         sop[2].sem_num = SYSVSEM_USAGE;
     215               1 :         sop[2].sem_op  = 1;
     216               1 :         sop[2].sem_flg = SEM_UNDO;
     217               2 :         while (semop(semid, sop, 3) == -1) {
     218               0 :                 if (errno != EINTR) {
     219               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed acquiring SYSVSEM_SETVAL for key 0x%lx: %s", key, strerror(errno));
     220               0 :                         break;
     221                 :                 }
     222                 :         }
     223                 : 
     224                 :         /* Get the usage count. */
     225               1 :         count = semctl(semid, SYSVSEM_USAGE, GETVAL, NULL);
     226               1 :         if (count == -1) {
     227               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     228                 :         }
     229                 : 
     230                 :         /* If we are the only user, then take this opportunity to set the max. */
     231                 : 
     232               1 :         if (count == 1) {
     233                 : #if HAVE_SEMUN
     234                 :                 /* This is correct for Linux which has union semun. */
     235                 :                 union semun semarg;
     236               1 :                 semarg.val = max_acquire;
     237               1 :                 if (semctl(semid, SYSVSEM_SEM, SETVAL, semarg) == -1) {
     238               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     239                 :                 }
     240                 : #elif defined(SETVAL_WANTS_PTR)
     241                 :                 /* This is correct for Solaris 2.6 which does not have union semun. */
     242                 :                 if (semctl(semid, SYSVSEM_SEM, SETVAL, &max_acquire) == -1) {
     243                 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     244                 :                 }
     245                 : #else
     246                 :                 /* This works for i.e. AIX */
     247                 :                 if (semctl(semid, SYSVSEM_SEM, SETVAL, max_acquire) == -1) {
     248                 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%lx: %s", key, strerror(errno));
     249                 :                 }
     250                 : #endif
     251                 :         }
     252                 : 
     253                 :         /* Set semaphore 1 back to zero. */
     254                 : 
     255               1 :         sop[0].sem_num = SYSVSEM_SETVAL;
     256               1 :         sop[0].sem_op  = -1;
     257               1 :         sop[0].sem_flg = SEM_UNDO;
     258               2 :         while (semop(semid, sop, 1) == -1) {
     259               0 :                 if (errno != EINTR) {
     260               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed releasing SYSVSEM_SETVAL for key 0x%lx: %s", key, strerror(errno));
     261               0 :                         break;
     262                 :                 }
     263                 :         }
     264                 : 
     265               1 :         sem_ptr = (sysvsem_sem *) emalloc(sizeof(sysvsem_sem));
     266               1 :         sem_ptr->key   = key;
     267               1 :         sem_ptr->semid = semid;
     268               1 :         sem_ptr->count = 0;
     269               1 :         sem_ptr->auto_release = auto_release;
     270                 : 
     271               1 :         sem_ptr->id = ZEND_REGISTER_RESOURCE(return_value, sem_ptr, php_sysvsem_module.le_sem);
     272                 : }
     273                 : /* }}} */
     274                 : 
     275                 : /* {{{ php_sysvsem_semop
     276                 :  */
     277                 : static void php_sysvsem_semop(INTERNAL_FUNCTION_PARAMETERS, int acquire)
     278               2 : {
     279                 :         zval **arg_id;
     280                 :         sysvsem_sem *sem_ptr;
     281                 :         struct sembuf sop;
     282                 : 
     283               2 :         switch(ZEND_NUM_ARGS()) {
     284                 :                 case 1:
     285               2 :                         if (zend_get_parameters_ex(1, &arg_id)==FAILURE) {
     286               0 :                                 RETURN_FALSE;
     287                 :                         }
     288                 :                         break;
     289                 :                 default:
     290               0 :                         WRONG_PARAM_COUNT;
     291                 :                         break;
     292                 :         }
     293                 : 
     294               2 :         ZEND_FETCH_RESOURCE(sem_ptr, sysvsem_sem *, arg_id, -1, "SysV semaphore", php_sysvsem_module.le_sem);
     295                 : 
     296               2 :         if (!acquire && sem_ptr->count == 0) {
     297               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "SysV semaphore %ld (key 0x%x) is not currently acquired", Z_LVAL_PP(arg_id), sem_ptr->key);
     298               0 :                 RETURN_FALSE;
     299                 :         }
     300                 : 
     301               2 :         sop.sem_num = SYSVSEM_SEM;
     302               2 :         sop.sem_op  = acquire ? -1 : 1;
     303               2 :         sop.sem_flg = SEM_UNDO;
     304                 : 
     305               4 :         while (semop(sem_ptr->semid, &sop, 1) == -1) {
     306               0 :                 if (errno != EINTR) {
     307               0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to %s key 0x%x: %s", acquire ? "acquire" : "release", sem_ptr->key, strerror(errno));
     308               0 :                         RETURN_FALSE;
     309                 :                 }
     310                 :         }
     311                 : 
     312               2 :         sem_ptr->count -= acquire ? -1 : 1;
     313               2 :         RETURN_TRUE;
     314                 : }
     315                 : /* }}} */
     316                 : 
     317                 : /* {{{ proto bool sem_acquire(resource id)
     318                 :    Acquires the semaphore with the given id, blocking if necessary */
     319                 : PHP_FUNCTION(sem_acquire)
     320               1 : {
     321               1 :         php_sysvsem_semop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
     322               1 : }
     323                 : /* }}} */
     324                 : 
     325                 : /* {{{ proto bool sem_release(resource id)
     326                 :    Releases the semaphore with the given id */
     327                 : PHP_FUNCTION(sem_release)
     328               1 : {
     329               1 :         php_sysvsem_semop(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
     330               1 : }
     331                 : /* }}} */
     332                 : 
     333                 : /* {{{ proto bool sem_remove(resource id)
     334                 :    Removes semaphore from Unix systems */
     335                 : 
     336                 : /*
     337                 :  * contributed by Gavin Sherry gavin@linuxworld.com.au
     338                 :  * Fri Mar 16 00:50:13 EST 2001
     339                 :  */
     340                 : 
     341                 : PHP_FUNCTION(sem_remove)
     342               1 : {
     343                 :         zval **arg_id;
     344                 :         sysvsem_sem *sem_ptr;
     345                 : #if HAVE_SEMUN
     346                 :         union semun un;
     347                 :         struct semid_ds buf;
     348                 : #endif
     349                 : 
     350               1 :         if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_id) == FAILURE) {
     351               0 :                         WRONG_PARAM_COUNT;
     352                 :         }
     353                 : 
     354               1 :         ZEND_FETCH_RESOURCE(sem_ptr, sysvsem_sem *, arg_id, -1, "SysV semaphore", php_sysvsem_module.le_sem);
     355                 : 
     356                 : #if HAVE_SEMUN
     357               1 :         un.buf = &buf;
     358               1 :         if (semctl(sem_ptr->semid, 0, IPC_STAT, un) < 0) {
     359                 : #else
     360                 :         if (semctl(sem_ptr->semid, 0, IPC_STAT, NULL) < 0) {
     361                 : #endif
     362               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "SysV semaphore %ld does not (any longer) exist", Z_LVAL_PP(arg_id));
     363               0 :                 RETURN_FALSE;
     364                 :         }
     365                 : 
     366                 : #if HAVE_SEMUN
     367               1 :         if (semctl(sem_ptr->semid, 0, IPC_RMID, un) < 0) {
     368                 : #else
     369                 :         if (semctl(sem_ptr->semid, 0, IPC_RMID, NULL) < 0) {
     370                 : #endif
     371               0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for SysV sempphore %ld: %s", Z_LVAL_PP(arg_id), strerror(errno));
     372               0 :                 RETURN_FALSE;
     373                 :         }
     374                 :         
     375                 :         /* let release_sysvsem_sem know we have removed
     376                 :          * the semaphore to avoid issues with releasing.
     377                 :          */ 
     378                 : 
     379               1 :         sem_ptr->count = -1;
     380               1 :         RETURN_TRUE;
     381                 : }
     382                 : 
     383                 : /* }}} */
     384                 : 
     385                 : #endif /* HAVE_SYSVSEM */
     386                 : 
     387                 : /*
     388                 :  * Local variables:
     389                 :  * tab-width: 4
     390                 :  * c-basic-offset: 4
     391                 :  * End:
     392                 :  * vim600: sw=4 ts=4 fdm=marker
     393                 :  * vim<600: sw=4 ts=4
     394                 :  */

Generated by: LTP GCOV extension version 1.5

Generated at Thu, 19 Nov 2009 08:20:24 +0000 (5 days ago)

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