GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Dec 20, 2014 12:53 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: How can I opening /dev files without root privileges
PostPosted: Sat Jan 26, 2013 8:17 pm 
Offline

Joined: Sat Jan 26, 2013 9:37 am
Posts: 3
Location: Barcelona (Spain)
Hi to all in this forum,

This is my first post in this forum, sorry if anything is wrong...

Ok, I'm writing a program under Linux, that uses the serial port, for communicate with different devices in different protocols (Modbus, CANBus, SSI, etc)
My first problem was the listing of existing serial ports installed in PC.
I wrote a program that list this devices, the problem is that only works with root priveleges. Inside the program is necessary the open device in /dev directory for get information about the serial port.
Example:
Code:
...
// Try to open the device
int fd = open(dev_path, O_RDWR | O_NONBLOCK | O_NOCTTY | O_NDELAY);
if (fd >= 0) {
// Get serial_info
if (ioctl(fd, TIOCGSERIAL, &serinfo)==0) {
....


And the question is:
How can i get this information without root priveleges? Or
How can I present the MessageBox asking the root password and use it?

Thanks
Gabriel


Top
 Profile  
 
 Post subject: Re: How can I opening /dev files without root privileges
PostPosted: Sat Jan 26, 2013 9:12 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
Your first mistake might be to ask this question in a gtk forum and not in a linux developer forum. Nevertheless the easiest solution might be to add your user to the group which owns the file.

if you enter:

ls -l /dev/ttyS0

you'll get in ubuntu 12.10:

crw-rw---- 1 root dialout 4, 64 Jan 26 20:52 /dev/ttyS0

and then enter as root:

adduser <your user> dialout


Top
 Profile  
 
 Post subject: Re: How can I opening /dev files without root privileges
PostPosted: Sat Jan 26, 2013 9:51 pm 
Offline

Joined: Sat Jan 26, 2013 9:37 am
Posts: 3
Location: Barcelona (Spain)
asdfgh2091 wrote:
Your first mistake might be to ask this question in a gtk forum and not in a linux developer forum. Nevertheless the easiest solution might be to add your user to the group which owns the file.


Many thanks for your answer asdfgh2091, yes you right, the question may be differs slightly GTK, but the program I'm writing in GTK, and I want to display in a GtkWindow list of all the PC's serial ports. The program works fine, and I get a complete list of devices, with I/O address and IRQ values, but only works in root.

I will question another way, how do in GTK programs to display a dialog box and ask for the root password?
Show this easy, but once you have the password, how it is used from the program?
For example, programs such as YaST, Apptitude and other...

Or in others words, how is possible get root privileges programming in GTK?


Top
 Profile  
 
 Post subject: Re: How can I opening /dev files without root privileges
PostPosted: Sun Jan 27, 2013 4:11 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
If you want to get root privileges the easiest way is to run your program initially as root from the terminal, e.g. sudo YourProgramName. Even better would be if your program didn't need root access e.g. if opening the file as read only (from what you say the program does I don't see any need for write access). In the case of the former I'd hope that you program it well because most people wouldn't trust entering sudo unless the program has the stamp of approval of a mainstream Linux distribution on it.
As asdfgh2091 has said adding the users permission for the file is a good method as it limits root-like permissions to only that particular file. This might not be practical in many cases. If you want to see how yast/aptitude etc. do this it is best to look at the source code (I must confess my ignorance in this area). Be careful how you go about handling passwords, as these could cause security violations.
By the way, no brow beating is intended for criticising posting this in a GTK forum. Just that though it might be a GTK program, your particular issue does not depend per say on what GUI toolkit you are using but is rather a wider c programming issue. I have no issue with you posting it here, just you'd likely find more people able to help you on a more generalised forum. Though you are developing for linux I'd advise you to not rule out portability to other OS's (GTK function calls are cross-platform so why not ensure the rest of your c code is cross-platform too) and look into using glib function calls for file operations etc.


Top
 Profile  
 
 Post subject: Re: How can I opening /dev files without root privileges
PostPosted: Sun Jan 27, 2013 9:55 am 
Offline

Joined: Sat Jan 26, 2013 9:37 am
Posts: 3
Location: Barcelona (Spain)
Many thanks for your explanation Paul Childs. You are right, maybe this forum was not the right place for my query.
Anyway I have been very pleased with the quick responses.

I close off this thread, but in the future I will not hesitate to use it with more specific questions about the GTK GUI

The possibility of bringing this project to other platforms is very difficult due to the low-level (kernel) programming
I think about it ...

Thanks for everything
Gabriel


Top
 Profile  
 
 Post subject: Re: How can I opening /dev files without root privileges
PostPosted: Sun Jan 27, 2013 10:51 am 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
If you really need root privileges, you can make your programm or only part of it setuid http://en.wikipedia.org/wiki/Setuid. But be aware of http://www.gtk.org/setuid.html.


Top
 Profile  
 
 Post subject: Re: How can I opening /dev files without root privileges
PostPosted: Sun Jan 27, 2013 9:38 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
If you are doing low level kernel work and GUI programming, you may want to separate the GUI code from the low level stuff. That way you only have a smaller subset to worry about porting as well as pushing all the security issues to the process interacting with the kernel (as per the example given in the latter link by asdfgh2091).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group