git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH] maintenance: specify explicit stdin for crontab
@ 2021-03-29 21:09 Kevin Daudt
  2021-03-30  5:41 ` Martin Ågren
  0 siblings, 1 reply; 7+ messages in thread
From: Kevin Daudt @ 2021-03-29 21:09 UTC (permalink / raw)
  To: git; +Cc: Derrick Stolee, Kevin Daudt

There are multiple crontab implementations that require stdin for
editing a crontab to be explicitly specified as '-'.

BusyBox crontab just shows usage when executed without arguments.
[man 1 crontab][0] from openbsd states:

> The pseudo-filename '-' must be specified to read from standard
> input.

Other implementations might not require it, but accept '-', so
explicitly specify that crontab should read from stdin by passing the
'-' pseudo-filename.

[0]: https://www.unix.com/man-page/freebsd/1/crontab/

Signed-off-by: Kevin Daudt <me@ikke.info>
---
The `git maintenance start` command is currently broken on the default
alpine setup. This would fix it. I've tested `echo '* * * * * echo test'
| crontab -` on the different platforms I have access to, and it worked
without issues..
 builtin/gc.c            | 1 +
 t/helper/test-crontab.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

diff --git a/builtin/gc.c b/builtin/gc.c
index ef7226d7b..dfdb5bce9 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -1904,6 +1904,7 @@ static int crontab_update_schedule(int run_maintenance, int fd, const char *cmd)
 	rewind(cron_list);
 
 	strvec_split(&crontab_edit.args, cmd);
+	strvec_push(&crontab_edit.args, "-");
 	crontab_edit.in = -1;
 	crontab_edit.git_cmd = 0;
 
diff --git a/t/helper/test-crontab.c b/t/helper/test-crontab.c
index e7c0137a4..525cb318a 100644
--- a/t/helper/test-crontab.c
+++ b/t/helper/test-crontab.c
@@ -17,7 +17,7 @@ int cmd__crontab(int argc, const char **argv)
 		if (!from)
 			return 0;
 		to = stdout;
-	} else if (argc == 2) {
+	} else if ((argc == 3 && !strcmp(argv[2], "-")) || argc == 2) {
 		from = stdin;
 		to = fopen(argv[1], "w");
 	} else
-- 
2.30.1


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

* Re: [PATCH] maintenance: specify explicit stdin for crontab
  2021-03-29 21:09 [PATCH] maintenance: specify explicit stdin for crontab Kevin Daudt
@ 2021-03-30  5:41 ` Martin Ågren
  2021-03-30 12:02   ` Derrick Stolee
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Ågren @ 2021-03-30  5:41 UTC (permalink / raw)
  To: Kevin Daudt; +Cc: Git Mailing List, Derrick Stolee

On Mon, 29 Mar 2021 at 23:23, Kevin Daudt <me@ikke.info> wrote:
>
> There are multiple crontab implementations that require stdin for
> editing a crontab to be explicitly specified as '-'.

[...]

> --- a/t/helper/test-crontab.c
> +++ b/t/helper/test-crontab.c
> @@ -17,7 +17,7 @@ int cmd__crontab(int argc, const char **argv)
>                 if (!from)
>                         return 0;
>                 to = stdout;
> -       } else if (argc == 2) {
> +       } else if ((argc == 3 && !strcmp(argv[2], "-")) || argc == 2) {
>                 from = stdin;
>                 to = fopen(argv[1], "w");

Would it make sense to make this

  } else if (argc == 3 && !strcmp(argv[2], "-")) {

in order to make this test-tool as picky as possible and to only accept
the kind of usage we want to (well, need to) use? The tests as they
stand would still pass, which I think argues for us not really needing
that "argc == 2".

This would be followed by

  } else
          return error("unknown arguments");

which wouldn't be super helpful if you forgot the "-", but helpful
enough for an internal test-tool, I guess.

Speaking of usage and hints, there's "Usage: ..." in a comment at the
top of this file. It should probably be updated either way.

Martin

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

* Re: [PATCH] maintenance: specify explicit stdin for crontab
  2021-03-30  5:41 ` Martin Ågren
@ 2021-03-30 12:02   ` Derrick Stolee
  2021-03-30 17:12     ` Kevin Daudt
  2021-03-30 17:43     ` Todd Zullinger
  0 siblings, 2 replies; 7+ messages in thread
From: Derrick Stolee @ 2021-03-30 12:02 UTC (permalink / raw)
  To: Martin Ågren, Kevin Daudt; +Cc: Git Mailing List

On 3/30/2021 1:41 AM, Martin Ågren wrote:
> On Mon, 29 Mar 2021 at 23:23, Kevin Daudt <me@ikke.info> wrote:
>>
>> There are multiple crontab implementations that require stdin for
>> editing a crontab to be explicitly specified as '-'.

Thank you for reporting this, especially with a patch!

However, I'm not sure about this adding of '-' being something that
crontab ignores so commonly. My Ubuntu machine reports this:

$ crontab -e -
crontab: usage error: no arguments permitted after this option
usage:  crontab [-u user] file
        crontab [ -u user ] [ -i ] { -e | -l | -r }
                (default operation is replace, per 1003.2)
        -e      (edit user's crontab)
        -l      (list user's crontab)
        -r      (delete user's crontab)
        -i      (prompt before deleting user's crontab)

Is there a way we could attempt writing over stdin, notice the
failure, then retry with the '-' option?

> 
> [...]
> 
>> --- a/t/helper/test-crontab.c
>> +++ b/t/helper/test-crontab.c
>> @@ -17,7 +17,7 @@ int cmd__crontab(int argc, const char **argv)
>>                 if (!from)
>>                         return 0;
>>                 to = stdout;
>> -       } else if (argc == 2) {
>> +       } else if ((argc == 3 && !strcmp(argv[2], "-")) || argc == 2) {
>>                 from = stdin;
>>                 to = fopen(argv[1], "w");
> 
> Would it make sense to make this
> 
>   } else if (argc == 3 && !strcmp(argv[2], "-")) {
> 
> in order to make this test-tool as picky as possible and to only accept
> the kind of usage we want to (well, need to) use? The tests as they
> stand would still pass, which I think argues for us not really needing
> that "argc == 2".
> 
> This would be followed by
> 
>   } else
>           return error("unknown arguments");
> 
> which wouldn't be super helpful if you forgot the "-", but helpful
> enough for an internal test-tool, I guess.
>
> Speaking of usage and hints, there's "Usage: ..." in a comment at the
> top of this file. It should probably be updated either way.

I agree with Martin's review here, too.

Thanks,
-Stolee

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

* Re: [PATCH] maintenance: specify explicit stdin for crontab
  2021-03-30 12:02   ` Derrick Stolee
