DPRINTF(("not enough semaphores left (need %d, got %d)\n", nsems, seminfo.semmns - semtot));
sys_semget(struct proc *p, void *v, register_t *retval){	struct sys_semget_args /* {		syscallarg(key_t) key;		syscallarg(int) nsems;		syscallarg(int) semflg;	} */ *uap = v;	int semid, error;	int key = SCARG(uap, key);	int nsems = SCARG(uap, nsems);	int semflg = SCARG(uap, semflg);	struct semid_ds *semaptr, *semaptr_new = NULL;	struct ucred *cred = p->p_ucred;#ifdef SEM_DEBUG	printf("semget(0x%x, %d, 0%o)\n", key, nsems, semflg);#endif	/*	 * Preallocate space for the new semaphore.  If we are going	 * to sleep, we want to sleep now to elliminate any race	 * condition in allocating a semaphore with a specific key.	 */	if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {		semaptr_new = pool_get(&sema_pool, PR_WAITOK);		semaptr_new->sem_base = malloc(nsems * sizeof(struct sem),		    M_SEM, M_WAITOK);		bzero(semaptr_new->sem_base, nsems * sizeof(struct sem));	}	if (key != IPC_PRIVATE) {		for (semid = 0, semaptr = NULL; semid < seminfo.semmni; semid++) {			if ((semaptr = sema[semid]) != NULL &&			    semaptr->sem_perm.key == key)				break;		}		if (semaptr != NULL) {#ifdef SEM_DEBUG			printf("found public key\n");#endif			if ((error = ipcperm(cred, &semaptr->sem_perm,			    semflg & 0700)))				goto error;			if (nsems > 0 && semaptr->sem_nsems < nsems) {#ifdef SEM_DEBUG				printf("too small\n");#endif				error = EINVAL;				goto error;			}			if ((semflg & IPC_CREAT) && (semflg & IPC_EXCL)) {#ifdef SEM_DEBUG				printf("not exclusive\n");#endif				error = EEXIST;				goto error;			}			goto found;		}	}#ifdef SEM_DEBUG	printf("need to allocate the semid_ds\n");#endif	if (key == IPC_PRIVATE || (semflg & IPC_CREAT)) {		if (nsems <= 0 || nsems > seminfo.semmsl) {#ifdef SEM_DEBUG			printf("nsems out of range (0<%d<=%d)\n", nsems,			    seminfo.semmsl);#endif			error = EINVAL;			goto error;		}		if (nsems > seminfo.semmns - semtot) {#ifdef SEM_DEBUG			printf("not enough semaphores left (need %d, got %d)\n",			    nsems, seminfo.semmns - semtot);#endif			error = ENOSPC;			goto error;		}		for (semid = 0; semid < seminfo.semmni; semid++) {			if ((semaptr = sema[semid]) == NULL)				break;		}		if (semid == seminfo.semmni) {#ifdef SEM_DEBUG			printf("no more semid_ds's available\n");#endif			error = ENOSPC;			goto error;		}#ifdef SEM_DEBUG		printf("semid %d is available\n", semid);#endif		semaptr_new->sem_perm.key = key;		semaptr_new->sem_perm.cuid = cred->cr_uid;		semaptr_new->sem_perm.uid = cred->cr_uid;		semaptr_new->sem_perm.cgid = cred->cr_gid;		semaptr_new->sem_perm.gid = cred->cr_gid;		semaptr_new->sem_perm.mode = (semflg & 0777);		semaptr_new->sem_perm.seq = semseqs[semid] =		    (semseqs[semid] + 1) & 0x7fff;		semaptr_new->sem_nsems = nsems;		semaptr_new->sem_otime = 0;		semaptr_new->sem_ctime = time.tv_sec;		sema[semid] = semaptr_new;	} else {#ifdef SEM_DEBUG		printf("didn't find it and wasn't asked to create it\n");#endif		return (ENOENT);	}found:	*retval = IXSEQ_TO_IPCID(semid, sema[semid]->sem_perm);	return (0);error:	if (semaptr_new != NULL) {		free(semaptr_new->sem_base, M_SEM);		pool_put(&sema_pool, semaptr_new);	}	return (error);}