mknod system call to clone /dev/null


 
Thread Tools Search this Thread
Top Forums Programming mknod system call to clone /dev/null
# 1  
Old 09-18-2006
mknod system call to clone /dev/null

I'm trying to use the "mknod" call in C to create a clone of /dev/null. I am stumped as to the final parameter I should provide to "mknod()". I am supposed to give it a type dev_t, which specifies a major & minor number. I want to specify major 3, minor 1, but how can I do this?

dev_t seems to be a long long. It takes up 8 bytes, but I can't find anything about it. I've searched through quite a few /usr/include files, but I still can't find anything (nor on google).

If I 'stat()' /dev/null and then print the value of st_dev as an int (%d), I get '16'. But when I try and create a file called 'mynull', a clone of /dev/null, I get a major number of 0 and minor number of 16 , as shown below ( mknod() fails unless I call it as root ).
Code:
c---------  1 root root 0, 16 Sep 18 21:20 mynull

From my man page:
Code:
       If the file type is S_IFCHR or S_IFBLK then dev specifies the major and minor  numbers of the newly created device special file; otherwise it is ignored.

Does anyone have any ideas as to how I can create a character special file with major number 1 and minor number 3?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

/* int mknod(const char *pathname, mode_t mode, dev_t dev); */

main( argc, argv )
  int   argc;
  char  *argv[];
{
  int rc;
  char path[256];
  char errstr[1024];

  strcpy( path, "mynull" );

  rc = mknod( path, S_IFCHR, 16 );

  if ( rc < 0 )
    perror( "mknod" );

  return(rc);
}

Here's what I get when I stat /dev/null:
Code:
  rc = stat( "/dev/null" , &fstatus );
  printf("dev_t for /dev/null = %d\n", fstatus.st_dev);

$ a.out
dev_t for /dev/null = 16
# 2  
Old 09-19-2006
nathan,
You are referencing the wrong struct member to get device numbers. You should be referring to st_rdev and not st_dev.

From man stat:
Code:
dev_t    st_dev;      /* ID of device containing */
                           /* a directory entry for this file */
     dev_t    st_rdev;     /* ID of device */
                           /* This entry is defined only for */
                           /* char special or block special files */

Also, if you know the major and the minor device numbers, you can use the makedevice macro to create the device number. The makedevice macro is defined in /usr/include/sys/sysmacros.h. Maybe you could take a look at that file as well.
# 3  
Old 09-20-2006
blowtorch, thanks for the explanation. You are right, the macro is defined in /usr/include/sys/sysmacros.h. I was able to find the correct value using makedev(1,3).

If I would have printed the correct member from the stat() call, I would have found that it is the number 259.

Thanks for your help.

The final code is below, with changes in red.

Code:
$ cat mknod.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <errno.h>

/* int mknod(const char *pathname, mode_t mode, dev_t dev); */

main( argc, argv )
  int   argc;
  char  *argv[];
{
  int rc;
  int old_umask;
  char path[256];
  char errstr[1024];

  strcpy( path, "mynull" );

  old_umask = umask(0);
  rc = mknod( path, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, makedev(1,3) );

  if ( rc < 0 )
    perror( "mknod" );

  return(rc);
}
[root@localhost c]# a.out
[root@localhost c]# ll mynull
crw-rw-rw-  1 root root 1, 3 Sep 19 23:21 mynull

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

2>/dev/null

Friends have the following problem a search may not find anything which would correct example: ls -ltr *prueba.txt | nawk '{ print $9 }' > Procesar.dat 2>/dev/null When he finds nothing gives me the following error ls: prueba.txt: No such file or directory because 2> / dev / null... (4 Replies)
Discussion started by: tricampeon81
4 Replies

2. Shell Programming and Scripting

Help with /dev/null Please

Hello All and a Happy New year to yous guys. I'm running the below command on my AIX box and it keeps giving me the message that the file doesn't exist. I know the file don't exist, but I don't want to see the error. 2>/dev/null doesn't work. bash-3.00$ ls -l C* | wc -l 2>/dev/null ls:... (2 Replies)
Discussion started by: bbbngowc
2 Replies

3. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

4. Solaris

Canīt open /dev/dsk/c1t0d0s0 Clone/mirror

Hello friends, I Working with Solaris 8 on a SunFireV890, 150 GB SCSI HD's in Raid 1 (mirroring), my problem is that the master disk failed and going to put the slave (mirror) as a Master in the slot 0 (SCSI) will not start. The original mounting this, mirror in Raid 0: c1t0d0s0 (master)... (10 Replies)
Discussion started by: grymorum
10 Replies

5. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

6. Shell Programming and Scripting

/dev/null what is the use of it?

when do you use the path /dev/null (3 Replies)
Discussion started by: webmunkey23
3 Replies

7. Shell Programming and Scripting

/dev/null

Hi expert, May I know what is the difference between below cron tab entry ? 0,12 * * * * /abc/myscript.sh > /dev/null 2>&1 0,12 * * * * /abc/myscript.sh (7 Replies)
Discussion started by: olaris
7 Replies

8. Solaris

What is /dev/tty /dev/null and /dev/console

Hi, Anyone can help My solaris 8 system has the following /dev/null , /dev/tty and /dev/console All permission are lrwxrwxrwx Can this be change to a non-world write ?? any impact ?? (12 Replies)
Discussion started by: civic2005
12 Replies

9. Shell Programming and Scripting

> /dev/null

hello all, In many shell scripts i found '> /dev/null' , i am not able to get this, will any one please explain why we are using this. thanks sudha (2 Replies)
Discussion started by: rrs
2 Replies

10. UNIX for Advanced & Expert Users

Q1 :/dev/null Q2 -A

Hi, Q1-What does nroff -ms > /dev/null Q2- What does mean -A under STAT column : ps aux |head -20 UTIL PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND root 516 93,0 0,0 12 12 - A 04 nov 3906:51 wait Thank you. (4 Replies)
Discussion started by: big123456
4 Replies
Login or Register to Ask a Question