@ 2021-03-30 17:12     ` Kevin Daudt
  2021-03-30 19:32       ` Derrick Stolee
  2021-03-30 17:43     ` Todd Zullinger
  1 sibling, 1 reply; 7+ messages in thread
From: Kevin Daudt @ 2021-03-30 17:12 UTC (permalink / raw)
  To: Derrick Stolee; +Cc: Martin Ågren, Git Mailing List

On Tue, Mar 30, 2021 at 08:02:22AM -0400, Derrick Stolee wrote:
> On 3/30/2021 1:41 AM, Martin Ågren wrote:
> > On Mon, 29 Mar 2021 at 23:23, Kevin Daudt <me@ikke.info> wrote:
> >>
> >> There are multiple crontab implementations that require stdin for
> >> editing a crontab to be explicitly specified as '-'.
> 
> Thank you for reporting this, especially with a patch!
> 
> However, I'm not sure about this adding of '-' being something that
> crontab ignores so commonly. My Ubuntu machine reports this:
> 
> $ crontab -e -
> crontab: usage error: no arguments permitted after this option
> usage:  crontab [-u user] file
>         crontab [ -u user ] [ -i ] { -e | -l | -r }
>                 (default operation is replace, per 1003.2)
>         -e      (edit user's crontab)
>         -l      (list user's crontab)
>         -r      (delete user's crontab)
>         -i      (prompt before deleting user's crontab)
> 
> Is there a way we could attempt writing over stdin, notice the
> failure, then retry with the '-' option?

We do not use -e to edit, we run `crontab` and provide the contents to
stdin. `crontab -e` just opens the crontab in the users editor, which
would work with busybox as well, but that's not what's being done here.

> 
> > 
> > [...]
> > 
> >> --- a/t/helper/test-crontab.c
> >> +++ b/t/helper/test-crontab.c
> >> @@ -17,7 +17,7 @@ int cmd__crontab(int argc, const char **argv)
> >>                 if (!from)
> >>                         return 0;
> >>                 to = stdout;
> >> -       } else if (argc == 2) {
> >> +       } else if ((argc == 3 && !strcmp(argv[2], "-")) || argc == 2) {
> >>                 from = stdin;
> >>                 to = fopen(argv[1], "w");
> > 
> > Would it make sense to make this
> > 
> >   } else if (argc == 3 && !strcmp(argv[2], "-")) {
> > 
> > in order to make this test-tool as picky as possible and to only accept
> > the kind of usage we want to (well, need to) use? The tests as they
> > stand would still pass, which I think argues for us not really needing
> > that "argc == 2".
> > 
> > This would be followed by
> > 
> >   } else
> >           return error("unknown arguments");
> > 
> > which wouldn't be super helpful if you forgot the "-", but helpful
> > enough for an internal test-tool, I guess.
> >
> > Speaking of usage and hints, there's "Usage: ..." in a comment at the
> > top of this file. It should probably be updated either way.
> 
> I agree with Martin's review here, too.

Yes, I agree too, was already contemplating that.

> 
> Thanks,
> -Stolee

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

* Re: [PATCH] maintenance: specify explicit stdin for crontab
  2021-03-30 12:02   ` Derrick Stolee
  2021-03-30 17:12     ` Kevin Daudt
@ 2021-03-30 17:43     ` Todd Zullinger
  2021-03-30 19:38       ` Derrick Stolee
  1 sibling, 1 reply; 7+ messages in thread
From: Todd Zullinger @ 2021-03-30 17:43 UTC (permalink / raw)
  To: Derrick Stolee; +Cc: Martin Ågren, Kevin Daudt, Git Mailing List

Hi,

Derrick Stolee wrote:
> On 3/30/2021 1:41 AM, Martin Ågren wrote:
>> On Mon, 29 Mar 2021 at 23:23, Kevin Daudt <me@ikke.info> wrote:
>>>
>>> There are multiple crontab implementations that require stdin for
>>> editing a crontab to be explicitly specified as '-'.

Amusingly, I wrote the exact same patch 2 weeks ago
(including not dropping the `argc == 2` which Martin
mentioned).  That was in response to a report in the Fedora
bugzilla:

    https://bugzilla.redhat.com/1939930

I thought cronie might be rather rare with it's non-POSIX
handling of crontab without arguments.

In the end, the cronie folks upstream adjusted things so
that crontab behaves as defined by POSIX if stdin is not a
TTY:

    https://github.com/cronie-crond/cronie/commit/8b0241f

That allows cronie to behave more sensibly for interactive
use without breaking tools like git maintenance.  And it let
me sidestep proposing a patch to git (or worse, maintaining
it in the Fedora packages).

But I didn't dig in to find out whether or how many other
crontab implemntations had also eschewed the (rather poor)
POSIX-confirming behavior.  Knowing there are several among
popular OS's makes it easy to see something like this patch
being generally useful.

Though, as Derrick notes below, we would break systems which
implement crontab strictly per the POSIX spec.  I don't know
how many crontab's don't accept `-`.

At the time, I checked on an older OmniOS system I had
access to (based on Illumos/OpenSolaris) and it did not
accept `-`.  So my quick sample size of 3 (Fedora, CentOS,
and OmniOS) I had a 1/3 failure rate.

> Thank you for reporting this, especially with a patch!
> 
> However, I'm not sure about this adding of '-' being something that
> crontab ignores so commonly. My Ubuntu machine reports this:
> 
> $ crontab -e -
> crontab: usage error: no arguments permitted after this option
> usage:  crontab [-u user] file
>         crontab [ -u user ] [ -i ] { -e | -l | -r }
>                 (default operation is replace, per 1003.2)
>         -e      (edit user's crontab)
>         -l      (list user's crontab)
>         -r      (delete user's crontab)
>         -i      (prompt before deleting user's crontab)
> 
> Is there a way we could attempt writing over stdin, notice the
> failure, then retry with the '-' option?

You'd skip the `-e` there, no?  Running `crontab -` in a
current ubuntu container with the cron package installed
(what looks like vixie-cron-3.0pl1) works as expected.

Perhaps a Makefile knob to allow systems with such a crontab
to adjust the behavior would be an alternative to detecting
and retrying?

NEEDS_CRONTAB_STDIN_OPT or something like that, with
config.mak.uname to override whichever default is chosen.
Whether that's a better option really depends on how much
effort it is to add and maintain the detection in the code
weighed against how many systems would need to have the
default changed.

Mildy related, I wonder whether we'll eventually see a patch
to use systemd timers instead of cron (optionally, of
course).  Fedora, for example, doesn't install crond by
default anymore.  (Though, warts and all, I still prefer
crond myself.)

>> [...]
>> 
>>> --- a/t/helper/test-crontab.c
>>> +++ b/t/helper/test-crontab.c
>>> @@ -17,7 +17,7 @@ int cmd__crontab(int argc, const char **argv)
>>>                 if (!from)
>>>                         return 0;
>>>                 to = stdout;
>>> -       } else if (argc == 2) {
>>> +       } else if ((argc == 3 && !strcmp(argv[2], "-")) || argc == 2) {
>>>                 from = stdin;
>>>                 to = fopen(argv[1], "w");
>> 
>> Would it make sense to make this
>> 
>>   } else if (argc == 3 && !strcmp(argv[2], "-")) {
>> 
>> in order to make this test-tool as picky as possible and to only accept
>> the kind of usage we want to (well, need to) use? The tests as they
>> stand would still pass, which I think argues for us not really needing
>> that "argc == 2".
>> 
>> This would be followed by
>> 
>>   } else
>>           return error("unknown arguments");
>> 
>> which wouldn't be super helpful if you forgot the "-", but helpful
>> enough for an internal test-tool, I guess.
>>
>> Speaking of usage and hints, there's "Usage: ..." in a comment at the
>> top of this file. It should probably be updated either way.
> 
> I agree with Martin's review here, too.

-- 
Todd

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

* Re: [PATCH] maintenance: specify explicit stdin for crontab
  2021-03-30 17:12     ` Kevin Daudt
