Skip to content

"tcsetpgrp: No such process" when running in firejail #3345

Closed

Description

@ivan
  • Have you checked if problem occurs with fish 2.3.1?
  • Tried fish without third-party customizations (check sh -c 'env HOME=$(mktemp -d) fish')?

fish version installed (fish --version): fish, version 2.3.1

OS/terminal used: Ubuntu 16.04.1, roxterm

Repro steps:

  1. Install firejail on Ubuntu 16.04.1, either with apt-get install firejail, or from source (tested this commit)

  2. Add an alias to config.fish:

    echo "alias ls='ls -a'" >> ~/.config/fish/config.fish
  3. Run firejail --noprofile fish:

# firejail --noprofile fish
Parent pid 12650, child pid 12651
Child process initialized
fish: Could not return shell to foreground
tcsetpgrp: No such process
fish: Could not return shell to foreground
tcsetpgrp: No such process
fish: Could not return shell to foreground
tcsetpgrp: No such process
fish: Could not return shell to foreground
tcsetpgrp: No such process
fish: Could not return shell to foreground
tcsetpgrp: No such process
fish: Could not return shell to foreground
tcsetpgrp: No such process

fish doesn't start up properly and goes in a suspended state in the background:

# jobs
[1]    suspended (tty input)  firejail --noprofile fish

An alias or umask (both autoloaded builtins? use eval) seem to trigger this, but only if they're run from config.fish during startup. Both alias and umask work fine after fish has already started.

This probably happens because firejail uses namespaces to lock down the PIDs the jail knows about. ps ux inside the jail lists:

> ps ux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
at           1  0.0  0.0  14148  2036 pts/4    S    01:39   0:00 firejail --noprofile fish
at           2  0.0  0.2 172148  5948 pts/4    S    01:39   0:00 fish
at         115  0.0  0.1  37364  3324 pts/4    R+   01:41   0:00 ps ux

I have no idea why this only breaks during startup, though.

