Sponsored Content
Top Forums Programming mknod system call to clone /dev/null Post 302089359 by nathan on Monday 18th of September 2006 10:26:24 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
explain_mknod(3)					     Library Functions Manual						  explain_mknod(3)

NAME
explain_mknod - explain mknod(2) errors SYNOPSIS
#include <libexplain/mknod.h> const char *explain_mknod(const char *pathname, mode_t mode, dev_t dev); const char *explain_errno_mknod(int errnum, const char *pathname, mode_t mode, dev_t dev); void explain_message_mknod(char *message, int message_size, const char *pathname, mode_t mode, dev_t dev); void explain_message_errno_mknod(char *message, int message_size, int errnum, const char *pathname, mode_t mode, dev_t dev); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the mknod(2) system call. explain_mknod const char *explain_mknod(const char *pathname, mode_t mode, dev_t dev); The explain_mknod function is used to obtain an explanation of an error returned by the mknod(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. pathname The original pathname, exactly as passed to the mknod(2) system call. mode The original mode, exactly as passed to the mknod(2) system call. dev The original dev, exactly as passed to the mknod(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: if (mknod(pathname, mode, dev) < 0) { fprintf(stderr, "%s ", explain_mknod(pathname, mode, dev)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_mknod_or_die(3) function. explain_errno_mknod const char *explain_errno_mknod(int errnum, const char *pathname, mode_t mode, dev_t dev); The explain_errno_mknod function is used to obtain an explanation of an error returned by the mknod(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. pathname The original pathname, exactly as passed to the mknod(2) system call. mode The original mode, exactly as passed to the mknod(2) system call. dev The original dev, exactly as passed to the mknod(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. Example: This function is intended to be used in a fashion similar to the following example: if (mknod(pathname, mode, dev) < 0) { int err = errno; fprintf(stderr, "%s ", explain_errno_mknod(err, pathname, mode, dev)); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_mknod_or_die(3) function. explain_message_mknod void explain_message_mknod(char *message, int message_size, const char *pathname, mode_t mode, dev_t dev); The explain_message_mknod function is used to obtain an explanation of an error returned by the mknod(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. pathname The original pathname, exactly as passed to the mknod(2) system call. mode The original mode, exactly as passed to the mknod(2) system call. dev The original dev, exactly as passed to the mknod(2) system call. Example: This function is intended to be used in a fashion similar to the following example: if (mknod(pathname, mode, dev) < 0) { char message[3000]; explain_message_mknod(message, sizeof(message), pathname, mode, dev); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_mknod_or_die(3) function. explain_message_errno_mknod void explain_message_errno_mknod(char *message, int message_size, int errnum, const char *pathname, mode_t mode, dev_t dev); The explain_message_errno_mknod function is used to obtain an explanation of an error returned by the mknod(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. pathname The original pathname, exactly as passed to the mknod(2) system call. mode The original mode, exactly as passed to the mknod(2) system call. dev The original dev, exactly as passed to the mknod(2) system call. Example: This function is intended to be used in a fashion similar to the following example: if (mknod(pathname, mode, dev) < 0) { int err = errno; char message[3000]; explain_message_errno_mknod(message, sizeof(message), err, pathname, mode, dev); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } The above code example is available pre-packaged as the explain_mknod_or_die(3) function. SEE ALSO
mknod(2) create a special or ordinary file explain_mknod_or_die(3) create a special or ordinary file and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2009 Peter Miller explain_mknod(3)
All times are GMT -4. The time now is 04:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy