LinuxAnswers - the LQ Linux tutorial section.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-10-2011, 12:39 AM   #1
LQ Newbie
Registered: Jul 2010
Posts: 9

Rep: Reputation: 0
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.
Old 02-10-2011, 12:54 AM   #2
Registered: Jan 2011
Distribution: RedHat,Debian-Ubuntu,Fedora
Posts: 73

Rep: Reputation: 8

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

Try this

system("sudo <command>")
Old 02-10-2011, 08:25 AM   #3
Registered: Jul 2009
Location: Kiev,Ukraine
Distribution: Ubuntu,Slax,RedHat
Posts: 289
Blog Entries: 1

Rep: Reputation: 41
sudo prividleges apply recursive.. eg if user run script as sudo ./ inside script it will have sudo priviledge.
Old 02-10-2011, 09:01 AM   #4
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 334Reputation: 334Reputation: 334Reputation: 334
Have your system administrator update the /etc/sudoers file to allow the user (or users) to use sudo when running the C program.
Old 02-11-2011, 01:40 AM   #5
LQ Newbie
Registered: Jul 2010
Posts: 9

Original Poster
Rep: Reputation: 0

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.
Old 02-11-2011, 01:49 AM   #6
Registered: Jan 2011
Distribution: RedHat,Debian-Ubuntu,Fedora
Posts: 73

Rep: Reputation: 8
i think "system" is the best way to execute a shell command.
Old 02-11-2011, 08:38 AM   #7
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,323

Rep: Reputation: 252Reputation: 252Reputation: 252
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


mount, sudo

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Ultimate Sudo FAQ To Sudo Or Not To Sudo? LXer Syndicated Linux News 13 04-13-2013 02:36 AM
open sudo terminal program in aix in c sachin.s.puri AIX 1 12-21-2009 09:22 AM
providing root priviledges through a C program with GTK based gui sumitshining Programming 6 06-22-2009 02:29 AM
startup program: is sudo needed? Patrick K Ubuntu 2 04-24-2007 11:29 AM
sudo problem. Want permission to run a sbin program without a password inverted.gravity Linux - Software 11 03-21-2007 04:34 PM

All times are GMT -5. The time now is 06:09 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration