2001-12-13 Marcus Brinkmann <marcus@g10code.de>

* 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.
This commit is contained in:
Marcus Brinkmann 2001-12-13 22:02:08 +00:00
parent 831692ebf7
commit 4a55249ee6
3 changed files with 84 additions and 34 deletions

View File

@ -1,3 +1,19 @@
2001-12-13 Marcus Brinkmann <marcus@g10code.de>
* 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 <wk@gnupg.org> 2001-12-13 Werner Koch <wk@gnupg.org>
* engine-gpgsm.c (_gpgme_gpgsm_set_colon_line_handler): New. * engine-gpgsm.c (_gpgme_gpgsm_set_colon_line_handler): New.

View File

@ -107,59 +107,91 @@ _gpgme_gpgsm_check_version (void)
? 0 : mk_error (Invalid_Engine); ? 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 GpgmeError
_gpgme_gpgsm_new (GpgsmObject *r_gpgsm) _gpgme_gpgsm_new (GpgsmObject *r_gpgsm)
{ {
GpgmeError err = 0; GpgmeError err = 0;
GpgsmObject gpgsm; GpgsmObject gpgsm;
char *argv[] = { "gpgsm", "--server", NULL }; char *argv[] = { "gpgsm", "--server", NULL };
int ip[2] = { -1, -1 }; int fds[2];
int op[2] = { -1, -1 };
int mp[2] = { -1, -1 };
*r_gpgsm = NULL; *r_gpgsm = NULL;
gpgsm = xtrycalloc (1, sizeof *gpgsm); gpgsm = xtrycalloc (1, sizeof *gpgsm);
if (!gpgsm) if (!gpgsm)
{ {
err = mk_error (Out_Of_Core); 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); err = mk_error (General_Error);
goto leave; goto leave;
} }
gpgsm->input_fd = ip[1]; gpgsm->input_fd = fds[1];
fcntl (ip[1], F_SETFD, FD_CLOEXEC); /* FIXME */ gpgsm->input_fd_server = fds[0];
gpgsm->input_fd_server = ip[0];
if (_gpgme_io_pipe (op, 1) < 0) if (_gpgme_io_pipe (fds, 1) < 0)
{ {
err = mk_error (General_Error); err = mk_error (General_Error);
goto leave; goto leave;
} }
gpgsm->output_fd = op[0]; gpgsm->output_fd = fds[0];
fcntl (op[0], F_SETFD, FD_CLOEXEC); /* FIXME */ gpgsm->output_fd_server = fds[1];
gpgsm->output_fd_server = op[1];
if (_gpgme_io_pipe (mp, 0) < 0) if (_gpgme_io_pipe (fds, 0) < 0)
{ {
err = mk_error (General_Error); err = mk_error (General_Error);
goto leave; goto leave;
} }
gpgsm->message_fd = mp[1]; gpgsm->message_fd = fds[1];
fcntl (mp[1], F_SETFD, FD_CLOEXEC); /* FIXME */ gpgsm->message_fd_server = fds[0];
gpgsm->message_fd_server = mp[0];
err = assuan_pipe_connect (&gpgsm->assuan_ctx, err = assuan_pipe_connect (&gpgsm->assuan_ctx,
_gpgme_get_gpgsm_path (), argv); _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: leave:
if (ip[0] != -1) /* Close the server ends of the pipes. Our ends are closed in
_gpgme_io_close (ip[0]); _gpgme_gpgsm_release. */
if (op[1] != -1) if (gpgsm->input_fd_server != -1)
_gpgme_io_close (op[1]); _gpgme_io_close (gpgsm->input_fd_server);
if (mp[0] != -1) if (gpgsm->output_fd_server != -1)
_gpgme_io_close (mp[0]); _gpgme_io_close (gpgsm->output_fd_server);
if (gpgsm->message_fd_server != -1)
_gpgme_io_close (gpgsm->message_fd_server);
if (err) if (err)
_gpgme_gpgsm_release (gpgsm); _gpgme_gpgsm_release (gpgsm);
@ -256,7 +288,6 @@ _gpgme_gpgsm_op_decrypt (GpgsmObject gpgsm, GpgmeData ciph, GpgmeData plain)
if (err) if (err)
return mk_error (General_Error); /* FIXME */ return mk_error (General_Error); /* FIXME */
_gpgme_io_close (gpgsm->message_fd); _gpgme_io_close (gpgsm->message_fd);
gpgsm->message_fd = -1;
return 0; return 0;
} }
@ -330,7 +361,6 @@ _gpgme_gpgsm_op_encrypt (GpgsmObject gpgsm, GpgmeRecipients recp,
if (err) if (err)
return mk_error (General_Error); /* FIXME */ return mk_error (General_Error); /* FIXME */
_gpgme_io_close (gpgsm->message_fd); _gpgme_io_close (gpgsm->message_fd);
gpgsm->message_fd = -1;
err = gpgsm_set_recipients (gpgsm->assuan_ctx, recp); err = gpgsm_set_recipients (gpgsm->assuan_ctx, recp);
if (err) if (err)
@ -371,9 +401,7 @@ _gpgme_gpgsm_op_import (GpgsmObject gpgsm, GpgmeData keydata)
if (err) if (err)
return mk_error (General_Error); /* FIXME */ return mk_error (General_Error); /* FIXME */
_gpgme_io_close (gpgsm->output_fd); _gpgme_io_close (gpgsm->output_fd);
gpgsm->output_fd = -1;
_gpgme_io_close (gpgsm->message_fd); _gpgme_io_close (gpgsm->message_fd);
gpgsm->message_fd = -1;
return 0; return 0;
} }
@ -394,11 +422,8 @@ _gpgme_gpgsm_op_keylist (GpgsmObject gpgsm, const char *pattern,
strcpy (&line[9], pattern); strcpy (&line[9], pattern);
_gpgme_io_close (gpgsm->input_fd); _gpgme_io_close (gpgsm->input_fd);
gpgsm->input_fd = -1;
_gpgme_io_close (gpgsm->output_fd); _gpgme_io_close (gpgsm->output_fd);
gpgsm->output_fd = -1;
_gpgme_io_close (gpgsm->message_fd); _gpgme_io_close (gpgsm->message_fd);
gpgsm->message_fd = -1;
gpgsm->command = line; gpgsm->command = line;
return 0; return 0;
@ -429,7 +454,6 @@ _gpgme_gpgsm_op_sign (GpgsmObject gpgsm, GpgmeData in, GpgmeData out,
if (err) if (err)
return mk_error (General_Error); /* FIXME */ return mk_error (General_Error); /* FIXME */
_gpgme_io_close (gpgsm->message_fd); _gpgme_io_close (gpgsm->message_fd);
gpgsm->message_fd = -1;
return 0; return 0;
} }
@ -463,7 +487,6 @@ _gpgme_gpgsm_op_verify (GpgsmObject gpgsm, GpgmeData sig, GpgmeData text)
if (err) if (err)
return mk_error (General_Error); /* FIXME */ return mk_error (General_Error); /* FIXME */
_gpgme_io_close (gpgsm->output_fd); _gpgme_io_close (gpgsm->output_fd);
gpgsm->output_fd = -1;
return 0; return 0;
} }

View File

@ -75,10 +75,21 @@ _gpgme_io_write ( int fd, const void *buffer, size_t count )
} }
int 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 */ int err;
return pipe ( filedes );
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 int