serenity icon indicating copy to clipboard operation
serenity copied to clipboard

Utilites: Implement nohup

Open kemzeb opened this issue 1 year ago • 7 comments

This snapshot implements the nohup command-line utility.

Screenshot 2023-07-01 212923

Running it in a directory with root permissions while being anon: Screenshot 2023-07-01 213358

kemzeb avatar Jul 02 '23 04:07 kemzeb

One thing important to note, I have tried to run a command such as nohup sleep 8, where I would exit the terminal after invoking it to see if SIGHUP is ignored. It appears to work after the terminal has exited, but when sleep ends I will get the following stack trace:

849.832 [#0 Finalizer Task(5:5)]: Generating coredump for pid: 85
849.835 CrashDaemon(34:34): New coredump file: /tmp/coredump/Shell_85_1688272836
849.916 CrashReporter(91:91): Started Thread "Background Thread"(92)
850.585 CrashReporter(91:92): Generating backtrace took 669 ms
850.589 CrashReporter(91:92): --- Backtrace for thread #0 (TID 85) ---
850.589 CrashReporter(91:92): 0x00000011bd09a02b: [/usr/lib/libsystem.so] syscall2 +0xb (syscall.cpp:25 => syscall.cpp:24)
850.597 CrashReporter(91:92): 0x00000010ab71c50d: [/usr/lib/libc.so] abort +0x26 (stdlib.cpp:386)
850.601 CrashReporter(91:92): 0x00000010ab722a15: [/usr/lib/libc.so] __assertion_failed.localalias +0x85 (assert.cpp:34)
850.607 CrashReporter(91:92): 0x0000000bc43ceac3: [/usr/lib/libline.so.serenity] Line::Editor::get_line(AK::DeprecatedString const&) +0x473 (Error.h:202 => Editor.cpp:750)
850.614 CrashReporter(91:92): 0x0000001691239826: [/usr/lib/libshell.so.serenity] Shell::Shell::read_single_line() +0xc6 (Shell.cpp:2097)
850.619 CrashReporter(91:92): 0x0000001691239b97: [/usr/lib/libshell.so.serenity] Shell::Shell::custom_event(Core::CustomEvent&) +0x47 (Shell.cpp:2134 => Shell.cpp:2131)
850.625 CrashReporter(91:92): 0x0000000e92f4fba7: [/usr/lib/libcore.so.serenity] Core::Object::dispatch_event(Core::Event&, Core::Object*) +0x57 (Object.cpp:214)
850.631 CrashReporter(91:92): 0x0000000e92f685fb: [/usr/lib/libcore.so.serenity] Core::ThreadEventQueue::process() +0x2eb (ThreadEventQueue.cpp:111)
850.636 CrashReporter(91:92): 0x0000000e92f46953: [/usr/lib/libcore.so.serenity] Core::EventLoopImplementationUnix::exec() +0x33 (EventLoopImplementationUnix.cpp:109 => EventLoopImplementationUnix.cpp:101)
850.647 CrashReporter(91:92): 0x0000000e92f4319d: [/usr/lib/libcore.so.serenity] Core::EventLoop::exec() +0x6d (EventLoop.cpp:75)
850.654 CrashReporter(91:92): 0x000000034906fab1: [/bin/Shell] serenity_main(Main::Arguments) +0x16a1 (main.cpp:273)
850.659 CrashReporter(91:92): 0x000000034906c6ea: [/bin/Shell] main +0x14a (Main.cpp:43)
850.666 CrashReporter(91:92): 0x000000034906c888: [/bin/Shell] _entry +0x48 (crt0.cpp:50)

Note that I truncated it as I noticed that running the top command would produce the same stack trace. See #19742.

kemzeb avatar Jul 02 '23 04:07 kemzeb

Forgot to mention that I had trouble manually testing the code path relating to the "stderr redirects to stdout" error as I had trouble porting gcc to Serenity, but will keep trying!

kemzeb avatar Jul 02 '23 05:07 kemzeb

I converted this as a draft PR for now as it looks like I may need to make changes as to how I make my isatty checks

kemzeb avatar Jul 02 '23 06:07 kemzeb

My most recent changes add some error handling so that I can detect if either stdout or stderr are open (the isatty() call allows me to perform this check) and some refactoring.

I have also been able to test the code path I mentioned earlier by closing stdout (just learned about the >&- operator!):

Screenshot 2023-07-02 002513

Screenshot 2023-07-01 223413

kemzeb avatar Jul 02 '23 07:07 kemzeb

Whoops! I should be using the returned boolean value of isatty() instead of assuming that an error means false and no error means true. My recent change reflects this.

kemzeb avatar Jul 02 '23 07:07 kemzeb

Just rebased to try out the changes made in #19749, where I found that invoking nohup sleep 8 and exiting Terminal doesn't cause Shell to crash anymore.

kemzeb avatar Jul 03 '23 17:07 kemzeb

The most recent change includes a commit that adds the nohup man page.

kemzeb avatar Jul 09 '23 01:07 kemzeb

Thanks!

kemzeb avatar Jul 12 '23 06:07 kemzeb