-
-
Notifications
You must be signed in to change notification settings - Fork 347
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
Script passed as pipe, fix for locks, support for python #153
base: master
Are you sure you want to change the base?
Conversation
src/shc.c
Outdated
@@ -149,6 +150,9 @@ static int MMAP2_flag = 0; | |||
static const char BUSYBOXON_line[] = | |||
"#define BUSYBOXON %d /* Define as 1 to enable work with busybox */\n"; | |||
static int BUSYBOXON_flag = 0; | |||
static const char PIPESCRIPT_line[] = | |||
"#define PIPESCRIPT %d /* Define as 1 to submit script as a pipe */\n"; | |||
static int PIPESCRIPT_flag; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please initialize to 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, but static variables are initialized to 0 anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do, but if you feel it is necessary to modify something, you should have push access to my fork.
However, if you prefer to ask me to operate on it, no problem at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, good job @fabio-brugnara, the PR fix the most important limitations of command line length limits. I'm chipping in to say that it's common practice for the PR raiser to fix things that the author reviewed, but I'm sure you know that as well. I know you're enjoying the fix yourself, but hope your contribution can serve the world as well. (I'm the DM of the shc Debian package, thanks)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @suntong, actually my intention was to follow author's indication and I immediately pushed a commit addressing the issue as requested, even if I felt it was not necessary. Maybe this is not the correct way to proceed? Is there something I overlooked?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @fabio-brugnara. I saw "New changes since you last viewed". Hope @neurobin can be satisfied to take the PR.
Hi @ashamedbit, would you take a look at this as well please?
I've integrated this in #168 as well. What is missing:
|
Hi @mdeweerd , thanks for your work. I pushed a commit modifying |
I've been working with it since a while now. Now I've updated the flow and the script so that more errors are flagged and an artifact is available for analysis. Check this out : https://github.com/mdeweerd/shc/actions/runs/10357725849 . The artefact can be download from summary. I'll let you have a look - I now now that it is in the "pipe" code - a multiplication exceeds the size of int (which is 32bit here even on a 64bit platform). Maybe it's easier that you start from my branch and make a PR to my repository (and enable actions in your fork). I also have some failures on WsL with csh, but I do think that is not related to this PR:
|
Hi @mdeweerd , I've seen the error report, very clear, thank you. The integer overflow is irrelevant: the multiplication is only used to generate pseudo-random numbers in order to create a unique file name for a fifo, it does not matter if the result "wraps around" the 32 bit limit. |
I am adding an annotation to skip the check there. |
Thank you @mdeweerd |
I had the annotation, but it's more lumpy than making i locally an unsigned. However, now the output is not the expected output: https://github.com/mdeweerd/shc/actions/runs/10359338552/job/28675539171 . |
Hi @mdeweerd , |
|
Ah, OK. So you could simply use an unsigned local variable just for that loop. Given the minimal amount of change, please allow me to propose the change right here without going through the fork and the PR:
|
You can update your PR to have it up-to-date. I do not know how this is going to go - ideally #168 would get merged into the main branch and possibly automatically close several PRs. |
Hi @mdeweerd , I updated my PR hopefully according to your suggestions. |
Hi @fabio-brugnara, just realized something -- How shc will handle
Hopefully not I know it has been an issue for all tools using pipes, like chenzhch/shellc#10, which is unacceptable / unusable for my cases. |
$ cat test.cmd
#!/bin/bash
echo "command line: $0"
$ ./shc -P -f test.cmd
$ ./test.cmd.x
command line: /tmp/1a09ab52
$ ./shc -f test.cmd
$ ./test.cmd.x
command line: ./test.cmd.x The result without -P is preferable. And hopefully there is a solution for it. |
Thanks for the report, mdeweerd. It's fixable, please see: https://github.com/liberize/ssc/blob/24e131be8cfc1799ca654440ad2e8c960962f69d/src/main.cpp#L230-L236 i.e., from it readme:
thx |
The non-piping version can "fake" The following would only help for shell scripts, so it's not a generic solution, but executing it shows the filename of the script, showing that sourcing the 'pipe' does not change $0. #!/bin/bash
rm ./tmpfifo >& /dev/null
mkfifo ./tmpfifo
echo 'echo "command line: $0"' > ./tmpfifo &
. ./tmpfifo |
I think that is wat's done when the -P option is not used as far as the debug output goes, with with the pipe option some argv settings are skipped. |
I don't have access to a PC now, so I answer by email. It's actually a
known issue that is mentioned in the PR description.
Try something like:
#!/usr/bin/bash
me=$_
echo I am $me
That is, take $_ in the very first command.
Il Gio 15 Ago 2024, 19:07 suntong ***@***.***> ha scritto:
… Hi @fabio-brugnara <https://github.com/fabio-brugnara>, just realized
something --
How shc will handle $0 after using pipes instead of command line to pass
the script to the interpreter? I.e., what'd the output be for the following?
echo "command line: $0"
Hopefully not /proc/self/fd/nn, as I'm using $0 to located related
resources for all my scripts all the time.
I know it has been an issue for all tools using pipes, like
chenzhch/shellc#10 <chenzhch/shellc#10>, which
is unacceptable / unusable for my cases.
—
Reply to this email directly, view it on GitHub
<#153 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ATJEP6S5ROESD6RUN45BWGDZRTN3HAVCNFSM6AAAAABMJ3OVWCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJRG4ZTENJZGY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Thanks for that @fabio-brugnara . I tested bash and csh. { "csh", "-c", "-b", "exec '%s' $argv" }, /* AIX: No file for $0 */ The following sample launches bash from a csh, but the core solution is the command to bash and the fact that the first argument is what you want to see in Therefore, a trick for most shells is to launch them with a #!/bin/csh
rm ./tmpfifo >& /dev/null
mkfifo ./tmpfifo
echo 'echo "command line: $0"' > ./tmpfifo &
echo "START SHELL"
bash -c "source ./tmpfifo" hello
echo "END SHELL"
rm ./tmpfifo >& /dev/null The output from the bash script is |
HI @mdeweerd , I created a branch |
I was testing with the current shc, v4.0.3-1, and I realized that my approach
|
@fabio-brugnara I'll have a look into your fork and check if I can find an approch for python. |
This seems an acceptable approach for python: cat > the_script.py <<EOF
import sys;
print("# Python's ARGV[0] is " + sys.argv[0] + "\n")
EOF
python -c 'import sys; sys.argv[0] = "forged_script_name.py"; exec(open("the_script.py").read())'
# Python's ARGV[0] is forged_script_name.py |
Hi @mdeweerd , thank you for the idea. I'll see if I find time today to put it inside the code. |
FYI, I've updated I noticed that while perl is supported in the script, it's also not showing the expected arguments. Result of a test run: Test script: |
For certen exec versions, the ( exec -a forgedname /bin/bash -c '/bin/printf $0' ) |
Hi @mdeweerd , please have a look at the last commit on branch argv0.
Hope it is useful. In this week I had some spare time to work on this, in the near future I don't think I will be able to do the same. |
@fabio-brugnara Thanks. I've added the ci flow to argv0 (which can be removed if needed later) where I disable the run with the sanitizer (as those issues still remain in this branch). I think I had better results on an earlier run (locally). |
Hi @mdeweerd , whit my last commit, I added support for perl (as with python, -P option is forced, there is nothing to lose). |
@fabio-brugnara Thank you for these updates. When running 'a.out' for that test, I get: $ ./a.out
Ambiguous output redirect. I also have a multitule of 'a.out' processes in my process list. |
By replacing the '-c' option with '-xvc', I found that the binary file is sourced with tcsh with '-P' active. I suspect that this is the same for other "shells":
|
I extended the test to verify the behavior with arguments containing spaces and quotes. There is still the sourcing of the binary to fix with |
Hi @mdeweerd , I fixed sourcing of binaries for csh/tcsh. I still see problems with rc and zsh. This happen because they set $0 also inside sourced scripts, so the trick does not work |
@fabio-brugnara That's great - I managed to forge $0 for zsh with the pipe option as well. |
Hi @mdeweerd , thank you. So you think the state of the software is acceptable now? (btw, I fixed a minor oversight in ttest.sh were the shells list was left with zsh only.) If we are finished, is it the case to merge the |
It's been an nice cooperation on this 😄 . Thanks for removing the line to limit to zsh which was the focus of my tests. There are just two things that remain IMHO:
I'll merge the updates in my integration branch #168 . While it's vacation time, I think @neurobin has not been responding on this project for about a year, so we need to consider having a new "official" fork. |
Yes, nice cooperation AND great achievement indeed!!! I checked the following first thing in the morning then come back here to congratulate you two. https://github.com/mdeweerd/shc/actions/runs/10440900647/job/28911372960
Hmm... yes I agree with you theoretically, but just to not confuse people coming from
TOTALLY agree.
I was hoping that either @neurobin or @ashamedbit would participate in the work of the new shc release, but since neither of them responded, and @neurobin has not been responding on this project for about a year, I too think it is about time. @mdeweerd, I know that you has expressed that you don't want to be the maintainer of We can also create a shc GH organization, to share the maintenance responsibilities, but just an ideal, totally up to you two to decide. |
Hi @mdeweerd , @suntong I'm also grateful to you for this nice and effective collaboration. As for the option, I made it opt-out through I've also forced I also think that there is little chance that this PR will be taken into consideration given the lack of reaction of the current maintainer. As for the candidate for a new mantainer, I agree in seeing @mdeweerd a perfect match for what I've seen, but of course the role has to accepted willingly. |
Ok, I'll manage the (interrim) repo to merge requests (and interact for those), keep the code up-to-date, but not as such to fix/solve/respond to issues. I am busy myself, but a long time ago somebody told me that this was the reason: busy people can always do more he said (implying that the others don't and because that's there's a skill to know how to "be busy"). I checked the GH Organisation option, The evolution for '-p' is great, but this leaves the '-0' option open. We can rename it, but I feel that the feature should be 'on' by default. I'll merge the latest changes once we settled on the -0 option (I need to go to a few steps to merge: apply the code formatting on a separate branch, then merge that branch). @fabio-brugnara and myself both lifted some weight - there is a net improvement to shc with the changes made whilst working on this PR (the testcases prove it). |
Hi @mdeweerd , thank you for your availability to take care of the repo. I'll try to be of support, even without an "official" role. Consider that my involvement in shc was related only to this pipe thing, mainly motivated by the reason that exposing the code through /proc/*/cmdline seemed too risky to me. (Well, and the other question of the occasional lockups that I met). As for the Now that I think of it, we could make the two options alternatives:
Does this make more sense in your opinion (and @suntong's)? |
@fabio-brugnara Thanks for trying to help out. Regarding the options, this looks better to me:
|
OK, then, since you two agree with each other, and yes, I agree, it's better than -p amending -P.
Oh, yeah, what a shame, someone took it but abandoned it. Anyway, So, maybe we make a little bit change, like go project tends to be named as go-xx, i.e., we can call the org c-shc, but I actually don't like that. Maybe theshc, the-shc, shc-proj, proj-shc ..? Worse case, we spell it out -- shell-script-compiler :-) :-) |
Well, the tool is historically SHC, not well defined, but supposedly ShCompiler. I think ShcTeam, OpenSHC, NextSHC are more appropriate choices. |
LGTM, all good, go ahead with your top choice first then, :) |
Hi @mdeweerd , I pushed a commit that gives the meaning we agreed upon to the options -P, -p. |
Or, we can wait and see what the outcome would be, from cheers |
@fabio-brugnara Your changes have been integreted. I've also added tests for the new option ( And a "milestone" has been reached - 100 commits ahead of master in the integration branch. |
Oh, it was promptly declined. Well, at least I tried, and asked very nicely and convincingly. No regret anymore. :) |
This PR proposes a solution (at least in my experience), for a couple of questions appearing among the Issues.
A drawback of the "pipe" feature, is the fact that the script may lose the possibility to retrieve the original name by accessing $0. Often this can be worked around referring to the $_ environment variable, but not always. In exchange for this annoyance, the limit on the script size and the possibility of seeing the source in clear with ps or /proc/*/cmdline both disappear.
These modifications have been applied to the version found in Francisco Rosales' page and have been working reliably for years under linux.