From 4a55249ee60f9f94da1e4b92824ef7427154745c Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Thu, 13 Dec 2001 22:02:08 +0000 Subject: [PATCH] 2001-12-13 Marcus Brinkmann * engine-gpgsm.c (close_notify_handler): New function. (_gpgme_gpgsm_new): Manage the file descriptors a bit differently. Do not set close-on-exec flags. (_gpgme_gpgsm_op_decrypt): Do not set message_fd to -1, this is done by the close handler. (_gpgme_gpgsm_op_encrypt): Likewise. (_gpgme_gpgsm_op_import): Likewise (also for output_fd). (_gpgme_gpgsm_op_keylist): Likewise (also for input_fd and output_fd). (_gpgme_gpgsm_op_sign): Likewise. (_gpgme_gpgsm_op_verify): Likewise, but for output_fd. * posix-io.c (_gpgme_io_pipe): Set the close-on-exec flag for the non-inherited file descriptor index of the pipe. --- gpgme/ChangeLog | 16 +++++++++ gpgme/engine-gpgsm.c | 85 ++++++++++++++++++++++++++++---------------- gpgme/posix-io.c | 17 +++++++-- 3 files changed, 84 insertions(+), 34 deletions(-) diff --git a/gpgme/ChangeLog b/gpgme/ChangeLog index bf9796d..c314452 100644 --- a/gpgme/ChangeLog +++ b/gpgme/ChangeLog @@ -1,3 +1,19 @@ +2001-12-13 Marcus Brinkmann + + * engine-gpgsm.c (close_notify_handler): New function. + (_gpgme_gpgsm_new): Manage the file descriptors a + bit differently. Do not set close-on-exec flags. + (_gpgme_gpgsm_op_decrypt): Do not set message_fd + to -1, this is done by the close handler. + (_gpgme_gpgsm_op_encrypt): Likewise. + (_gpgme_gpgsm_op_import): Likewise (also for output_fd). + (_gpgme_gpgsm_op_keylist): Likewise (also for input_fd and output_fd). + (_gpgme_gpgsm_op_sign): Likewise. + (_gpgme_gpgsm_op_verify): Likewise, but for output_fd. + + * posix-io.c (_gpgme_io_pipe): Set the close-on-exec flag for the + non-inherited file descriptor index of the pipe. + 2001-12-13 Werner Koch * engine-gpgsm.c (_gpgme_gpgsm_set_colon_line_handler): New. diff --git a/gpgme/engine-gpgsm.c b/gpgme/engine-gpgsm.c index 38ce87d..776ef0b 100644 --- a/gpgme/engine-gpgsm.c +++ b/gpgme/engine-gpgsm.c @@ -107,59 +107,91 @@ _gpgme_gpgsm_check_version (void) ? 0 : mk_error (Invalid_Engine); } +static void +close_notify_handler (int fd, void *opaque) +{ + GpgsmObject gpgsm = opaque; + + assert (fd != -1); + if (gpgsm->input_fd == fd) + gpgsm->input_fd = -1; + else if (gpgsm->output_fd == fd) + gpgsm->output_fd = -1; + else if (gpgsm->message_fd == fd) + gpgsm->message_fd = -1; +} + GpgmeError _gpgme_gpgsm_new (GpgsmObject *r_gpgsm) { GpgmeError err = 0; GpgsmObject gpgsm; char *argv[] = { "gpgsm", "--server", NULL }; - int ip[2] = { -1, -1 }; - int op[2] = { -1, -1 }; - int mp[2] = { -1, -1 }; + int fds[2]; *r_gpgsm = NULL; gpgsm = xtrycalloc (1, sizeof *gpgsm); if (!gpgsm) { err = mk_error (Out_Of_Core); - goto leave; + return err; } - if (_gpgme_io_pipe (ip, 0) < 0) + gpgsm->input_fd = -1; + gpgsm->input_fd_server = -1; + gpgsm->output_fd = -1; + gpgsm->output_fd_server = -1; + gpgsm->message_fd = -1; + gpgsm->message_fd_server = -1; + + if (_gpgme_io_pipe (fds, 0) < 0) { err = mk_error (General_Error); goto leave; } - gpgsm->input_fd = ip[1]; - fcntl (ip[1], F_SETFD, FD_CLOEXEC); /* FIXME */ - gpgsm->input_fd_server = ip[0]; - if (_gpgme_io_pipe (op, 1) < 0) + gpgsm->input_fd = fds[1]; + gpgsm->input_fd_server = fds[0]; + + if (_gpgme_io_pipe (fds, 1) < 0) { err = mk_error (General_Error); goto leave; } - gpgsm->output_fd = op[0]; - fcntl (op[0], F_SETFD, FD_CLOEXEC); /* FIXME */ - gpgsm->output_fd_server = op[1]; - if (_gpgme_io_pipe (mp, 0) < 0) + gpgsm->output_fd = fds[0]; + gpgsm->output_fd_server = fds[1]; + + if (_gpgme_io_pipe (fds, 0) < 0) { err = mk_error (General_Error); goto leave; } - gpgsm->message_fd = mp[1]; - fcntl (mp[1], F_SETFD, FD_CLOEXEC); /* FIXME */ - gpgsm->message_fd_server = mp[0]; + gpgsm->message_fd = fds[1]; + gpgsm->message_fd_server = fds[0]; err = assuan_pipe_connect (&gpgsm->assuan_ctx, _gpgme_get_gpgsm_path (), argv); + if (!err && + (_gpgme_io_set_close_notify (gpgsm->input_fd, + close_notify_handler, gpgsm) + || _gpgme_io_set_close_notify (gpgsm->output_fd, + close_notify_handler, gpgsm) + || _gpgme_io_set_close_notify (gpgsm->message_fd, + close_notify_handler, gpgsm))) + { + err = mk_error (General_Error); + goto leave; + } + leave: - if (ip[0] != -1) - _gpgme_io_close (ip[0]); - if (op[1] != -1) - _gpgme_io_close (op[1]); - if (mp[0] != -1) - _gpgme_io_close (mp[0]); + /* Close the server ends of the pipes. Our ends are closed in + _gpgme_gpgsm_release. */ + if (gpgsm->input_fd_server != -1) + _gpgme_io_close (gpgsm->input_fd_server); + if (gpgsm->output_fd_server != -1) + _gpgme_io_close (gpgsm->output_fd_server); + if (gpgsm->message_fd_server != -1) + _gpgme_io_close (gpgsm->message_fd_server); if (err) _gpgme_gpgsm_release (gpgsm); @@ -256,7 +288,6 @@ _gpgme_gpgsm_op_decrypt (GpgsmObject gpgsm, GpgmeData ciph, GpgmeData plain) if (err) return mk_error (General_Error); /* FIXME */ _gpgme_io_close (gpgsm->message_fd); - gpgsm->message_fd = -1; return 0; } @@ -330,7 +361,6 @@ _gpgme_gpgsm_op_encrypt (GpgsmObject gpgsm, GpgmeRecipients recp, if (err) return mk_error (General_Error); /* FIXME */ _gpgme_io_close (gpgsm->message_fd); - gpgsm->message_fd = -1; err = gpgsm_set_recipients (gpgsm->assuan_ctx, recp); if (err) @@ -371,9 +401,7 @@ _gpgme_gpgsm_op_import (GpgsmObject gpgsm, GpgmeData keydata) if (err) return mk_error (General_Error); /* FIXME */ _gpgme_io_close (gpgsm->output_fd); - gpgsm->output_fd = -1; _gpgme_io_close (gpgsm->message_fd); - gpgsm->message_fd = -1; return 0; } @@ -394,11 +422,8 @@ _gpgme_gpgsm_op_keylist (GpgsmObject gpgsm, const char *pattern, strcpy (&line[9], pattern); _gpgme_io_close (gpgsm->input_fd); - gpgsm->input_fd = -1; _gpgme_io_close (gpgsm->output_fd); - gpgsm->output_fd = -1; _gpgme_io_close (gpgsm->message_fd); - gpgsm->message_fd = -1; gpgsm->command = line; return 0; @@ -429,7 +454,6 @@ _gpgme_gpgsm_op_sign (GpgsmObject gpgsm, GpgmeData in, GpgmeData out, if (err) return mk_error (General_Error); /* FIXME */ _gpgme_io_close (gpgsm->message_fd); - gpgsm->message_fd = -1; return 0; } @@ -463,7 +487,6 @@ _gpgme_gpgsm_op_verify (GpgsmObject gpgsm, GpgmeData sig, GpgmeData text) if (err) return mk_error (General_Error); /* FIXME */ _gpgme_io_close (gpgsm->output_fd); - gpgsm->output_fd = -1; return 0; } diff --git a/gpgme/posix-io.c b/gpgme/posix-io.c index 2043854..eaa8950 100644 --- a/gpgme/posix-io.c +++ b/gpgme/posix-io.c @@ -75,10 +75,21 @@ _gpgme_io_write ( int fd, const void *buffer, size_t count ) } int -_gpgme_io_pipe ( int filedes[2], int inherit_idx ) +_gpgme_io_pipe (int filedes[2], int inherit_idx) { - /* we don't need inherit_idx in this implementation */ - return pipe ( filedes ); + int err; + + err = pipe (filedes); + if (err < 0) + return err; + /* FIXME: Should get the old flags first. */ + err = fcntl (filedes[1 - inherit_idx], F_SETFD, FD_CLOEXEC); + if (err < 0) + { + close (filedes[0]); + close (filedes[1]); + } + return err; } int -- 2.26.2