LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Passing groupadd to execv() returned error msg: "file already locked." (http://www.linuxquestions.org/questions/programming-9/passing-groupadd-to-execv-returned-error-msg-file-already-locked-329703/)

walwali 06-02-2005 06:04 PM

Passing groupadd to execv() returned error msg: "file already locked."
 
I am writing a program in linux to add group. The program calls execv("/usr/sbin/groupadd", "newGroupName"); This program failed returning the following error message: "Cannot lock group file: already locked." When I ran the "/usr/sbin/groupadd" command with "newGroupName" argument from the shell it worked and the newGroupName was created. I ran the new program with root privilages. Any idea why my program failed and which "group file" the error message is referring to?

walwali 06-02-2005 06:09 PM

hmmm

anandt4u 06-02-2005 06:25 PM

Re: Passing groupadd to execv() returned error msg: "file already locked."
 
Quote:

Originally posted by walwali
I am writing a program in linux to add group. The program calls execv("/usr/sbin/groupadd", "newGroupName"); This program failed returning the following error message: "Cannot lock group file: already locked." When I ran the "/usr/sbin/groupadd" command with "newGroupName" argument from the shell it worked and the newGroupName was created. I ran the new program with root privilages. Any idea why my program failed and which "group file" the error message is referring to?
was the program run as root??

walwali 06-02-2005 06:32 PM

Yes!

anandt4u 06-02-2005 07:06 PM

well i tried but it worked without errors but did not add any new group

itsme86 06-02-2005 08:52 PM

You're using execv() incorrectly. Check out the man page:
Quote:

int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);

The execv and execvp functions provide an array of point_
ers to null-terminated strings that represent the argument
list available to the new program. The first argument, by
convention, should point to the file name associated with
the file being executed. The array of pointers must be
terminated by a NULL pointer.
You're just passing in a string for the argv parameter. It doesn't work that way. Why don't you just use system() instead?

walwali 06-03-2005 12:12 AM

Quote:

Originally posted by anandt4u
well i tried but it worked without errors but did not add any new group
Thanks anandt4u for following on with me. Actually the error message is embedded inside a CommandResult object and indicated by the global variable errno. To view it I had to have a logger that sends it messages to /var/log/messages. If the group isn't added then apparently your program isn't working either, you just can't see where it is failing.

walwali 06-03-2005 12:20 AM

Hi itsme86. Actually I have used execv() correctly, I just did not list the whole thing in my post for simplicity.
I did create a const char* path variable and a const char* argv[2] whose first element contains the new group name and the second was NULL, then passed them to execv().
Add to that the error message the program returned was kind of showing that the problem was not the way execv() invoked, rather it seems like a file access conflict.
I've tried system() and it did not work either. It did not create the new group and returned a non zero value (I don't remember the returned value for sure now maybe 1280 or something, I can reproduce it if it make a difference, however it seems to me that it would have the same indication of the file lock conflict.)

anandt4u 06-03-2005 11:59 AM

After some googling and referring the man pages for execv, i tried this and it actually worked. There needs to be a null characted in the last. But i wonder why we should be repeating the groupadd command two times



Code:

#include<unistd.h>

int main(int args, char *argv[])
{
 char *cmd[]={"groupadd","newgroup",(char *)0};
 int a=execv("/usr/sbin/groupadd",cmd);
 printf("\n%d\n",a);
}


jeename 11-22-2010 07:36 AM

My experience
 
I had the same problem,i tried everything manually but nothing worked,i am using a tool for all such type of errors which is available at pathtoodeep.com which will solve your problem in a click


All times are GMT -5. The time now is 05:51 AM.