3, semop系统调用
最终都调用semtimedop系统调用实现,
SYSCALL_DEFINE4(semtimedop,int, semid, structsembuf __user *, tsops,
unsigned,nsops, const structtimespec __user *, timeout)
{
interror = -EINVAL;
structsem_array *sma;
structsembuf fast_sops[SEMOPM_FAST];
structsembuf* sops = fast_sops, *sop;
structsem_undo *un;
intundos = 0, alter = 0, max;
structsem_queue queue;
unsignedlong jiffies_left = 0;
structipc_namespace *ns;
ns = current->nsproxy->ipc_ns;
if(nsops < 1 || semid < 0)
return-EINVAL;
if(nsops > ns->sc_semopm)
return-E2BIG;
if(nsops> SEMOPM_FAST) {
sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
if(sops==NULL)
return -ENOMEM;
}
if(copy_from_user (sops, tsops, nsops * sizeof(*tsops))){
error=-EFAULT;
gotoout_free;
}
if(timeout) {
structtimespec _timeout;
if(copy_from_user(&_timeout, timeout, sizeof(*timeout))){
error = -EFAULT;
goto out_free;
}
if(_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
_timeout.tv_nsec>= 1000000000L){
error = -EINVAL;
goto out_free;
}
jiffies_left =timespec_to_jiffies(&_timeout);
}
max = 0;
for(sop = sops; sop < sops + nsops; sop++) {
if(sop->sem_num >= max)
max =sop->sem_num;
if(sop->sem_flg & SEM_UNDO)
undos = 1;
if(sop->sem_op != 0)
alter = 1;
}
if(undos) {
un = find_alloc_undo(ns,semid);
if(IS_ERR(un)) {
error = PTR_ERR(un);
goto out_free;
}
} else
un = NULL;
sma = sem_lock_check(ns, semid);
if(IS_ERR(sma)) {
if(un)
rcu_read_unlock();
error = PTR_ERR(sma);
gotoout_free;
}
/*
* semid identifiers are not unique - find_alloc_undomay have
* allocated an undo structure, it wasinvalidated by an RMID
* and now a new array with received the sameid. Check and fail.
* This case can be detected checkingun->semid. The existance of
* "un" itself is guaranteed by rcu.
*/
error = -EIDRM;
if (un){
if(un->semid == -1) {
rcu_read_unlock();
goto out_unlock_free;
} else{
/*
* rcu lock can be released, "un"cannot disappear:
* - sem_lock is acquired, thus IPC_RMID is
* impossible.
* - exit_sem is impossible, it always operateson
* current (or a dead task).
*/
rcu_read_unlock();
}
}
error = -EFBIG;
if (max>= sma->sem_nsems)
gotoout_unlock_free;
error = -EACCES;
if(ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
gotoout_unlock_free;
error = security_sem_semop(sma, sops,nsops, alter);
if(error)
gotoout_unlock_free;
error = try_atomic_semop (sma, sops,nsops, un, task_tgid_vnr(current));
if(error <= 0) {
if(alter && error == 0)
update_queue (sma);
gotoout_unlock_free;
}
/* We need tosleep on this operation, so we put the current
* task into the pending queue and go to sleep.
*/
queue.sops = sops;
queue.nsops = nsops;
queue.undo = un;
queue.pid = task_tgid_vnr(current);
queue.alter = alter;
if(alter)
list_add_tail(&queue.list,&sma->sem_pending);
else
list_add(&queue.list,&sma->sem_pending);
queue.status = -EINTR;
queue.sleeper = current;
current->state = TASK_INTERRUPTIBLE;
sem_unlock(sma);
if(timeout)
jiffies_left =schedule_timeout(jiffies_left);
else
schedule();
error = queue.status;
while(unlikely(error== IN_WAKEUP)) {
cpu_relax();
error = queue.status;
}
if(error != -EINTR) {
/*fast path: update_queue already obtained all requested
* resources */
gotoout_free;
}
sma = sem_lock(ns, semid);
if(IS_ERR(sma)) {
error = -EIDRM;
gotoout_free;
}
/*
* If queue.status != -EINTR we are woken up byanother process
*/
error = queue.status;
if(error != -EINTR) {
gotoout_unlock_free;
}
/*
* If an interrupt occurred we have to clean upthe queue
*/
if(timeout && jiffies_left == 0)
error = -EAGAIN;
list_del(&queue.list);
out_unlock_free:
sem_unlock(sma);
out_free:
if(sops!= fast_sops)
kfree(sops);
returnerror;
}
Semtimedop系统调用首先从用户空间得到参数值,然后进行权限检查,调用try_atomic_semop函数对sem的semval进行设置,如果需要休眠(semval值为负),将该sem_queue项初始化并添加到待决操作列表,然后重新进行调度,否则调用update_queue对队列其他项进行操作和进程唤醒。
