Skip to contentSkip to conversation

Fork and Exec a process within the kernel

DISCUSSION Eric Masson 7 participants 13 messages Aug 9, 2004 – Aug 11, 2004

lore
Share & tools
lore
#0 lore raw

Hi all,

I know it's not the best approach to the problem I'm attempting to solve, but I'm attempting to fork a thread and exec a process from inside the kernel. I tried creating a kernel thread and attempted to exec from there. I actually make it into my child thread, but the exec doesn't appear to do anything. Any ideas how to do this.

Eric

#1 lore raw

Try grep'ing for call_usermodehelper()

Signature · 3 lines
                          I won't rest till it's the best ...
                          Programmer, Linux Scalability
                          Paul Jackson <[email protected]> 1.650.933.1373
#2 lore raw

Thanks for the pointer! My user mode program is running. Any idea how to control which console it shows up on?

Eric

Quoted 4 lines · Paul Jackson

Try grep'ing for call_usermodehelper()

#3 lore raw
Quoted 1 line

My user mode program is running.

Good.

Quoted 1 line

Any idea how to control which console it shows up on?

No clue.

Signature · 3 lines
                          I won't rest till it's the best ...
                          Programmer, Linux Scalability
                          Paul Jackson <[email protected]> 1.650.933.1373
#12 lore raw
In reply to Eric Masson

On Tue, 2004-08-10 11:22:10 -0400, Eric Masson <[email protected]> wrote in message <[email protected]>:

Quoted 2 lines

Thanks for the pointer! My user mode program is running. Any idea how to control which console it shows up on?

The best idea for a usermode helper is to first close stdin/stdout/stderr and re-open it with /dev/null...

If you need to communicate, write into some logfile.

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
#4 lore raw
In reply to Paul Jackson
Quoted 7 lines · Paul Jackson
Quoted 1 line

My user mode program is running.

Good.

Quoted 1 line

Any idea how to control which console it shows up on?

No clue.

OK, ignore the peanut gallery, this is the real answer:

./foo > /dev/tty1

See /etc/syslog.conf on Debian for another example. Anywhere you could write to a file you can write to /dev/tty$FOO and it will do the right thing.

UNIX is full of stuff like this. Pretty cool, huh?

Lee

#5 lore raw
In reply to Paul Jackson
Quoted 9 lines · Paul Jackson
Quoted 1 line

My user mode program is running.

Good.

Quoted 1 line

Any idea how to control which console it shows up on?

No clue.

--

/dev/console is a symlink to /dev/tty0.

Alt-F1 = /dev/tty0
Alt-F2 = /dev/tty1
Alt-F3 = /dev/tty2

... etc.

    struct termios term;

    tcgetattr(0, &term);	// Get old terminal characteristics
    (void)close(0);		// Close old terminal(s)
    (void)close(1);
    (void)close(2);
    fd = open("/dev/console", O_RDWR);
    tcsetattr(fd, &term);	// Restore characteristics
    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);

Cheers,
Dick Johnson
Penguin : Linux version 2.4.26 on an i686 machine (5570.56 BogoMips).
Note 96.31% of all statistics are fiction.

#6 lore raw
Quoted 1 line · Richard B. Johnson

/dev/console is a symlink to /dev/tty0.

Please don't mislead newbies, Richard. /dev/console is NOT a link to /dev/tty0, it's a completely different device:

erik@abra2:~ >ls -l /dev/console
crw------- 1 root tty 5, 1 Apr 7 09:13 /dev/console
erik@abra2:~ >ls -l /dev/tty0
crw------- 1 root tty 4, 0 Feb 10 2000 /dev/tty0

On x86 desktop systems console output usually comes on the virtual terminals, but you can also use serial console. My embedded StrongARM board only has serial console.

Quoted 7 lines
    struct termios term;

    tcgetattr(0, &term);	// Get old terminal characteristics
    (void)close(0);		// Close old terminal(s)
    (void)close(1);
    (void)close(2);
    fd = open("/dev/console", O_RDWR);