@ 2021-03-30 19:32       ` Derrick Stolee
  0 siblings, 0 replies; 7+ messages in thread
From: Derrick Stolee @ 2021-03-30 19:32 UTC (permalink / raw)
  To: Kevin Daudt, Martin Ågren, Git Mailing List

On 3/30/2021 1:12 PM, Kevin Daudt wrote:
> On Tue, Mar 30, 2021 at 08:02:22AM -0400, Derrick Stolee wrote:
>> On 3/30/2021 1:41 AM, Martin Ågren wrote:
>>> On Mon, 29 Mar 2021 at 23:23, Kevin Daudt <me@ikke.info> wrote:
>>>>
>>>> There are multiple crontab implementations that require stdin for
>>>> editing a crontab to be explicitly specified as '-'.
>>
>> Thank you for reporting this, especially with a patch!
>>
>> However, I'm not sure about this adding of '-' being something that
>> crontab ignores so commonly. My Ubuntu machine reports this:
>>
>> $ crontab -e -
>> crontab: usage error: no arguments permitted after this option
>> usage:  crontab [-u user] file
>>         crontab [ -u user ] [ -i ] { -e | -l | -r }
>>                 (default operation is replace, per 1003.2)
>>         -e      (edit user's crontab)
>>         -l      (list user's crontab)
>>         -r      (delete user's crontab)
>>         -i      (prompt before deleting user's crontab)
>>
>> Is there a way we could attempt writing over stdin, notice the
>> failure, then retry with the '-' option?
> 
> We do not use -e to edit, we run `crontab` and provide the contents to
> stdin. `crontab -e` just opens the crontab in the users editor, which
> would work with busybox as well, but that's not what's being done here.

Thank you. Of course. Muscle memory from testing crontab manually.

Thanks,
-Stolee


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

* Re: [PATCH] maintenance: specify explicit stdin for crontab
  2021-03-30 17:43     ` Todd Zullinger
