Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I usually use mkdir function to make directory (e.g mkdir("/home/dhw/test", S_IREAD|S_IWRITE) )

But I can't make directory in /usr/local/bin.

I guess it is problem with authority.

I already know I can do this work with

system("sudo mkdir /usr/local/bin/test/");

and input root password. But I don't want to make user to get root permission manually.

What should I do?

share|improve this question

migrated from stackoverflow.com Aug 12 '11 at 6:46

This question came from our site for professional and enthusiast programmers.

9  
You have to get root permission because it is an action only root should be able to do. No way around. And there should be none. –  pmr Aug 12 '11 at 2:06
    
Your example is somewhat dodgy; why would you want to create a directory under /usr/local/bin? That completely goes against the FHS. Supporting files for a program go in /usr/local/lib and /usr/local/share. –  Delan Azabani Aug 12 '11 at 2:06
    
@Delan What You said is I should not create directory under /usr/local/bin, /usr/local/share and I should move my program in those directory? –  도현우 Aug 12 '11 at 2:11
1  
You should to be able to use the mkdir function to do this so long as you require the user to run your program as root (sudo ./myProgram), and just display a nice error message if your program is not root. If you're going about this, then you ought to need root access. –  Slubb Aug 12 '11 at 2:12
    
Not quite. The contents of bin should not include directories, but only directly executable files. If you have multiple final executables for your program, you may want to hyphenate the name, as in myapp-program1, myapp-program2, etc. Any supporting files should go into share if they're not architecture-dependent, and lib if they are. –  Delan Azabani Aug 12 '11 at 2:15

2 Answers 2

up vote 1 down vote accepted

Why not make the program have suid permission? But quite frankly, directories outside of /home should not be touched. However, if you insist on doing this, just make 100% sure that memory checks are rigorous and that there is zero vulnerabilities, then using suid permission will solve it.

Another way, make that program a member of root group.

All in all, this is an odd requirement to touch directories that is against FHS.

share|improve this answer

This question already has an accepted answer, but a bit of elucidation on the Unix file hierarchy seems to be in order. Everybody who first encounters the Unix file hierarchy wants to change it, not even excepting ex-CEO Steve Jobs (tip o'the hat in his general direction). The layout has been codified for Linux in the so called FHS (Filesystem Hierarchy Standard), which lays out the location and purpose for each of the typical system directories. Wikipedia has a concise listing of some these. The Linux Standard Base (LSB) goes further and outlines the APIs that must be present on an LSB-compliant system, part of which is specified by the FHS.

The Unix way is to isolate Unix system files from administrator files and further both of these from user files. The user is typically permitted to use his or her home directory (usually in the /home directory structure), /tmp, /var/tmp and certain specific files that the system may permit by design or administrator configuration.

The administrator typically has control over the system, but the FHS reserves a handful of directories for the administrator. Specifically, /usr/local and /opt (including some other related directories) are reserved by the system for administrator use. These are used to install software that is not maintained by or provided by the system.

The Linux system, which today is maintained and managed by the creator of the distro used, reserves most of the rest of the filesystem hierarchy for itself. This division of responsibilities helps maintain the security and stability of the system. When necessary, modifications may be made by either the administrator or the package system (if any) of the distro. Popular package systems include Debian's apt, Novell's zypper, RedHat's yum or Gentoo's portage package manager. Most of these package managers have means for the administrator to control the selection, configuration and maintenance of system packages.

In conclusion, an application programmer should keep in mind that the user has limited access on the system and for good reason. Messing with system directories is likely not the right direction, unless for one of the purposes outlined in the FHS.

If changes must be made, there are tools available to work around the situation, including judicious use of sudo, PolicyKit, ACLs and file attributes (chmod, etc.).

The original author has not explained the purpose for the directory he needs, so it is hard to answer more specifically. I hope this answer is useful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.