1
0
Fork 0
mirror of git://git.gnupg.org/gnupg.git synced 2025-07-03 22:56:33 +02:00

gpgsm: Terminate key listing on output write error.

* sm/keylist.c (list_internal_keys): Detect write errors to the output
stream.

* sm/server.c (any_failure_printed): New var.
(gpgsm_status2): Handle new var.  Move statusfp init to ...
(gpgsm_init_statusfp): new function.
(gpgsm_exit_failure_status): New.
* sm/gpgsm.c (main): Explicit statusfp init.
(gpgsm_exit): Print failure status on error.
--
Test by using

  gpgsm -k >/dev/full
  gpgsm -k --wit-colons >/dev/full

and also by redirecting to a file on a small partition.

GnuPG-bug-id: 6185
This commit is contained in:
Werner Koch 2024-10-23 10:40:36 +02:00
parent 40707c8bff
commit 18081e2ecf
No known key found for this signature in database
GPG key ID: E3FDFF218E45B72B
4 changed files with 74 additions and 15 deletions

View file

@ -37,6 +37,11 @@
#define set_error(e,t) assuan_set_error (ctx, gpg_error (e), (t))
/* Used to track whether we printed any FAILURE status in non-server
* mode. */
static int any_failure_printed;
/* The filepointer for status message used in non-server mode */
static FILE *statusfp;
@ -1515,6 +1520,24 @@ gpgsm_server (certlist_t default_recplist)
}
void
gpgsm_init_statusfp (ctrl_t ctrl)
{
if (statusfp || ctrl->status_fd == -1)
return;
if (ctrl->status_fd == 1)
statusfp = stdout;
else if (ctrl->status_fd == 2)
statusfp = stderr;
else
statusfp = fdopen (ctrl->status_fd, "w");
if (!statusfp)
log_fatal ("can't open fd %d for status output: %s\n",
ctrl->status_fd, strerror(errno));
}
gpg_error_t
gpgsm_status2 (ctrl_t ctrl, int no, ...)
@ -1527,23 +1550,11 @@ gpgsm_status2 (ctrl_t ctrl, int no, ...)
if (ctrl->no_server && ctrl->status_fd == -1)
; /* No status wanted. */
else if (ctrl->no_server && no == STATUS_FAILURE && any_failure_printed)
; /* Don't emit FAILURE a second time. */
else if (ctrl->no_server)
{
if (!statusfp)
{
if (ctrl->status_fd == 1)
statusfp = stdout;
else if (ctrl->status_fd == 2)
statusfp = stderr;
else
statusfp = fdopen (ctrl->status_fd, "w");
if (!statusfp)
{
log_fatal ("can't open fd %d for status output: %s\n",
ctrl->status_fd, strerror(errno));
}
}
gpgsm_init_statusfp (ctrl);
fputs ("[GNUPG:] ", statusfp);
fputs (get_status_string (no), statusfp);
@ -1562,6 +1573,10 @@ gpgsm_status2 (ctrl_t ctrl, int no, ...)
}
}
putc ('\n', statusfp);
if (no == STATUS_FAILURE)
any_failure_printed = 1;
if (ferror (statusfp))
err = gpg_error_from_syserror ();
else
@ -1614,6 +1629,23 @@ gpgsm_status_with_error (ctrl_t ctrl, int no, const char *text,
}
/* Function to print a FAILURE status line on exit. */
void
gpgsm_exit_failure_status (void)
{
/* stderr is used as a last but possible wrong resort if no status
* has yet been printed. */
FILE *fp = statusfp? statusfp : stderr;
if (any_failure_printed)
return;
fputs ("[GNUPG:] ", fp);
fputs (get_status_string (STATUS_FAILURE), fp);
fputs (" gpgsm-exit 50331649\n", fp);
}
/* This callback is used to emit progress status lines. */
gpg_error_t
gpgsm_progress_cb (ctrl_t ctrl, uint64_t current, uint64_t total)