Skip to content
Advertisement

Why open() has no ‘fd’ return in linux 1.0?

Since there is no return of ‘fd’, how does it read/write later for?

ex:

void init(void)
{
 ....
 (void) open("/dev/tty1",O_RDWR,0);
 ....

Advertisement

Answer

open returns a value in there. The cast-to-void is used to signal the compiler that the return value is being deliberately ignored.

The init function is the one where the current thread is prepared to execute the init program in user-space. init will expect the standard in, out and error descriptors be opened. The full code is this:

(void) open("/dev/tty1",O_RDWR,0);
(void) dup(0);
(void) dup(0);

There is no need to store the return value to anything, since the open is guaranteed to use the lowest free descriptor and none are used by the process before entering this function, thus open will return 0. Same rule of returning the lowest free applies to dup as well. After these 3 calls, all descriptors 0, 1, and 2 share the same file description which also means that you can write to standard in and read from standard error.

This is perhaps a micro-optimization, but there is indeed no need to use a variable and make the compiler generate substandard code when the return value of the open is known – it is after all analogous to

int fd = open("/dev/tty1",O_RDWR,0);
assert(fd == 0);
(void) dup(fd);
(void) dup(fd);

In the current revision there is an assert in place, checking that the open does not fail:

/* Open the /dev/console on the rootfs, this should never fail */
if (ksys_open((const char __user *) "/dev/console", O_RDWR, 0) < 0)
    pr_err("Warning: unable to open an initial console.n");

(void) ksys_dup(0);
(void) ksys_dup(0);

However, the actual file descriptor return values are being ignored.

Advertisement