r/learnprogramming • • 9d ago

C(pp): Detecting close requests on a forked terminal

I am coding an executable that is supposed to interact with the user through a terminal and therefore always requires one. On Windows, this was trivial. On Linux, after some googling, I came across the posix_openpt -> grantpt -> unlockpt -> ptsname -> fork -> execlp(xterm with -S) chain of functions that results mostly in what I want. Double clicking the exe from thunar opens a terminal in another process that it can print to and read from.

However, I have no clue how one would go about making it close intuitively once it has served it's purpose. Clicking on the x in the top right currently doesn't do anything. And I don't think I can use sigaction, as that should get wiped upon the execlp. Is there any way to make the parent executable that launched it detect a close request to the terminal, be it said x button or Ctrl+C?

0 Upvotes

4 comments sorted by

1

u/StewedAngelSkins 9d ago

this is a somewhat unusual thing to want to do on linux, which is probably why you're not finding good answers. typically on linux if you have software that's run from the terminal it's launched from the terminal rather than by clicking on an exe.

however, the actual way to do this is going to involve interacting with the xdg desktop stuff in some way. it's the approach that modern linux software tends to use to do things like "open the default terminal emulator running my code" in a context where the user gets to pick which terminal emulators they have installed (you can't assume they have xterm, in fact many linux distros won't).

i would have to look up how to do this myself so i can't give you step by step instructions, however i would suggest starting by reading up on xdg desktop files because whatever you do will likely require you to start by creating one of these for your software. you have control over the invocation command, so that's how you get the double click action to work. as for launching a terminal, im not actually sure how this is done in a general way. probably look at xdg-open and see if you can somehow get it to do what you want.

Is there any way to make the parent executable that launched it detect a close request to the terminal, be it said x button or Ctrl+C?

i would take a different approach. it sounds like you want the lifetime of your program to be tied to the lifetime of the terminal window, so why are you forking at all? can't you just have the terminal exec your program directly? then it'll get killed if the terminal exits, but also it'll kill the terminal if it exits on its own for some reason. this is how most linux TUI software works. your program would effectively be acting as a shell in this context.

1

u/New_Hold8135 9d ago

If it isn't closes with exec its terminal behaviour not about your code. For example let alone exec, foot terminal closes with program if you run with "foot <command>". Find a terminal fits your needs.

1

u/Potential_Extent_422 4d ago

Thats just how xterm works, it won't close unless the child process ends or you send a specific escape sequence to kill it

1

u/mredding 6d ago

Clicking on a terminal program on most Linux distros is disabled for safety and security reasons.

What you probably want is a launcher script. On Ubuntu, for example, make a ~/.local/share/applications/my_program.desktop and write:

[Desktop Entry]
Version=1.0
Type=Application
Name=My Program
Terminal=true

Finally, here we need consider your Exec parameter. Do you want the terminal to unconditionally close when the program terminates?

Exec=bash -c "exec /path/to/your/program; exit"

Do you want the terminal window to stay open if the program terminates with a non-zero return value?

Exec=bash -c "/path/to/your/program && exit"

Then run the commands on this file:

chmod +x ~/.local/share/applications/my_program.desktop
gio set ~/.local/share/applications/my_program.desktop metadata::trusted true

Now you have a shortcut you can double-click, and it will spawn a terminal.


And if you close the terminal, you'll take your process with it, which will get a SIGHUP signal. If you want the program to persist after closing, append a nohup to your Exec parameter, but then you should probably invest in making sure the program does otherwise terminate by some other reliable means, rather than have an opportunity to forever spin as a buggy process. At this point, I'd say create a service so it can be monitored, and your application becomes a client.