And what happens when you have console on a device that's not a serial port like a line printer?

Erik

Signature · 2 lines
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
#7 lore raw
Quoted 11 lines · Erik Mouw
Quoted 1 line · Richard B. Johnson

/dev/console is a symlink to /dev/tty0.

Please don't mislead newbies, Richard. /dev/console is NOT a link to /dev/tty0, it's a completely different device:

erik@abra2:~ >ls -l /dev/console
crw------- 1 root tty 5, 1 Apr 7 09:13 /dev/console
erik@abra2:~ >ls -l /dev/tty0
crw------- 1 root tty 4, 0 Feb 10 2000 /dev/tty0

Bullshit. I know how to use file.

Script started on Wed Aug 11 07:21:39 2004

# file /dev/console
/dev/console: symbolic link to /dev/tty0
# exit
Script done on Wed Aug 11 07:21:51 2004
Quoted 4 lines

On x86 desktop systems console output usually comes on the virtual terminals, but you can also use serial console. My embedded StrongARM board only has serial console.

Then that's your problem.

Quoted 8 lines
Quoted 7 lines
    struct termios term;

    tcgetattr(0, &term);	// Get old terminal characteristics
    (void)close(0);		// Close old terminal(s)
    (void)close(1);
    (void)close(2);
    fd = open("/dev/console", O_RDWR);
Quoted 6 lines

And what happens when you have console on a device that's not a serial port like a line printer?

Erik

It will still work but there is no input and it can't be turned into a "controlling terminal".

Cheers,
Dick Johnson
Penguin : Linux version 2.4.26 on an i686 machine (5570.56 BogoMips).
Note 96.31% of all statistics are fiction.

#8 lore raw
Quoted 10 lines · Richard B. Johnson
Quoted 8 lines · Erik Mouw

Please don't mislead newbies, Richard. /dev/console is NOT a link to /dev/tty0, it's a completely different device:

erik@abra2:~ >ls -l /dev/console
crw------- 1 root tty 5, 1 Apr 7 09:13 /dev/console
erik@abra2:~ >ls -l /dev/tty0
crw------- 1 root tty 4, 0 Feb 10 2000 /dev/tty0

Quoted 5 lines

Bullshit. I know how to use file.

Script started on Wed Aug 11 07:21:39 2004

# file /dev/console
/dev/console: symbolic link to /dev/tty0

It might be a symlink on your machine, but that doesn't mean it's the right way. For almost 7 years, /dev/console is a separate device, not a symlink. Here's the relevant section from Documentation/devices.txt:

The console device, /dev/console, is the device to which system messages should be sent, and on which logins should be permitted in single-user mode. Starting with Linux 2.1.71, /dev/console is managed by the kernel; for previous versions it should be a symbolic link to either /dev/tty0, a specific virtual console such as /dev/tty1, or to a serial port primary (tty*, not cu*) device, depending on the configuration of the system.

Linux-2.1.71 was released on December 4, 1997. In your signature you claim to run linux-2.4.26. Please update your system.

Erik

Signature · 2 lines
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
#9 lore raw
Quoted 29 lines · Erik Mouw
Quoted 10 lines · Richard B. Johnson
Quoted 8 lines · Erik Mouw

Please don't mislead newbies, Richard. /dev/console is NOT a link to /dev/tty0, it's a completely different device:

erik@abra2:~ >ls -l /dev/console
crw------- 1 root tty 5, 1 Apr 7 09:13 /dev/console
erik@abra2:~ >ls -l /dev/tty0
crw------- 1 root tty 4, 0 Feb 10 2000 /dev/tty0

Quoted 5 lines

Bullshit. I know how to use file.

Script started on Wed Aug 11 07:21:39 2004

# file /dev/console
/dev/console: symbolic link to /dev/tty0

