The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/sysv_sem.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* $FreeBSD$ */
    2 
    3 /*
    4  * Implementation of SVID semaphores
    5  *
    6  * Author:  Daniel Boulet
    7  *
    8  * This software is provided ``AS IS'' without any warranties of any kind.
    9  */
   10 
   11 #include "opt_sysvipc.h"
   12 
   13 #include <sys/param.h>
   14 #include <sys/systm.h>
   15 #include <sys/sysproto.h>
   16 #include <sys/kernel.h>
   17 #include <sys/proc.h>
   18 #include <sys/sem.h>
   19 #include <sys/sysent.h>
   20 #include <sys/sysctl.h>
   21 #include <sys/malloc.h>
   22 #include <sys/jail.h>
   23 
   24 static MALLOC_DEFINE(M_SEM, "sem", "SVID compatible semaphores");
   25 
   26 static void seminit __P((void *));
   27 
   28 #ifndef _SYS_SYSPROTO_H_
   29 struct __semctl_args;
   30 int __semctl __P((struct proc *p, struct __semctl_args *uap));
   31 struct semget_args;
   32 int semget __P((struct proc *p, struct semget_args *uap));
   33 struct semop_args;
   34 int semop __P((struct proc *p, struct semop_args *uap));
   35 #endif
   36 
   37 static struct sem_undo *semu_alloc __P((struct proc *p));
   38 static int semundo_adjust __P((struct proc *p, struct sem_undo **supptr, 
   39                 int semid, int semnum, int adjval));
   40 static void semundo_clear __P((int semid, int semnum));
   41 
   42 /* XXX casting to (sy_call_t *) is bogus, as usual. */
   43 static sy_call_t *semcalls[] = {
   44         (sy_call_t *)__semctl, (sy_call_t *)semget,
   45         (sy_call_t *)semop
   46 };
   47 
   48 static int      semtot = 0;
   49 static struct semid_ds *sema;   /* semaphore id pool */
   50 static struct sem *sem;         /* semaphore pool */
   51 static struct sem_undo *semu_list; /* list of active undo structures */
   52 static int      *semu;          /* undo structure pool */
   53 
   54 struct sem {
   55         u_short semval;         /* semaphore value */
   56         pid_t   sempid;         /* pid of last operation */
   57         u_short semncnt;        /* # awaiting semval > cval */
   58         u_short semzcnt;        /* # awaiting semval = 0 */
   59 };
   60 
   61 /*
   62  * Undo structure (one per process)
   63  */
   64 struct sem_undo {
   65         struct  sem_undo *un_next;      /* ptr to next active undo structure */
   66         struct  proc *un_proc;          /* owner of this structure */
   67         short   un_cnt;                 /* # of active entries */
   68         struct undo {
   69                 short   un_adjval;      /* adjust on exit values */
   70                 short   un_num;         /* semaphore # */
   71                 int     un_id;          /* semid */
   72         } un_ent[1];                    /* undo entries */
   73 };
   74 
   75 /*
   76  * Configuration parameters
   77  */
   78 #ifndef SEMMNI
   79 #define SEMMNI  10              /* # of semaphore identifiers */
   80 #endif
   81 #ifndef SEMMNS
   82 #define SEMMNS  60              /* # of semaphores in system */
   83 #endif
   84 #ifndef SEMUME
   85 #define SEMUME  10              /* max # of undo entries per process */
   86 #endif
   87 #ifndef SEMMNU
   88 #define SEMMNU  30              /* # of undo structures in system */
   89 #endif
   90 
   91 /* shouldn't need tuning */
   92 #ifndef SEMMAP
   93 #define SEMMAP  30              /* # of entries in semaphore map */
   94 #endif
   95 #ifndef SEMMSL
   96 #define SEMMSL  SEMMNS          /* max # of semaphores per id */
   97 #endif
   98 #ifndef SEMOPM
   99 #define SEMOPM  100             /* max # of operations per semop call */
  100 #endif
  101 
  102 #define SEMVMX  32767           /* semaphore maximum value */
  103 #define SEMAEM  16384           /* adjust on exit max value */
  104 
  105 /*
  106  * Due to the way semaphore memory is allocated, we have to ensure that
  107  * SEMUSZ is properly aligned.
  108  */
  109 
  110 #define SEM_ALIGN(bytes) (((bytes) + (sizeof(long) - 1)) & ~(sizeof(long) - 1))
  111 
  112 /* actual size of an undo structure */
  113 #define SEMUSZ  SEM_ALIGN(offsetof(struct sem_undo, un_ent[SEMUME]))
  114 
  115 /*
  116  * Macro to find a particular sem_undo vector
  117  */
  118 #define SEMU(ix)        ((struct sem_undo *)(((intptr_t)semu)+ix * seminfo.semusz))
  119 
  120 /*
  121  * semaphore info struct
  122  */
  123 struct seminfo seminfo = {
  124                 SEMMAP,         /* # of entries in semaphore map */
  125                 SEMMNI,         /* # of semaphore identifiers */
  126                 SEMMNS,         /* # of semaphores in system */
  127                 SEMMNU,         /* # of undo structures in system */
  128                 SEMMSL,         /* max # of semaphores per id */
  129                 SEMOPM,         /* max # of operations per semop call */
  130                 SEMUME,         /* max # of undo entries per process */
  131                 SEMUSZ,         /* size in bytes of undo structure */
  132                 SEMVMX,         /* semaphore maximum value */
  133                 SEMAEM          /* adjust on exit max value */
  134 };
  135 
  136 TUNABLE_INT("kern.ipc.semmap", &seminfo.semmap);
  137 TUNABLE_INT("kern.ipc.semmni", &seminfo.semmni);
  138 TUNABLE_INT("kern.ipc.semmns", &seminfo.semmns);
  139 TUNABLE_INT("kern.ipc.semmnu", &seminfo.semmnu);
  140 TUNABLE_INT("kern.ipc.semmsl", &seminfo.semmsl);
  141 TUNABLE_INT("kern.ipc.semopm", &seminfo.semopm);
  142 TUNABLE_INT("kern.ipc.semume", &seminfo.semume);
  143 TUNABLE_INT("kern.ipc.semusz", &seminfo.semusz);
  144 TUNABLE_INT("kern.ipc.semvmx", &seminfo.semvmx);
  145 TUNABLE_INT("kern.ipc.semaem", &seminfo.semaem);
  146 
  147 SYSCTL_DECL(_kern_ipc);
  148 SYSCTL_INT(_kern_ipc, OID_AUTO, semmap, CTLFLAG_RW, &seminfo.semmap, 0, "");
  149 SYSCTL_INT(_kern_ipc, OID_AUTO, semmni, CTLFLAG_RD, &seminfo.semmni, 0, "");
  150 SYSCTL_INT(_kern_ipc, OID_AUTO, semmns, CTLFLAG_RD, &seminfo.semmns, 0, "");
  151 SYSCTL_INT(_kern_ipc, OID_AUTO, semmnu, CTLFLAG_RD, &seminfo.semmnu, 0, "");
  152 SYSCTL_INT(_kern_ipc, OID_AUTO, semmsl, CTLFLAG_RW, &seminfo.semmsl, 0, "");
  153 SYSCTL_INT(_kern_ipc, OID_AUTO, semopm, CTLFLAG_RD, &seminfo.semopm, 0, "");
  154 SYSCTL_INT(_kern_ipc, OID_AUTO, semume, CTLFLAG_RD, &seminfo.semume, 0, "");
  155 SYSCTL_INT(_kern_ipc, OID_AUTO, semusz, CTLFLAG_RD, &seminfo.semusz, 0, "");
  156 SYSCTL_INT(_kern_ipc, OID_AUTO, semvmx, CTLFLAG_RW, &seminfo.semvmx, 0, "");
  157 SYSCTL_INT(_kern_ipc, OID_AUTO, semaem, CTLFLAG_RW, &seminfo.semaem, 0, "");
  158 
  159 #if 0
  160 RO seminfo.semmap       /* SEMMAP unused */
  161 RO seminfo.semmni
  162 RO seminfo.semmns
  163 RO seminfo.semmnu       /* undo entries per system */
  164 RW seminfo.semmsl
  165 RO seminfo.semopm       /* SEMOPM unused */
  166 RO seminfo.semume
  167 RO seminfo.semusz       /* param - derived from SEMUME for per-proc sizeof */
  168 RO seminfo.semvmx       /* SEMVMX unused - user param */
  169 RO seminfo.semaem       /* SEMAEM unused - user param */
  170 #endif
  171 
  172 static void
  173 seminit(dummy)
  174         void *dummy;
  175 {
  176         register int i;
  177 
  178         sem = malloc(sizeof(struct sem) * seminfo.semmns, M_SEM, M_WAITOK);
  179         if (sem == NULL)
  180                 panic("sem is NULL");
  181         sema = malloc(sizeof(struct semid_ds) * seminfo.semmni, M_SEM, M_WAITOK);
  182         if (sema == NULL)
  183                 panic("sema is NULL");
  184         semu = malloc(seminfo.semmnu * seminfo.semusz, M_SEM, M_WAITOK);
  185         if (semu == NULL)
  186                 panic("semu is NULL");
  187 
  188         for (i = 0; i < seminfo.semmni; i++) {
  189                 sema[i].sem_base = 0;
  190                 sema[i].sem_perm.mode = 0;
  191         }
  192         for (i = 0; i < seminfo.semmnu; i++) {
  193                 register struct sem_undo *suptr = SEMU(i);
  194                 suptr->un_proc = NULL;
  195         }
  196         semu_list = NULL;
  197 }
  198 SYSINIT(sysv_sem, SI_SUB_SYSV_SEM, SI_ORDER_FIRST, seminit, NULL)
  199 
  200 /*
  201  * Entry point for all SEM calls
  202  */
  203 int
  204 semsys(p, uap)
  205         struct proc *p;
  206         /* XXX actually varargs. */
  207         struct semsys_args /* {
  208                 u_int   which;
  209                 int     a2;
  210                 int     a3;
  211                 int     a4;
  212                 int     a5;
  213         } */ *uap;
  214 {
  215 
  216         if (!jail_sysvipc_allowed && p->p_prison != NULL)
  217                 return (ENOSYS);
  218 
  219         if (uap->which >= sizeof(semcalls)/sizeof(semcalls[0]))
  220                 return (EINVAL);
  221         return ((*semcalls[uap->which])(p, &uap->a2));
  222 }
  223 
  224 /*
  225  * Allocate a new sem_undo structure for a process
  226  * (returns ptr to structure or NULL if no more room)
  227  */
  228 
  229 static struct sem_undo *
  230 semu_alloc(p)
  231         struct proc *p;
  232 {
  233         register int i;
  234         register struct sem_undo *suptr;
  235         register struct sem_undo **supptr;
  236         int attempt;
  237 
  238         /*
  239          * Try twice to allocate something.
  240          * (we'll purge any empty structures after the first pass so
  241          * two passes are always enough)
  242          */
  243 
  244         for (attempt = 0; attempt < 2; attempt++) {
  245                 /*
  246                  * Look for a free structure.
  247                  * Fill it in and return it if we find one.
  248                  */
  249 
  250                 for (i = 0; i < seminfo.semmnu; i++) {
  251                         suptr = SEMU(i);
  252                         if (suptr->un_proc == NULL) {
  253                                 suptr->un_next = semu_list;
  254                                 semu_list = suptr;
  255                                 suptr->un_cnt = 0;
  256                                 suptr->un_proc = p;
  257                                 return(suptr);
  258                         }
  259                 }
  260 
  261                 /*
  262                  * We didn't find a free one, if this is the first attempt
  263                  * then try to free some structures.
  264                  */
  265 
  266                 if (attempt == 0) {
  267                         /* All the structures are in use - try to free some */
  268                         int did_something = 0;
  269 
  270                         supptr = &semu_list;
  271                         while ((suptr = *supptr) != NULL) {
  272                                 if (suptr->un_cnt == 0)  {
  273                                         suptr->un_proc = NULL;
  274                                         *supptr = suptr->un_next;
  275                                         did_something = 1;
  276                                 } else
  277                                         supptr = &(suptr->un_next);
  278                         }
  279 
  280                         /* If we didn't free anything then just give-up */
  281                         if (!did_something)
  282                                 return(NULL);
  283                 } else {
  284                         /*
  285                          * The second pass failed even though we freed
  286                          * something after the first pass!
  287                          * This is IMPOSSIBLE!
  288                          */
  289                         panic("semu_alloc - second attempt failed");
  290                 }
  291         }
  292         return (NULL);
  293 }
  294 
  295 /*
  296  * Adjust a particular entry for a particular proc
  297  */
  298 
  299 static int
  300 semundo_adjust(p, supptr, semid, semnum, adjval)
  301         register struct proc *p;
  302         struct sem_undo **supptr;
  303         int semid, semnum;
  304         int adjval;
  305 {
  306         register struct sem_undo *suptr;
  307         register struct undo *sunptr;
  308         int i;
  309 
  310         /* Look for and remember the sem_undo if the caller doesn't provide
  311            it */
  312 
  313         suptr = *supptr;
  314         if (suptr == NULL) {
  315                 for (suptr = semu_list; suptr != NULL;
  316                     suptr = suptr->un_next) {
  317                         if (suptr->un_proc == p) {
  318                                 *supptr = suptr;
  319                                 break;
  320                         }
  321                 }
  322                 if (suptr == NULL) {
  323                         if (adjval == 0)
  324                                 return(0);
  325                         suptr = semu_alloc(p);
  326                         if (suptr == NULL)
  327                                 return(ENOSPC);
  328                         *supptr = suptr;
  329                 }
  330         }
  331 
  332         /*
  333          * Look for the requested entry and adjust it (delete if adjval becomes
  334          * 0).
  335          */
  336         sunptr = &suptr->un_ent[0];
  337         for (i = 0; i < suptr->un_cnt; i++, sunptr++) {
  338                 if (sunptr->un_id != semid || sunptr->un_num != semnum)
  339                         continue;
  340                 if (adjval == 0)
  341                         sunptr->un_adjval = 0;
  342                 else
  343                         sunptr->un_adjval += adjval;
  344                 if (sunptr->un_adjval == 0) {
  345                         suptr->un_cnt--;
  346                         if (i < suptr->un_cnt)
  347                                 suptr->un_ent[i] =
  348                                     suptr->un_ent[suptr->un_cnt];
  349                 }
  350                 return(0);
  351         }
  352 
  353         /* Didn't find the right entry - create it */
  354         if (adjval == 0)
  355                 return(0);
  356         if (suptr->un_cnt != seminfo.semume) {
  357                 sunptr = &suptr->un_ent[suptr->un_cnt];
  358                 suptr->un_cnt++;
  359                 sunptr->un_adjval = adjval;
  360                 sunptr->un_id = semid; sunptr->un_num = semnum;
  361         } else
  362                 return(EINVAL);
  363         return(0);
  364 }
  365 
  366 static void
  367 semundo_clear(semid, semnum)
  368         int semid, semnum;
  369 {
  370         register struct sem_undo *suptr;
  371 
  372         for (suptr = semu_list; suptr != NULL; suptr = suptr->un_next) {
  373                 register struct undo *sunptr = &suptr->un_ent[0];
  374                 register int i = 0;
  375 
  376                 while (i < suptr->un_cnt) {
  377                         if (sunptr->un_id == semid) {
  378                                 if (semnum == -1 || sunptr->un_num == semnum) {
  379                                         suptr->un_cnt--;
  380                                         if (i < suptr->un_cnt) {
  381                                                 suptr->un_ent[i] =
  382                                                   suptr->un_ent[suptr->un_cnt];
  383                                                 continue;
  384                                         }
  385                                 }
  386                                 if (semnum != -1)
  387                                         break;
  388                         }
  389                         i++, sunptr++;
  390                 }
  391         }
  392 }
  393 
  394 /*
  395  * Note that the user-mode half of this passes a union, not a pointer
  396  */
  397 #ifndef _SYS_SYSPROTO_H_
  398 struct __semctl_args {
  399         int     semid;
  400         int     semnum;
  401         int     cmd;
  402         union   semun *arg;
  403 };
  404 #endif
  405 
  406 int
  407 __semctl(p, uap)
  408         struct proc *p;
  409         register struct __semctl_args *uap;
  410 {
  411         int semid = uap->semid;
  412         int semnum = uap->semnum;
  413         int cmd = uap->cmd;
  414         union semun *arg = uap->arg;
  415         union semun real_arg;
  416         struct ucred *cred = p->p_ucred;
  417         int i, rval, eval;
  418         struct semid_ds sbuf;
  419         register struct semid_ds *semaptr;
  420 
  421 #ifdef SEM_DEBUG
  422         printf("call to semctl(%d, %d, %d, 0x%x)\n", semid, semnum, cmd, arg);
  423 #endif
  424 
  425         if (!jail_sysvipc_allowed && p->p_prison != NULL)
  426                 return (ENOSYS);
  427 
  428         semid = IPCID_TO_IX(semid);
  429         if (semid < 0 || semid >= seminfo.semmni)
  430                 return(EINVAL);
  431 
  432         semaptr = &sema[semid];
  433         if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
  434             semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
  435                 return(EINVAL);
  436 
  437         eval = 0;
  438         rval = 0;
  439 
  440         switch (cmd) {
  441         case IPC_RMID:
  442                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_M)))
  443                         return(eval);
  444                 semaptr->sem_perm.cuid = cred->cr_uid;
  445                 semaptr->sem_perm.uid = cred->cr_uid;
  446                 semtot -= semaptr->sem_nsems;
  447                 for (i = semaptr->sem_base - sem; i < semtot; i++)
  448                         sem[i] = sem[i + semaptr->sem_nsems];
  449                 for (i = 0; i < seminfo.semmni; i++) {
  450                         if ((sema[i].sem_perm.mode & SEM_ALLOC) &&
  451                             sema[i].sem_base > semaptr->sem_base)
  452                                 sema[i].sem_base -= semaptr->sem_nsems;
  453                 }
  454                 semaptr->sem_perm.mode = 0;
  455                 semundo_clear(semid, -1);
  456                 wakeup((caddr_t)semaptr);
  457                 break;
  458 
  459         case IPC_SET:
  460                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_M)))
  461                         return(eval);
  462                 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  463                         return(eval);
  464                 if ((eval = copyin(real_arg.buf, (caddr_t)&sbuf,
  465                     sizeof(sbuf))) != 0)
  466                         return(eval);
  467                 semaptr->sem_perm.uid = sbuf.sem_perm.uid;
  468                 semaptr->sem_perm.gid = sbuf.sem_perm.gid;
  469                 semaptr->sem_perm.mode = (semaptr->sem_perm.mode & ~0777) |
  470                     (sbuf.sem_perm.mode & 0777);
  471                 semaptr->sem_ctime = time_second;
  472                 break;
  473 
  474         case IPC_STAT:
  475                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
  476                         return(eval);
  477                 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  478                         return(eval);
  479                 eval = copyout((caddr_t)semaptr, real_arg.buf,
  480                     sizeof(struct semid_ds));
  481                 break;
  482 
  483         case GETNCNT:
  484                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
  485                         return(eval);
  486                 if (semnum < 0 || semnum >= semaptr->sem_nsems)
  487                         return(EINVAL);
  488                 rval = semaptr->sem_base[semnum].semncnt;
  489                 break;
  490 
  491         case GETPID:
  492                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
  493                         return(eval);
  494                 if (semnum < 0 || semnum >= semaptr->sem_nsems)
  495                         return(EINVAL);
  496                 rval = semaptr->sem_base[semnum].sempid;
  497                 break;
  498 
  499         case GETVAL:
  500                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
  501                         return(eval);
  502                 if (semnum < 0 || semnum >= semaptr->sem_nsems)
  503                         return(EINVAL);
  504                 rval = semaptr->sem_base[semnum].semval;
  505                 break;
  506 
  507         case GETALL:
  508                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
  509                         return(eval);
  510                 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  511                         return(eval);
  512                 for (i = 0; i < semaptr->sem_nsems; i++) {
  513                         eval = copyout((caddr_t)&semaptr->sem_base[i].semval,
  514                             &real_arg.array[i], sizeof(real_arg.array[0]));
  515                         if (eval != 0)
  516                                 break;
  517                 }
  518                 break;
  519 
  520         case GETZCNT:
  521                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_R)))
  522                         return(eval);
  523                 if (semnum < 0 || semnum >= semaptr->sem_nsems)
  524                         return(EINVAL);
  525                 rval = semaptr->sem_base[semnum].semzcnt;
  526                 break;
  527 
  528         case SETVAL:
  529                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W)))
  530                         return(eval);
  531                 if (semnum < 0 || semnum >= semaptr->sem_nsems)
  532                         return(EINVAL);
  533                 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  534                         return(eval);
  535                 semaptr->sem_base[semnum].semval = real_arg.val;
  536                 semundo_clear(semid, semnum);
  537                 wakeup((caddr_t)semaptr);
  538                 break;
  539 
  540         case SETALL:
  541                 if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W)))
  542                         return(eval);
  543                 if ((eval = copyin(arg, &real_arg, sizeof(real_arg))) != 0)
  544                         return(eval);
  545                 for (i = 0; i < semaptr->sem_nsems; i++) {
  546                         eval = copyin(&real_arg.array[i],
  547                             (caddr_t)&semaptr->sem_base[i].semval,
  548                             sizeof(real_arg.array[0]));
  549                         if (eval != 0)
  550                                 break;
  551                 }
  552                 semundo_clear(semid, -1);
  553                 wakeup((caddr_t)semaptr);
  554                 break;
  555 
  556         default:
  557                 return(EINVAL);
  558         }
  559 
  560         if (eval == 0)
  561                 p->p_retval[0] = rval;
  562         return(eval);
  563 }
  564 
  565 #ifndef _SYS_SYSPROTO_H_
  566 struct semget_args {
  567         key_t   key;
  568         int     nsems;
  569         int     semflg;
  570 };
  571 #endif
  572 
  573 int
  574 semget(p, uap)
  575         struct proc *p;
  576         register struct semget_args *uap;
  577 {
  578         int semid, eval;
  579         int key = uap->key;
  580         int nsems = uap->nsems;
  581         int semflg = uap->semflg;
  582         struct ucred *cred = p->p_ucred;
  583 
  584 #ifdef SEM_DEBUG
  585         printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);
  586 #endif
  587 
  588         if (!jail_sysvipc_allowed && p->p_prison != NULL)
  589                 return (ENOSYS);
  590 
  591         if (key != IPC_PRIVATE) {
  592                 for (semid = 0; semid < seminfo.semmni; semid++) {
  593                         if ((sema[semid].sem_perm.mode & SEM_ALLOC) &&
  594                             sema[semid].sem_perm.key == key)
  595                                 break;
  596                 }
  597                 if (semid < seminfo.semmni) {
  598 #ifdef SEM_DEBUG
  599                         printf("found public key\n");
  600 #endif
  601                         if ((eval = ipcperm(p, &sema[semid].sem_perm,
  602                             semflg & 0700)))
  603                                 return(eval);
  604                         if (nsems > 0 && sema[semid].sem_nsems < nsems) {
  605 #ifdef SEM_DEBUG
  606                                 printf("too small\n");
  607 #endif
  608                                 return(EINVAL);
  609                         }
  610                         if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {
  611 #ifdef SEM_DEBUG
  612                                 printf("not exclusive\n");
  613 #endif
  614                                 return(EEXIST);
  615                         }
  616                         goto found;
  617                 }
  618         }
  619 
  620 #ifdef SEM_DEBUG
  621         printf("need to allocate the semid_ds\n");
  622 #endif
  623         if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {
  624                 if (nsems <= 0 || nsems > seminfo.semmsl) {
  625 #ifdef SEM_DEBUG
  626                         printf("nsems out of range (0<%d<=%d)\n", nsems,
  627                             seminfo.semmsl);
  628 #endif
  629                         return(EINVAL);
  630                 }
  631                 if (nsems > seminfo.semmns - semtot) {
  632 #ifdef SEM_DEBUG
  633                         printf("not enough semaphores left (need %d, got %d)\n",
  634                             nsems, seminfo.semmns - semtot);
  635 #endif
  636                         return(ENOSPC);
  637                 }
  638                 for (semid = 0; semid < seminfo.semmni; semid++) {
  639                         if ((sema[semid].sem_perm.mode & SEM_ALLOC) == 0)
  640                                 break;
  641                 }
  642                 if (semid == seminfo.semmni) {
  643 #ifdef SEM_DEBUG
  644                         printf("no more semid_ds's available\n");
  645 #endif
  646                         return(ENOSPC);
  647                 }
  648 #ifdef SEM_DEBUG
  649                 printf("semid %d is available\n", semid);
  650 #endif
  651                 sema[semid].sem_perm.key = key;
  652                 sema[semid].sem_perm.cuid = cred->cr_uid;
  653                 sema[semid].sem_perm.uid = cred->cr_uid;
  654                 sema[semid].sem_perm.cgid = cred->cr_gid;
  655                 sema[semid].sem_perm.gid = cred->cr_gid;
  656                 sema[semid].sem_perm.mode = (semflg & 0777) | SEM_ALLOC;
  657                 sema[semid].sem_perm.seq =
  658                     (sema[semid].sem_perm.seq + 1) & 0x7fff;
  659                 sema[semid].sem_nsems = nsems;
  660                 sema[semid].sem_otime = 0;
  661                 sema[semid].sem_ctime = time_second;
  662                 sema[semid].sem_base = &sem[semtot];
  663                 semtot += nsems;
  664                 bzero(sema[semid].sem_base,
  665                     sizeof(sema[semid].sem_base[0])*nsems);
  666 #ifdef SEM_DEBUG
  667                 printf("sembase = 0x%x, next = 0x%x\n", sema[semid].sem_base,
  668                     &sem[semtot]);
  669 #endif
  670         } else {
  671 #ifdef SEM_DEBUG
  672                 printf("didn't find it and wasn't asked to create it\n");
  673 #endif
  674                 return(ENOENT);
  675         }
  676 
  677 found:
  678         p->p_retval[0] = IXSEQ_TO_IPCID(semid, sema[semid].sem_perm);
  679         return(0);
  680 }
  681 
  682 #ifndef _SYS_SYSPROTO_H_
  683 struct semop_args {
  684         int     semid;
  685         struct  sembuf *sops;
  686         u_int   nsops;
  687 };
  688 #endif
  689 
  690 int
  691 semop(p, uap)
  692         struct proc *p;
  693         register struct semop_args *uap;
  694 {
  695         int semid = uap->semid;
  696         u_int nsops = uap->nsops;
  697         struct sembuf sops[MAX_SOPS];
  698         register struct semid_ds *semaptr;
  699         register struct sembuf *sopptr;
  700         register struct sem *semptr;
  701         struct sem_undo *suptr = NULL;
  702         int i, j, eval;
  703         int do_wakeup, do_undos;
  704 
  705 #ifdef SEM_DEBUG
  706         printf("call to semop(%d, 0x%x, %u)\n", semid, sops, nsops);
  707 #endif
  708 
  709         if (!jail_sysvipc_allowed && p->p_prison != NULL)
  710                 return (ENOSYS);
  711 
  712         semid = IPCID_TO_IX(semid);     /* Convert back to zero origin */
  713 
  714         if (semid < 0 || semid >= seminfo.semmni)
  715                 return(EINVAL);
  716 
  717         semaptr = &sema[semid];
  718         if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
  719                 return(EINVAL);
  720         if (semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
  721                 return(EINVAL);
  722 
  723         if ((eval = ipcperm(p, &semaptr->sem_perm, IPC_W))) {
  724 #ifdef SEM_DEBUG
  725                 printf("eval = %d from ipaccess\n", eval);
  726 #endif
  727                 return(eval);
  728         }
  729 
  730         if (nsops > MAX_SOPS) {
  731 #ifdef SEM_DEBUG
  732                 printf("too many sops (max=%d, nsops=%u)\n", MAX_SOPS, nsops);
  733 #endif
  734                 return(E2BIG);
  735         }
  736 
  737         if ((eval = copyin(uap->sops, &sops, nsops * sizeof(sops[0]))) != 0) {
  738 #ifdef SEM_DEBUG
  739                 printf("eval = %d from copyin(%08x, %08x, %u)\n", eval,
  740                     uap->sops, &sops, nsops * sizeof(sops[0]));
  741 #endif
  742                 return(eval);
  743         }
  744 
  745         /*
  746          * Loop trying to satisfy the vector of requests.
  747          * If we reach a point where we must wait, any requests already
  748          * performed are rolled back and we go to sleep until some other
  749          * process wakes us up.  At this point, we start all over again.
  750          *
  751          * This ensures that from the perspective of other tasks, a set
  752          * of requests is atomic (never partially satisfied).
  753          */
  754         do_undos = 0;
  755 
  756         for (;;) {
  757                 do_wakeup = 0;
  758 
  759                 for (i = 0; i < nsops; i++) {
  760                         sopptr = &sops[i];
  761 
  762                         if (sopptr->sem_num >= semaptr->sem_nsems)
  763                                 return(EFBIG);
  764 
  765                         semptr = &semaptr->sem_base[sopptr->sem_num];
  766 
  767 #ifdef SEM_DEBUG
  768                         printf("semop:  semaptr=%x, sem_base=%x, semptr=%x, sem[%d]=%d : op=%d, flag=%s\n",
  769                             semaptr, semaptr->sem_base, semptr,
  770                             sopptr->sem_num, semptr->semval, sopptr->sem_op,
  771                             (sopptr->sem_flg & IPC_NOWAIT) ? "nowait" : "wait");
  772 #endif
  773 
  774                         if (sopptr->sem_op < 0) {
  775                                 if (semptr->semval + sopptr->sem_op < 0) {
  776 #ifdef SEM_DEBUG
  777                                         printf("semop:  can't do it now\n");
  778 #endif
  779                                         break;
  780                                 } else {
  781                                         semptr->semval += sopptr->sem_op;
  782                                         if (semptr->semval == 0 &&
  783                                             semptr->semzcnt > 0)
  784                                                 do_wakeup = 1;
  785                                 }
  786                                 if (sopptr->sem_flg & SEM_UNDO)
  787                                         do_undos = 1;
  788                         } else if (sopptr->sem_op == 0) {
  789                                 if (semptr->semval > 0) {
  790 #ifdef SEM_DEBUG
  791                                         printf("semop:  not zero now\n");
  792 #endif
  793                                         break;
  794                                 }
  795                         } else {
  796                                 if (semptr->semncnt > 0)
  797                                         do_wakeup = 1;
  798                                 semptr->semval += sopptr->sem_op;
  799                                 if (sopptr->sem_flg & SEM_UNDO)
  800                                         do_undos = 1;
  801                         }
  802                 }
  803 
  804                 /*
  805                  * Did we get through the entire vector?
  806                  */
  807                 if (i >= nsops)
  808                         goto done;
  809 
  810                 /*
  811                  * No ... rollback anything that we've already done
  812                  */
  813 #ifdef SEM_DEBUG
  814                 printf("semop:  rollback 0 through %d\n", i-1);
  815 #endif
  816                 for (j = 0; j < i; j++)
  817                         semaptr->sem_base[sops[j].sem_num].semval -=
  818                             sops[j].sem_op;
  819 
  820                 /*
  821                  * If the request that we couldn't satisfy has the
  822                  * NOWAIT flag set then return with EAGAIN.
  823                  */
  824                 if (sopptr->sem_flg & IPC_NOWAIT)
  825                         return(EAGAIN);
  826 
  827                 if (sopptr->sem_op == 0)
  828                         semptr->semzcnt++;
  829                 else
  830                         semptr->semncnt++;
  831 
  832 #ifdef SEM_DEBUG
  833                 printf("semop:  good night!\n");
  834 #endif
  835                 eval = tsleep((caddr_t)semaptr, (PZERO - 4) | PCATCH,
  836                     "semwait", 0);
  837                 /* return code is checked below, after sem[nz]cnt-- */
  838 #ifdef SEM_DEBUG
  839                 printf("semop:  good morning (eval=%d)!\n", eval);
  840 #endif
  841 
  842                 suptr = NULL;   /* sem_undo may have been reallocated */
  843 
  844                 /*
  845                  * Make sure that the semaphore still exists
  846                  */
  847                 if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0 ||
  848                     semaptr->sem_perm.seq != IPCID_TO_SEQ(uap->semid))
  849                         return(EIDRM);
  850 
  851                 /*
  852                  * The semaphore is still alive.  Readjust the count of
  853                  * waiting processes.
  854                  */
  855                 if (sopptr->sem_op == 0)
  856                         semptr->semzcnt--;
  857                 else
  858                         semptr->semncnt--;
  859 
  860                 /*
  861                  * Is it really morning, or was our sleep interrupted?
  862                  * (Delayed check of msleep() return code because we
  863                  * need to decrement sem[nz]cnt either way.)
  864                  */
  865                 if (eval != 0)
  866                         return(EINTR);
  867 #ifdef SEM_DEBUG
  868                 printf("semop:  good morning!\n");
  869 #endif
  870         }
  871 
  872 done:
  873         /*
  874          * Process any SEM_UNDO requests.
  875          */
  876         if (do_undos) {
  877                 for (i = 0; i < nsops; i++) {
  878                         /*
  879                          * We only need to deal with SEM_UNDO's for non-zero
  880                          * op's.
  881                          */
  882                         int adjval;
  883 
  884                         if ((sops[i].sem_flg & SEM_UNDO) == 0)
  885                                 continue;
  886                         adjval = sops[i].sem_op;
  887                         if (adjval == 0)
  888                                 continue;
  889                         eval = semundo_adjust(p, &suptr, semid,
  890                             sops[i].sem_num, -adjval);
  891                         if (eval == 0)
  892                                 continue;
  893 
  894                         /*
  895                          * Oh-Oh!  We ran out of either sem_undo's or undo's.
  896                          * Rollback the adjustments to this point and then
  897                          * rollback the semaphore ups and down so we can return
  898                          * with an error with all structures restored.  We
  899                          * rollback the undo's in the exact reverse order that
  900                          * we applied them.  This guarantees that we won't run
  901                          * out of space as we roll things back out.
  902                          */
  903                         for (j = i - 1; j >= 0; j--) {
  904                                 if ((sops[j].sem_flg & SEM_UNDO) == 0)
  905                                         continue;
  906                                 adjval = sops[j].sem_op;
  907                                 if (adjval == 0)
  908                                         continue;
  909                                 if (semundo_adjust(p, &suptr, semid,
  910                                     sops[j].sem_num, adjval) != 0)
  911                                         panic("semop - can't undo undos");
  912                         }
  913 
  914                         for (j = 0; j < nsops; j++)
  915                                 semaptr->sem_base[sops[j].sem_num].semval -=
  916                                     sops[j].sem_op;
  917 
  918 #ifdef SEM_DEBUG
  919                         printf("eval = %d from semundo_adjust\n", eval);
  920 #endif
  921                         return(eval);
  922                 } /* loop through the sops */
  923         } /* if (do_undos) */
  924 
  925         /* We're definitely done - set the sempid's */
  926         for (i = 0; i < nsops; i++) {
  927                 sopptr = &sops[i];
  928                 semptr = &semaptr->sem_base[sopptr->sem_num];
  929                 semptr->sempid = p->p_pid;
  930         }
  931 
  932         /* Do a wakeup if any semaphore was up'd. */
  933         if (do_wakeup) {
  934 #ifdef SEM_DEBUG
  935                 printf("semop:  doing wakeup\n");
  936 #endif
  937                 wakeup((caddr_t)semaptr);
  938 #ifdef SEM_DEBUG
  939                 printf("semop:  back from wakeup\n");
  940 #endif
  941         }
  942 #ifdef SEM_DEBUG
  943         printf("semop:  done\n");
  944 #endif
  945         p->p_retval[0] = 0;
  946         return(0);
  947 }
  948 
  949 /*
  950  * Go through the undo structures for this process and apply the adjustments to
  951  * semaphores.
  952  */
  953 void
  954 semexit(p)
  955         struct proc *p;
  956 {
  957         register struct sem_undo *suptr;
  958         register struct sem_undo **supptr;
  959         int did_something;
  960 
  961         did_something = 0;
  962 
  963         /*
  964          * Go through the chain of undo vectors looking for one
  965          * associated with this process.
  966          */
  967 
  968         for (supptr = &semu_list; (suptr = *supptr) != NULL;
  969             supptr = &suptr->un_next) {
  970                 if (suptr->un_proc == p)
  971                         break;
  972         }
  973 
  974         if (suptr == NULL)
  975                 return;
  976 
  977 #ifdef SEM_DEBUG
  978         printf("proc @%08x has undo structure with %d entries\n", p,
  979             suptr->un_cnt);
  980 #endif
  981 
  982         /*
  983          * If there are any active undo elements then process them.
  984          */
  985         if (suptr->un_cnt > 0) {
  986                 int ix;
  987 
  988                 for (ix = 0; ix < suptr->un_cnt; ix++) {
  989                         int semid = suptr->un_ent[ix].un_id;
  990                         int semnum = suptr->un_ent[ix].un_num;
  991                         int adjval = suptr->un_ent[ix].un_adjval;
  992                         struct semid_ds *semaptr;
  993 
  994                         semaptr = &sema[semid];
  995                         if ((semaptr->sem_perm.mode & SEM_ALLOC) == 0)
  996                                 panic("semexit - semid not allocated");
  997                         if (semnum >= semaptr->sem_nsems)
  998                                 panic("semexit - semnum out of range");
  999 
 1000 #ifdef SEM_DEBUG
 1001                         printf("semexit:  %08x id=%d num=%d(adj=%d) ; sem=%d\n",
 1002                             suptr->un_proc, suptr->un_ent[ix].un_id,
 1003                             suptr->un_ent[ix].un_num,
 1004                             suptr->un_ent[ix].un_adjval,
 1005                             semaptr->sem_base[semnum].semval);
 1006 #endif
 1007 
 1008                         if (adjval < 0) {
 1009                                 if (semaptr->sem_base[semnum].semval < -adjval)
 1010                                         semaptr->sem_base[semnum].semval = 0;
 1011                                 else
 1012                                         semaptr->sem_base[semnum].semval +=
 1013                                             adjval;
 1014                         } else
 1015                                 semaptr->sem_base[semnum].semval += adjval;
 1016 
 1017                         wakeup((caddr_t)semaptr);
 1018 #ifdef SEM_DEBUG
 1019                         printf("semexit:  back from wakeup\n");
 1020 #endif
 1021                 }
 1022         }
 1023 
 1024         /*
 1025          * Deallocate the undo vector.
 1026          */
 1027 #ifdef SEM_DEBUG
 1028         printf("removing vector\n");
 1029 #endif
 1030         suptr->un_proc = NULL;
 1031         *supptr = suptr->un_next;
 1032 }

Cache object: 3109dbe9ac129171c1e496453dffe0ef


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.