hostnameInCron.patch

Xabier Oneca -- xOneca xoneca at gmail.com
Tue May 25 20:46:59 UTC 2021


Hi Matthew,

> I've been using this one for a while and it's stable.  It puts the
> hostname in emails sent from cron, otherwise it's hard to figure out
> which host the email came from.
>
>
> diff -ruN busybox-1.27.2.orig/miscutils/crond.c
> busybox-1.27.2/miscutils/crond.c
> --- busybox-1.27.2.orig/miscutils/crond.c    2017-07-18
> 09:14:50.000000000 -0800
> +++ busybox-1.27.2/miscutils/crond.c    2017-12-28 11:25:45.412623185 -0900
> @@ -667,6 +667,7 @@
>   static void start_one_job(const char *user, CronLine *line)
>   {
>       char mailFile[128];
> +    char *hostname;
>       int mailFd = -1;
>
>       line->cl_pid = 0;
> @@ -676,10 +677,11 @@
>           /* Open mail file (owner is root so nobody can screw with it) */
>           snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d",
> CRON_DIR, user, getpid());
>           mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL
> | O_APPEND, 0600);
> +        hostname = safe_gethostname();
>
>           if (mailFd >= 0) {
> -            fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n",
> line->cl_mailto,
> -                line->cl_cmd);
> +            fdprintf(mailFd, "To: %s\nSubject: %s cron: %s\n\n",
> line->cl_mailto,
> +                hostname, line->cl_cmd);
>               line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR);
>           } else {
>               bb_error_msg("can't create mail file %s for user %s, "

Shouldn't hostname be freed after use, as safe_gethostname() returns a
malloced string?

Cheers,

Xabier Oneca_,,_


More information about the busybox mailing list