Passing groupadd to execv() returned error msg: "file already locked."
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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?
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.
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.)
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);
}
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.