Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command capture bug in linux_procfs save strategy #449

Closed
mdeguzis opened this issue Aug 15, 2022 · 6 comments
Closed

Command capture bug in linux_procfs save strategy #449

mdeguzis opened this issue Aug 15, 2022 · 6 comments

Comments

@mdeguzis
Copy link
Contributor

mdeguzis commented Aug 15, 2022

For example, run:

vim --cmd "set pythonthreehome=/usr/bin/python3" /tmp/a 

Then check ps:

 ps -auwwx | grep vim
deguzim  52230  0.3  0.0 164936 12452 pts/0    S+   17:47   0:00 vim --cmd set pythonthreehome=/usr/bin/python3 /tmp/a   

They ideally need to use /proc//cmdline:

$ for i in /proc/5348*/ 
do
    basename "$i"
    < $i/cmdline xargs -0 zsh -c 'printf "\t%q" "$0" "$@"'
    printf '\n'
done

Or just one PID:

cat /proc/53482/cmdline | xargs -0 zsh -c 'printf "\t%q" "$0" "$@"'

Output:

53482
        vim      --cmd   set\ pythonthreehome=/apollo/env/envImprovement/python3.8       /tmp/a

This is a valid command:

vim     --cmd   set\ pythonthreehome=/apollo/env/envImprovement/python3.8       /tmp/a

man printf

%q ARGUMENT is printed in a format that can be reused as shell input, escaping non-print‐
able characters with the proposed POSIX $'' syntax.

@bruno-
Copy link
Member

bruno- commented Aug 15, 2022

Try using linux_procfs strategy for saving commands:

set @resurrect-save-command-strategy "linux_procfs"

All the available strategies are in save_command_strategies/ dir in the repo.

I just realized we don't have any docs for this... if you have some time to briefly explain the steps you did to try this, a PR would be welcome.

@mdeguzis
Copy link
Contributor Author

mdeguzis commented Aug 15, 2022

I saved again and see now it's the command without args

pane    work    1       :project-tests      0       :-      0       :/workplace/deguzim/project/src/project 1       vim     :
pane    work    1       :project-tests      0       :-      1       :/workplace/deguzim/project/src/project 0       zsh     :

With that "linux_procfs" outputs, isn't quite right it seems. Can you test what I have below? Would you accept a PR?

upsteam:

$ cat /proc/40353/cmdline | xargs -0 printf "%q "
printf: %q: invalid conversion specification

$ cat /proc/40353/cmdline                        
vim--cmdset pythonthreehome=/apollo/env/envImprovement/python3.8/home/deguzim/.tmux.conf%

Proposed:

$ cat /proc/40353/cmdline | xargs -0 bash -c 'printf "%q " "$0" "$@"'                           
vim --cmd set\ pythonthreehome=/apollo/env/envImprovement/python3.8 /home/deguzim/.tmux.conf

This 100% did the trick for me locally:

   14 full_command() {
   15     [[ -z "$COMMAND_PID" ]] && exit 0
~  16     #cat /proc/${COMMAND_PID}/cmdline | xargs -0 printf "%q "
+  17     cat /proc/${COMMAND_PID}/cmdline | xargs -0 bash -c 'printf "%q " "$0" "$@"'
   18 }

%q: escape/substitution
$0: command basename (e.g. vim)
$@: the rest of the full command

The original command doesn't save the full args.

When I did a restore after tmux kill-server && rm -rf /tmp/tmux-*, it worked perfect.

Note that this will not work:

cat /proc/${COMMAND_PID}/cmdline | xargs -0 printf "%q " "$0" "$@"

Some reasoning for this subshell usage can be found here: https://unix.stackexchange.com/a/567021

@mdeguzis mdeguzis changed the title Please use /proc/<PID>/cmdline line NOT ps... Command capture bug in linux_procfs save strategy Aug 15, 2022
@bruno-
Copy link
Member

bruno- commented Aug 16, 2022

Hi,

I'm not on Linux so I can't test this easily.

Yes, a PR for this is welcome.

@bruno-
Copy link
Member

bruno- commented Aug 16, 2022

Just please document why bash -c with those printf interpolations is used. A link to that stackexchange topic in the comments should suffice.

@mdeguzis
Copy link
Contributor Author

submitted #450

@bruno-
Copy link
Member

bruno- commented Aug 17, 2022

We just merged #450, so I think this one can be closed.

@bruno- bruno- closed this as completed Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants