-   Programming (
-   -   need sudo priviledges in c program (

jcwkyl 02-10-2011 12:39 AM

need sudo priviledges in c program
Dear all,

I need to do mount/umount operations in my c program. But my program can only be run under normal user. However, the user who run my program have sudo priviledges to do mount/umount operations. How can i apply the sudo privileges in the mount(2) system call? Please help.

appilu 02-10-2011 12:54 AM


which command you are going to use to call mount command in your program.

Try this

system("sudo <command>")

sunnydrake 02-10-2011 08:25 AM

sudo prividleges apply recursive.. eg if user run script as sudo ./ inside script it will have sudo priviledge.

dwhitney67 02-10-2011 09:01 AM

Have your system administrator update the /etc/sudoers file to allow the user (or users) to use sudo when running the C program.

jcwkyl 02-11-2011 01:40 AM


As appilu said, i use system() call instead:
system("/usr/bin/sudo /bin/mount -o loop,nodev,noexec,rw loop.img /mnt/disk");

I meant to use mount(2) system call directly:
mount("loop.img", "/mnt/disk", "loop", MS_NODEV | MS_NOEXEC, NULL);
but it seems i was wrong.

appilu 02-11-2011 01:49 AM

i think "system" is the best way to execute a shell command.

Reuti 02-11-2011 08:38 AM

What about allowing the user to use one of the loop devices? Hence he can mount any image he likes on a granted loop device without being root or sudo:

1. Allow the user to write to one of the loop devices:

# chmod g+w /dev/loop7
# ls -lh /dev/loop7
brw-rw---- 1 root disk 7, 7 Feb 15  2010 /dev/loop7

2. Add the user to the group disk:

# usermod -A disk reuti
3. Add a line to /etc/fstab:

/dev/loop7          /home/reuti/foobar  auto      defaults,loop,user 0 0

Now we use a fresh login to mount an image (mounter.e2fs) as an ordinary user:

$ id
uid=1000(reuti) gid=100(users) groups=6(disk),10(wheel),16(dialout),33(video),100(users)
$ /sbin/losetup /dev/loop7 mounter.e2fs
$ mount /dev/loop7
$ ls foobar
lost+found  foo  bar
$ umount /dev/loop7
$ /sbin/losetup -d /dev/loop7

All times are GMT -5. The time now is 05:33 PM.