Unix Programming - mounting floppy as a non-root user in C.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2004 > mounting floppy as a non-root user in C.





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 mounting floppy as a non-root user in C.
dalbosco

2004-07-28, 6:19 pm

Hi,


I am currently writing a Unix software ( Unix means "Unix portable") and
i'm facing a problem with writing data on a floppy as a simple user.


I'v given below a working example of what my problem is.
As a user the program answers :
# ./main
mount failed : Operation not permitted.
umount failed : Invalid argument.


Though as root it succeeds and replies:
# su -c ./main
Password:
mount succeeded.
umount succeeded.


What embarasses me, is that in /etc/fstab i can add the option "user" to
the floppy entry:

/dev/fd0 /mnt/floppy auto noauto,user,kudzu 0 0



and then in a shell as a user I can succesfully do "mount /mnt/floppy".

So I was wondering how to do the same in a C program WITHOUT making a
system("mount /mnt/floppy") call as far as i wan't to check the return
value of the mounting operation to be able to detect if there is a
floppy in the drive or not, not being able to write the file if the user
retrieves the floppy too fast...
Does anyone know how to do that?
Thanks by advance.
--
Dalbosco
Jean-François


#include <stdio.h>
#include <string.h>
#include <sys/mount.h>
#include <errno.h>


static void
check_error_ret(int ret, const char* szCmd)
{
if( -1 == ret )
{
printf("%s failed : %s.\n", szCmd, strerror(errno));
}
else
{
printf("%s succeeded.\n", szCmd);
}
}


int
main(int argc, char** argv)
{
int ret = 0;


ret = mount("/dev/fd0", "/mnt/floppy", "vfat", MS_MGC_VAL, "");
check_error_ret(ret, "mount");


ret = umount("/mnt/floppy");
check_error_ret(ret, "umount");
}
Daniel Rakel

2004-07-28, 6:19 pm

dalbosco wrote:

> I'v given below a working example of what my problem is.
> As a user the program answers :
> # ./main
> mount failed : Operation not permitted.
> umount failed : Invalid argument.
>
> Though as root it succeeds and replies:
> # su -c ./main
> Password:
> mount succeeded.
> umount succeeded.
>
> What embarasses me, is that in /etc/fstab i can add the option "user" to
> the floppy entry:
>
> /dev/fd0 /mnt/floppy auto noauto,user,kudzu 0 0
>
> and then in a shell as a user I can succesfully do "mount /mnt/floppy".


To mount you need root privileges in either case. mount(1) as a
setuid-root program has these privileges and checks the user option in
/etc/fstab when called by an unprivileged user.

Regards,
Daniel
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com