2

I need to create a directory using mknod() (use of mkdir() is not allow in my case), I would call the program from a certain directory and introduce the path where the new dir should be created inside the previous one.

Ex: If I'm /home/user/test/ and inside test there is /level1/, I want to create the directory level2 inside level1, so I would pass the argument /level1/level2/

I have the following code that works when I create a pipe,but when I change the mode to S_IFDIR, it doesn't do anything.

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

void main(int argc,char *argv[]){
    int status;
    if ((status  = mknod(argv[1], S_IFDIR,0)) == 1){
        printf("error\n" );
    }
    exit(0);

}
tony
  • 49
  • 1
  • 10
  • 1
    Perhaps you are running the code on an operating system (e.g. linux) that does not support creating directories with mknod(). – nos Nov 07 '14 at 23:44
  • 1
    in linux, the man page for mknod says: The file type must be one of S_IFREG, S_IFCHR, S_IFBLK, S_IFIFO, or S_IFSOCK to specify a regular file (which will be created empty), character special file, block special file, FIFO (named pipe), or UNIX domain socket, respectively. (Zero file type is equivalent to type S_IFREG.), I.E. there is no ability to generate a (sub-)directory here is the link to that man page: http://man7.org/linux/man-pages/man2/mknod.2.html – user3629249 Nov 08 '14 at 10:26
  • @user3629249 I wonder what the rational for disallowing it is. It'd be pretty nice, conceptually, if `mknod` could create all the possible file tree nodes. Perhaps there's a security reason. The ability for filter out directory creations, e.g., with `seccomp`, because they're a separate syscall? – Petr Skocik Feb 14 '19 at 15:56

2 Answers2

6

mknod is normally used for creating device nodes (special directories).

However, some OSs do support creating a regular directory with mknod (QNX).

Did you check the man page for mknod on the OS you're using? I am quite sure that S_IFDIR is a non-portable option for mknod.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
0

Nothing wrong with the code above. I bumped into this on an old version of unix and mknod needed the effective id of root in order to create the directory. I suspect if you called perror("") after the failure you will get something about "Not Allowed".