Activity

  1. ivan commented on Sep 1, 2016

    @ivan
    Author

    There is probably a better workaround, but right now I am just running alias a little later with:

    set -g __fish_setup_done 0
    
    function finish_setup
        if [ $__fish_setup_done = 1 ]
            return
        end
    
        set __fish_setup_done 1
    
        # [aliases and other things that break at startup]
    end
    
    function fish_prompt
        finish_setup
    
        # [...]
    end
  2. krader1961 commented on Sep 1, 2016

    @krader1961
    Contributor

    FWIW I can reproduce this. What is very odd is that leaving my normal, relatively complex, configuration in place but only removing the alias commands allows the shell to startup. Adding a single alias (the details don't matter) breaks it as reported in the original comment. Replacing the alias command with an explicit function definition also "fixes" the problem. That is, if instead of

    alias ll 'ls -blF'
    

    in my startup config I do

    function ll
        ls -blF $argv
    end
    

    The shell starts and the "alias" is defined and usable. Since alias is supposed to be just a shorthand for that function definition the difference in behavior is surprising. So I'm inclined to consider this a fish bug.

  3. floam commented on Sep 1, 2016

    @floam
    Member

    Looks familiar. I had thought I already reported this on our issue board, but I had not (or I can't find it): puma/puma-dev#69 (comment) on OS X.

    Also went away for him when removing his aliases. I would have figured the net result of wrapping those commands was going to be to blame. Interesting.

  4. modified the milestones: , fish-future on Sep 1, 2016
  5. faho commented on Sep 1, 2016

    @faho
    Member

    Alias doesn't do anything all that complicated, but it executes eval.

    And eval does some weird stuff.

    There's this

        set -l mode
        if status --is-interactive-job-control
            set mode interactive
        else
            if status --is-full-job-control
                set mode full
            else
                set mode none
            end
        end
        if status --is-interactive
            status --job-control full
        end
        # (some stuff cut to concentrate on the job control)
        status --job-control $mode

    and this

    echo "begin; $argv "\n" ;end <&3 3<&-" | source 3<&0

    @ivan: Can you try these lines on their own (obviously replace $argv with some fish code)?

    (We could probably replace the use of eval in alias with source, but that wouldn't solve the underlying problem - might be cleaner and faster though)

    (EDIT: I overlooked some important lines for the first piece of code)

  6. faho commented on Sep 1, 2016

    @faho
    Member

    Yes, the job control stuff does it.

    In particular, what happens here is that status --is-interactive-job-control is true (which means mode gets set to interactive), status --is-interactive also is (which means status --job-control full is executed) and then status --job-control interactive is run.

    The offending call is status --job-control full - commenting that out removes the error.

  7. added a commit that references this issue on Sep 1, 2016
  8. faho commented on Sep 1, 2016

    @faho
    Member

    @ivan: 443fc66 should avoid triggering the error with alias (and additonally speed up alias a bit).

    This should still be kept open as the underlying issue - that we try to set job control where we can't - remains.

  9. floam commented on Sep 1, 2016

    @floam
    Member

    When did job control stop working here? Did it never work in these situations? Should it not?

  10. faho commented on Sep 1, 2016

    @faho
    Member

    Okay, I've got some debug output:

    <3> fish: Set status of echo "Setting job control $mode" to 0 using short circuit
    <3> fish: Job is constructed
    <4> fish: Continue job 2, gid 0 (echo "Setting job control $mode"), COMPLETED, NON-INTERACTIVE
    <W> fish: Konnte Shell nicht wieder in Vordergrund zurückholen
    tcsetpgrp: Kein passender Prozess gefunden
    <4> fish: Exec job 'status --job-control $mode' with id 2
    <3> fish: Skipping fork: no output for internal builtin 'status'
    <3> fish: Set status of status --job-control $mode to 0 using short circuit

    (The german messages here are "Could not return shell to foreground" and "No such process")

    What's interesting here is that the job right before setting the job control mode (which is echo "Setting job control $mode") says it is "NON-INTERACTIVE". After this, sourcing config.fish is done and then fish goes interactive.

    Also, this is basically #1245 and #333.

  11. assigned and unassigned on Sep 6, 2016
  12. faho commented on Mar 31, 2017

    @faho
    Member

    Okay.... The issue here is as follows:

    • Fish starts eval something, which turns on more job control stuff (technical term) in config.fish

    • This causes it to start the job in its own process group and hand it control of the terminal

    • Then the job quits

    • Then fish sets up its terminal stuff (reader_interactive_init)

    • Fish figures it's not in control of the terminal (because tcgetpgrp still returns the finished job's pgid), and (for reasons I've yet to determine) tries determining if it is an orphan

    • This check involves reading outside of the foreground process group (which causes a SIGTTIN, which stops fish), and even outright sending SIGTTIN to its own process group, both of which cause fish to be stopped

    Removing these checks (i.e. if (false && tcgetpgrp(STDIN_FILENO) != shell_pgid)) makes everything work^tm.

    TBD is how firejail is special here - it's possible it's blocking us from blocking SIGTTIN (I haven't really looked at our signal handling).

    So, the nicest thing here to do would be to figure out that the pgid we received is bogus (tcgetpgrp returns "some value larger than 1 that is not presently a process group ID" in this case) and that the terminal is hence up for grabs, and just grab it. I have no idea how to do that since I haven't found a function that does this.

    Alternatively, the orphan logic could use reworking (or possibly removal - if we get SIGTTIN here anyway, we could just receive that later when we tcsetpgrp IIUC).

    Or we could init the reader stuff earlier.

    @ridiculousfish: Since you wrote that code: Words, possibly emojis?

  13. added a commit that references this issue on Jul 17, 2017
  14. mqudsi commented on Mar 10, 2018

    @mqudsi
    Contributor

    I just tested this and it doesn't seem to reproduce under current fish builds. Note that we dropped the tcsetpgrp in fish startup in 3.0, which is probably why this now works.

  15. modified the milestones: fish-future, on Mar 13, 2018
  16. locked as resolved and limited conversation to collaborators on Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions