linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* script relative shebang
@ 2016-05-31 21:47 Boris Rybalkin
  2016-05-31 23:02 ` Nicolai Stange
  0 siblings, 1 reply; 8+ messages in thread
From: Boris Rybalkin @ 2016-05-31 21:47 UTC (permalink / raw)
  To: linux-kernel

Hi All,

I would like to know if any changes to parsing '#!' script header line
are accepted in particular having ability to run interpreter from
relative to the script path?

Something like:

#!{dirname}/python/bin/python

Where {dirname} is a special keyword replaced with dirname of a script.

This is useful for apps which bring dependencies with them and do not
want to hardcode absolute paths as target fs layout is not known or
has to be flexible.

As I understand the implementation is here and I could probably provide a patch:
fs/binfmt_script.c

Sorry if this is not the right place to discuss these things.

Thank you.


--
Boris Rybalkin
ribalkin@gmail.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: script relative shebang
  2016-05-31 21:47 script relative shebang Boris Rybalkin
@ 2016-05-31 23:02 ` Nicolai Stange
  2016-06-01  8:00   ` Boris
  0 siblings, 1 reply; 8+ messages in thread
From: Nicolai Stange @ 2016-05-31 23:02 UTC (permalink / raw)
  To: Boris Rybalkin; +Cc: linux-kernel

Hi Boris,

Boris Rybalkin <ribalkin@gmail.com> writes:

> I would like to know if any changes to parsing '#!' script header line
> are accepted in particular having ability to run interpreter from
> relative to the script path?
>
> Something like:
>
> #!{dirname}/python/bin/python
>
> Where {dirname} is a special keyword replaced with dirname of a script.

Just for the record, this can already be done without any help from the
kernel:

Assuming the following demonstration directory layout

  <some_test_dir>/subdir/catself
  <some_test_dir>/relshebang

where catself.sh is your "interpreter":

  #!/bin/sh
  tail -n +2 $1

and relshebang is your script file invoking the toy interpreter from its
shebang as follows:

  #!/usr/bin/gawk {exit system("/bin/sh -c 'exec \"$(dirname \"$0\")\"/subdir/catself \"$0\"' " FILENAME);}
  Hello world.


You don't necessarily need to use gawk here, anything being able to do
system() and taking some code snippet from its first argument will
certainly work.

If this is too ugly, you could also write your own wrapper a la
/usr/bin/env and install that at some central location.


Best,

Nicolai

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: script relative shebang
  2016-05-31 23:02 ` Nicolai Stange
@ 2016-06-01  8:00   ` Boris
  2016-06-01 12:12     ` Bernd Petrovitsch
  2016-06-01 15:38     ` Austin S. Hemmelgarn
  0 siblings, 2 replies; 8+ messages in thread
From: Boris @ 2016-06-01  8:00 UTC (permalink / raw)
  To: Nicolai Stange; +Cc: linux-kernel

Hi Nicolai,

Yes, I think this is too ugly:

#!/usr/bin/gawk {exit system("/bin/sh -c 'exec \"$(dirname \"$0\")\"/subdir/catself \"$0\"' " FILENAME);}

Imagine you have that feature in your kernel would you rather use:

#!{dirname}/subdir/catself

You second advice involves changing root fs which is not desirable in copy-deployment apps (bring all the dependencies)

This more about making kernel "user" friendly.

Thank you.

On 1 June 2016 00:02:05 BST, Nicolai Stange <nicstange@gmail.com> wrote:
>Hi Boris,
>
>Boris Rybalkin <ribalkin@gmail.com> writes:
>
>> I would like to know if any changes to parsing '#!' script header
>line
>> are accepted in particular having ability to run interpreter from
>> relative to the script path?
>>
>> Something like:
>>
>> #!{dirname}/python/bin/python
>>
>> Where {dirname} is a special keyword replaced with dirname of a
>script.
>
>Just for the record, this can already be done without any help from the
>kernel:
>
>Assuming the following demonstration directory layout
>
>  <some_test_dir>/subdir/catself
>  <some_test_dir>/relshebang
>
>where catself.sh is your "interpreter":
>
>  #!/bin/sh
>  tail -n +2 $1
>
>and relshebang is your script file invoking the toy interpreter from
>its
>shebang as follows:
>
>#!/usr/bin/gawk {exit system("/bin/sh -c 'exec \"$(dirname
>\"$0\")\"/subdir/catself \"$0\"' " FILENAME);}
>  Hello world.
>
>
>You don't necessarily need to use gawk here, anything being able to do
>system() and taking some code snippet from its first argument will
>certainly work.
>
>If this is too ugly, you could also write your own wrapper a la
>/usr/bin/env and install that at some central location.
>
>
>Best,
>
>Nicolai

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: script relative shebang
  2016-06-01  8:00   ` Boris
@ 2016-06-01 12:12     ` Bernd Petrovitsch
  2016-06-01 15:38     ` Austin S. Hemmelgarn
  1 sibling, 0 replies; 8+ messages in thread
From: Bernd Petrovitsch @ 2016-06-01 12:12 UTC (permalink / raw)
  To: Boris, Nicolai Stange; +Cc: linux-kernel

Hi all!

On Wed, 2016-06-01 at 09:00 +0100, Boris wrote:
[...]
> Yes, I think this is too ugly:
> 
> #!/usr/bin/gawk {exit system("/bin/sh -c 'exec \"$(dirname
> \"$0\")\"/subdir/catself \"$0\"' " FILENAME);}

The simplest solution for an application today is to provide a wrapper
script/program (to be placed in /usr/bin or /usr/local/bin) which knows
where to search for the applications executables - if adding some
directories to $PATH is not enough.

> Imagine you have that feature in your kernel would you rather use:
> 
> #!{dirname}/subdir/catself

And what if "{dirname}" (or any other character sequence) happens to be
a valid pathname?

> You second advice involves changing root fs which is not desirable in
> copy-deployment apps (bring all the dependencies)
> 
> This more about making kernel "user" friendly.

User-friendlyness is not the job of the kernel ...

[ Fullquote deleted as it's a bad habit ]

MfG,
	Bernd
-- 
Bernd Petrovitsch                  Email : bernd@petrovitsch.priv.at
                     LUGA : http://www.luga.at

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: script relative shebang
  2016-06-01  8:00   ` Boris
  2016-06-01 12:12     ` Bernd Petrovitsch
@ 2016-06-01 15:38     ` Austin S. Hemmelgarn
  2016-06-02  0:04       ` Boris Rybalkin
  1 sibling, 1 reply; 8+ messages in thread
From: Austin S. Hemmelgarn @ 2016-06-01 15:38 UTC (permalink / raw)
  To: Boris; +Cc: Nicolai Stange, linux-kernel

On 2016-06-01 04:00, Boris wrote:
> Hi Nicolai,
>
> Yes, I think this is too ugly:
>
> #!/usr/bin/gawk {exit system("/bin/sh -c 'exec \"$(dirname \"$0\")\"/subdir/catself \"$0\"' " FILENAME);}
>
> Imagine you have that feature in your kernel would you rather use:
>
> #!{dirname}/subdir/catself
The problem with using a keyword is that current VFS standards on Linux 
mean that most filesystems that are native to Linux support _any_ 
character in a path name component except for a null byte.  Because of 
that, you can't from a practical perspective choose a keyword that is 
guaranteed to not clash with any path name, unless you want a 256 
character long keyword (which would be worse than the current option).
>
> You second advice involves changing root fs which is not desirable in copy-deployment apps (bring all the dependencies)
Not necessarily, include the wrapper in the app itself.  It's not hard 
even in shell script to figure out where you're being run from, so it's 
really not all that hard to handle this.

In bash, you can do the following in a script to get the canonical path 
to you're script (in an interactive shell, it will just point you at the 
bash executable):
SELF=$_
SELF=$(realpath ${SELF})

This only works if it's done at the top level of a script (not within a 
function).

If you go with the wrapper option, you can find your canonical path name 
in /proc/self/exe (note that this won't work right when doing something 
like `readlink /proc/self/exe` from an interpreter, as that will return 
the path to readlink), or just look up your PID and check /proc/PID/exe.

Also, the convention for such things on most UNIX like systems is to 
install wrappers, often using symlinks, in some location that is listed 
in $PATH.  For example, on many Linux systems, Dropbox gets installed 
into /opt/dropbox, and then a link for it is created in /opt/bin to 
point at the core program itself.  If it's a GUI only app, you could 
also just use .desktop files to point at the tools instead of symlinks, 
Google Chrome does this for example.

Using the bash example above, the following snippet could be used as a 
wrapper by placing it the top level directory of the above example and 
then symlinking to it from somewhere in $PATH:

#!/bin/bash
SELF=$_
SOURCE=$(dirname $(realpath ${SELF}))
exec ${SOURCE}/subdir/catself $@

You could then use that script as your interpreter.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: script relative shebang
  2016-06-01 15:38     ` Austin S. Hemmelgarn
@ 2016-06-02  0:04       ` Boris Rybalkin
  2016-06-02  4:19         ` Ken Moffat
  0 siblings, 1 reply; 8+ messages in thread
From: Boris Rybalkin @ 2016-06-02  0:04 UTC (permalink / raw)
  To: Austin S. Hemmelgarn; +Cc: Nicolai Stange, linux-kernel, Vladimir Sapronov

Sorry for insisting, but I would like to explore potential solutions
for fixing the root problem (missing relative shebang),
I know there are ways to workaround that, but I would like to make
sure the proper fix is not possible.
I understood that it is too late to introduce additional keywords
after #! as existing systems expect fs path there, OK.
But what about changing #! itself, is it possible to introduce another
special sequence like #? to denote a relative mode:

#?python/bin/python



-- 
Boris Rybalkin
ribalkin@gmail.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: script relative shebang
  2016-06-02  0:04       ` Boris Rybalkin
