LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 01-13-2008, 05:27 AM   #1
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Rep: Reputation: 15
mkdir in C++ setting wrong permissions


Within a C++ program, I am creating a new directory to store log files. The problem is that when I use this code ...

mkdir("logs",0777);

The created directory has it's permissions set to 755. This is catastrophic for my application as the logs directory is shared by all users on the system, and once the logs are initially created by the first user, no other user is able to enable logging.

I am hoping to avoid the need to create a separate logs directory for each user, as this will require substantial modification of the code I'm porting (It was originally a Windows application).

I need to avoid use of the system() function. Also, there is no sensitive information being stored in the logs folder that needs to be kept secret. It is merely diagnostic data to assist in error handling.

Any help would be greatly appreciated.
 
Old 01-13-2008, 08:35 AM   #2
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
I'm not sure you can throw octal at it like that when setting the permission bits. I'm not sure you can't, either, but try ORing the permission bits, like

mkdir("logs", S_IRWXU | S_IRWXG | S_IRWXO );

Another thought is that perhaps the umask is being applied after the directory is being created. If the above does not work, trying temporarily resetting umask to 000.

Hope that helps!
 
Old 01-13-2008, 09:50 AM   #3
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
Code:
// g++ -Wall -o mymkdir mymkdir.cpp

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

#define MY_MASK 0777

int main()
{
  int temp;

  printf("Default mask: %o\n", MY_MASK & ~022 & MY_MASK);

  temp = umask(0);
  printf("Previous umask = %o\n", temp);
  if ((temp = mkdir("foodir", MY_MASK)) != 0) {
    fprintf(stderr, "ERROR %d: unable to mkdir; %s\n", errno, strerror(errno));
  }

  return 0;
}

Last edited by h/w; 01-13-2008 at 09:58 AM.
 
Old 01-13-2008, 05:47 PM   #4
fcdev
Member
 
Registered: Sep 2005
Posts: 47

Original Poster
Rep: Reputation: 15
Thanks, it was fixed by a call to umask(0). I'd never encountered that function before, and had trouble finding documentation on it. I also changed the permissions to S_IRWXU | S_IRWXG | S_IRWXO instead of 0777. This was another problem of not being able to find any documentation on what flags were used by mkdir. Once I read that S_IRWXU ... I knew exactly where to look.

Thanks again guys.
 
Old 01-13-2008, 05:57 PM   #5
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
Glad to be of help.

man 2 umask
man 2 stat
 
  


Reply



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
wrong permissions - orbit High-gain Mandriva 4 08-06-2007 11:51 PM
MKDIR Permissions? carlosinfl Linux - General 4 05-17-2007 09:50 AM
Permissions wrong on FTP file upload jwest44 Linux - Newbie 3 06-23-2004 10:15 AM
permissions all wrong or just me? mi_july Linux - Newbie 15 05-03-2004 03:40 PM
No root access, wrong permissions unixgeek Linux - Software 3 09-16-2003 09:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:39 PM.

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