For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > July 2007 > semctl: Permission denied









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author semctl: Permission denied
redbox

2007-07-17, 4:19 am

i write a simply program about producer/consumer problem.when it run
in root user
,it is ok.
But when it run in other user ,occur a error. semctl: Permission
denied.
Part of my code as so :
key_t key=123;

if((semid=semget(key,2,IPC_CREAT))==0)
{
perror("semget");
exit(1);
}
if(semctl(semid,0,SETVAL,1))//for producer
{
perror("semctl");
exit(1);
}
Obviously ,there are some problem in semctl.

but i can't found anything about it .Tanks at first.

Jens Thoms Toerring

2007-07-17, 4:19 am

redbox <guojianlee@gmail.com> wrote:
> i write a simply program about producer/consumer problem.when it run
> in root user
> ,it is ok.
> But when it run in other user ,occur a error. semctl: Permission
> denied.
> Part of my code as so :
> key_t key=123;


> if((semid=semget(key,2,IPC_CREAT))==0)


a) The return value on failure is -1, not 0
b) You have to specify the permission bits together with IPC_CREAT.

> {
> perror("semget");
> exit(1);
> }
> if(semctl(semid,0,SETVAL,1))//for producer


Same here - semctl() returns a non-negative number on success,
and -1 on failure, so you need to test for -1 (or '< 0') to
catch errors.

> {
> perror("semctl");
> exit(1);
> }
> Obviously ,there are some problem in semctl.


Since it's not clear if you get an error at all (since you don't
check for the values that would indicate an error) it's hard to
even say if anything goes wrong. But then there probably are
problems even for the case where you are root user since you
don't set the access permissions for the semaphore - IPC_CREAT
normally seems to be defined to be 01000, so no-one has any
permissions for the semaphore. Finally, do you also delete the
semaphore? It won't go away just because your program ends,
if you don't delete it it will continue to exist until you
either delete it using e.g. the ipcrm utility or you reboot
your machine. And if you then want to re-open it as a diffe-
rent user than the one that created it this could lead to
problems if the access permissions bits aren't set correctly
for this case.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com