It might be a symlink on your machine, but that doesn't mean it's the right way. For almost 7 years, /dev/console is a separate device, not a symlink. Here's the relevant section from Documentation/devices.txt:

The console device, /dev/console, is the device to which system messages should be sent, and on which logins should be permitted in single-user mode. Starting with Linux 2.1.71, /dev/console is managed by the kernel; for previous versions it should be a symbolic link to either /dev/tty0, a specific virtual console such as /dev/tty1, or to a serial port primary (tty*, not cu*) device, depending on the configuration of the system.

Quoted 5 lines

Linux-2.1.71 was released on December 4, 1997. In your signature you claim to run linux-2.4.26. Please update your system.

Erik

RedHat is NOT Linux. The MAJOR-MINOR 5.1 used in RedHat for the console has a very serious problem for anybody doing development work. If there are any kernel messages, they go to __all__ open terminals. This means that the only "quiet" terminals that may be available to kill off a runaway process are available iff you can log in over the network.

Most everybody I know, who does serious development work, and certainly those who want to control where the %*)&$#!@ kernel messages go, will make sure they go to the "ALT-F1" as I have shown. The original query was about how to make kernel messages go to where, i.e., what VT do they come from. I have shown how you can control where they go.

Now, if you look at the kernel source, you will note that just prior to attempting to exec /sbin/init, /dev/console is opened. This means that you can properly use whatever you want if you continue to use a sym-link.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.26 on an i686 machine (5570.56 BogoMips).
Note 96.31% of all statistics are fiction.

#10 lore raw
Quoted 6 lines · Richard B. Johnson

RedHat is NOT Linux. The MAJOR-MINOR 5.1 used in RedHat for the console has a very serious problem for anybody doing development work. If there are any kernel messages, they go to __all__ open terminals. This means that the only "quiet" terminals that may be available to kill off a runaway process are available iff you can log in over the network.

If you don't want console messages, you just disable console at all, or you use a serial console (using the "console=" on the kernel command line), or you adjust the kernel console log level. Please have a look at the "dmesg" manual page, especially the "-n" parameter. In emergency situations, Alt-Sysrq-[0-8] will allow you to do the same (on VGA consoles for PCs, that is. for serial consoles use Break-[0-8]).

Quoted 4 lines

Most everybody I know, who does serious development work, and certainly those who want to control where the %*)&$#!@ kernel messages go, will make sure they go to the "ALT-F1" as I have shown.

Not all the world is a PC, there is a lot of linux beyond x86. Looks to me like you haven't done development on embedded systems that don't have any VGA terminal at all.

Getting kernel messages on a certain pseudo tty is just a matter of supplying the correct "console=" paramater to the kernel. For example:
"console=tty1" will get your console output on pseudo tty 1. Alternatively, you can configure syslog to write all kernel messages to a pseudo tty.

Quoted 3 lines

The original query was about how to make kernel messages go to where, i.e., what VT do they come from. I have shown how you can control where they go.

Here is the original question:

Thanks for the pointer! My user mode program is running. Any idea how to control which console it shows up on?

No reference on how to get kernel messages somewhere.

Quoted 4 lines

Now, if you look at the kernel source, you will note that just prior to attempting to exec /sbin/init, /dev/console is opened. This means that you can properly use whatever you want if you continue to use a sym-link.

That was true for linux < 2.1.71, which was released almost 7 years ago. Things have changed. Newer kernels use the "console=" kernel command line to select the console. With multiple the "console=" parameter, you can even have mutiple consoles.

Erik

Signature · 2 lines
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
#11 lore raw
In reply to Richard B. Johnson
Quoted 2 lines · Richard B. Johnson

RedHat is NOT Linux. The MAJOR-MINOR 5.1 used in RedHat for the console has a very serious problem for anybody doing development

Actually you might want to

a) man sysklogd
b) check devices.txt distributed with the kernel.

The former will explain why you are wrong and how to channel log messages, the latter will explain how you are further wrong.