@ 2016-06-02  4:19         ` Ken Moffat
  2016-06-02 22:41           ` Boris
  0 siblings, 1 reply; 8+ messages in thread
From: Ken Moffat @ 2016-06-02  4:19 UTC (permalink / raw)
  To: Boris Rybalkin
  Cc: Austin S. Hemmelgarn, Nicolai Stange, linux-kernel, Vladimir Sapronov

On Thu, Jun 02, 2016 at 01:04:46AM +0100, Boris Rybalkin wrote:
> Sorry for insisting, but I would like to explore potential solutions
> for fixing the root problem (missing relative shebang),
> I know there are ways to workaround that, but I would like to make
> sure the proper fix is not possible.
> I understood that it is too late to introduce additional keywords
> after #! as existing systems expect fs path there, OK.
> But what about changing #! itself, is it possible to introduce another
> special sequence like #? to denote a relative mode:
> 
> #?python/bin/python
> 
If you are able to get that accepted, it will only work on linux
systems running such recent kernels.  For your own systems, you can
of course do whatever you wish.  But for public availability you
will then need to wait several years until your target linux users
can be expected to have moved to a suitable kernel (presumably the
*next* long-term stable kernel after the change is accepted : I
guess that version is perhaps the best part of a year away even if
your change got accepted into 4.8, and then you need your users'
distros to move to it).

To me, that doesn't seem worth the trouble (to you) of coding it,
getting it reviewed and eventually accepted, and then fixing up
whatever problems arise after it gets into linux-next [ problems
will always appear, even if the new code turns out not to be the
cause ].

And first, you have to persuade somebody influential that this is a
good thing to do, particularly when people have suggested
alternative approaches.  I don't count, but at the moment I've not
seen any good reasons why the kernel should be changed to support
this.

But it's your time, and your itch to scratch.

ĸen
-- 
I had to walk fifteen miles to school, barefoot in the snow.  Uphill both ways.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: script relative shebang
  2016-06-02  4:19         ` Ken Moffat
@ 2016-06-02 22:41           ` Boris
  0 siblings, 0 replies; 8+ messages in thread
From: Boris @ 2016-06-02 22:41 UTC (permalink / raw)
  To: Ken Moffat
  Cc: Austin S. Hemmelgarn, Nicolai Stange, linux-kernel, Vladimir Sapronov

I still think it is a good thing to do.
I will try to implement it on github and may be some day someone influential will help me with that :)

On 2 June 2016 05:19:34 BST, Ken Moffat <zarniwhoop@ntlworld.com> wrote:
>On Thu, Jun 02, 2016 at 01:04:46AM +0100, Boris Rybalkin wrote:
>> Sorry for insisting, but I would like to explore potential solutions
>> for fixing the root problem (missing relative shebang),
>> I know there are ways to workaround that, but I would like to make
>> sure the proper fix is not possible.
>> I understood that it is too late to introduce additional keywords
>> after #! as existing systems expect fs path there, OK.
>> But what about changing #! itself, is it possible to introduce
>another
>> special sequence like #? to denote a relative mode:
>> 
>> #?python/bin/python
>> 
>If you are able to get that accepted, it will only work on linux
>systems running such recent kernels.  For your own systems, you can
>of course do whatever you wish.  But for public availability you
>will then need to wait several years until your target linux users
>can be expected to have moved to a suitable kernel (presumably the
>*next* long-term stable kernel after the change is accepted : I
>guess that version is perhaps the best part of a year away even if
>your change got accepted into 4.8, and then you need your users'
>distros to move to it).
>
>To me, that doesn't seem worth the trouble (to you) of coding it,
>getting it reviewed and eventually accepted, and then fixing up
>whatever problems arise after it gets into linux-next [ problems
>will always appear, even if the new code turns out not to be the
>cause ].
>
>And first, you have to persuade somebody influential that this is a
>good thing to do, particularly when people have suggested
>alternative approaches.  I don't count, but at the moment I've not
>seen any good reasons why the kernel should be changed to support
>this.
>
>But it's your time, and your itch to scratch.
>
>ĸen

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-06-02 22:41 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-31 21:47 script relative shebang Boris Rybalkin
2016-05-31 23:02 ` Nicolai Stange
2016-06-01  8:00   ` Boris
2016-06-01 12:12     ` Bernd Petrovitsch
2016-06-01 15:38     ` Austin S. Hemmelgarn
2016-06-02  0:04       ` Boris Rybalkin
2016-06-02  4:19         ` Ken Moffat
2016-06-02 22:41           ` Boris

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).