@ 2021-03-30 19:38       ` Derrick Stolee
  0 siblings, 0 replies; 7+ messages in thread
From: Derrick Stolee @ 2021-03-30 19:38 UTC (permalink / raw)
  To: Todd Zullinger; +Cc: Martin Ågren, Kevin Daudt, Git Mailing List

On 3/30/2021 1:43 PM, Todd Zullinger wrote:
> Hi,
> 
> Derrick Stolee wrote:
>> On 3/30/2021 1:41 AM, Martin Ågren wrote:
>>> On Mon, 29 Mar 2021 at 23:23, Kevin Daudt <me@ikke.info> wrote:
>>>>
>>>> There are multiple crontab implementations that require stdin for
>>>> editing a crontab to be explicitly specified as '-'.
> 
> Amusingly, I wrote the exact same patch 2 weeks ago
> (including not dropping the `argc == 2` which Martin
> mentioned).  That was in response to a report in the Fedora
> bugzilla:
> 
>     https://bugzilla.redhat.com/1939930
> 
> I thought cronie might be rather rare with it's non-POSIX
> handling of crontab without arguments.

Thanks for this link! I appreciate the context.
 
> In the end, the cronie folks upstream adjusted things so
> that crontab behaves as defined by POSIX if stdin is not a
> TTY:
> 
>     https://github.com/cronie-crond/cronie/commit/8b0241f
>> That allows cronie to behave more sensibly for interactive
> use without breaking tools like git maintenance.  And it let
> me sidestep proposing a patch to git (or worse, maintaining
> it in the Fedora packages).

Nice!
 
> But I didn't dig in to find out whether or how many other
> crontab implemntations had also eschewed the (rather poor)
> POSIX-confirming behavior.  Knowing there are several among
> popular OS's makes it easy to see something like this patch
> being generally useful.
> 
> Though, as Derrick notes below, we would break systems which
> implement crontab strictly per the POSIX spec.  I don't know
> how many crontab's don't accept `-`.
> 
> At the time, I checked on an older OmniOS system I had
> access to (based on Illumos/OpenSolaris) and it did not
> accept `-`.  So my quick sample size of 3 (Fedora, CentOS,
> and OmniOS) I had a 1/3 failure rate.
> 
>> Thank you for reporting this, especially with a patch!
>>
>> However, I'm not sure about this adding of '-' being something that
>> crontab ignores so commonly. My Ubuntu machine reports this:
>>
>> $ crontab -e -
>> crontab: usage error: no arguments permitted after this option
>> usage:  crontab [-u user] file
>>         crontab [ -u user ] [ -i ] { -e | -l | -r }
>>                 (default operation is replace, per 1003.2)
>>         -e      (edit user's crontab)
>>         -l      (list user's crontab)
>>         -r      (delete user's crontab)
>>         -i      (prompt before deleting user's crontab)
>>
>> Is there a way we could attempt writing over stdin, notice the
>> failure, then retry with the '-' option?
> 
> You'd skip the `-e` there, no?  Running `crontab -` in a
> current ubuntu container with the cron package installed
> (what looks like vixie-cron-3.0pl1) works as expected.

Yes, this is my mistake. My machine supports "crontab -".

> Perhaps a Makefile knob to allow systems with such a crontab
> to adjust the behavior would be an alternative to detecting
> and retrying?
> 
> NEEDS_CRONTAB_STDIN_OPT or something like that, with
> config.mak.uname to override whichever default is chosen.
> Whether that's a better option really depends on how much
> effort it is to add and maintain the detection in the code
> weighed against how many systems would need to have the
> default changed.
> 
> Mildy related, I wonder whether we'll eventually see a patch
> to use systemd timers instead of cron (optionally, of
> course).  Fedora, for example, doesn't install crond by
> default anymore.  (Though, warts and all, I still prefer
> crond myself.)

Perhaps the best way to approach this is to try adding '-'
by default, and remove it and try again on a failure. If the
usage is actually a problem, that first command should fail
quickly. I'm not sure if we could rely upon a specific
category of error codes or if we should just say "non-zero
exit means retry".

Thanks!
-Stolee

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

end of thread, other threads:[~2021-03-30 19:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-29 21:09 [PATCH] maintenance: specify explicit stdin for crontab Kevin Daudt
2021-03-30  5:41 ` Martin Ågren
2021-03-30 12:02   ` Derrick Stolee
2021-03-30 17:12     ` Kevin Daudt
2021-03-30 19:32       ` Derrick Stolee
2021-03-30 17:43     ` Todd Zullinger
2021-03-30 19:38       ` Derrick Stolee

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).