[gmime-devel] [PATCH 2/5] better documentation of how GMime uses the file descriptors for gpg



---
 gmime/gmime-gpg-context.c | 52 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 33 insertions(+), 19 deletions(-)

diff --git a/gmime/gmime-gpg-context.c b/gmime/gmime-gpg-context.c
index 206126e..da92ae3 100644
--- a/gmime/gmime-gpg-context.c
+++ b/gmime/gmime-gpg-context.c
@@ -295,7 +295,9 @@ struct _GpgCtx {
        int stdout_fd;
        int stderr_fd;
        int status_fd;
-       int secret_fd;  /* used for sign/decrypt/verify */
+       int multipurpose_fd;  /* used for exactly one of:
+                                (a) password for sign or decrypt or
+                                (b) out-of-band signature */
        
        /* status-fd buffer */
        char *statusbuf;
@@ -366,7 +368,7 @@ gpg_ctx_new (GMimeGpgContext *ctx)
        gpg->stdout_fd = -1;
        gpg->stderr_fd = -1;
        gpg->status_fd = -1;
-       gpg->secret_fd = -1;
+       gpg->multipurpose_fd = -1;
        
        gpg->statusbuf = g_malloc (128);
        gpg->statusptr = gpg->statusbuf;
@@ -537,8 +539,8 @@ gpg_ctx_free (struct _GpgCtx *gpg)
                close (gpg->stderr_fd);
        if (gpg->status_fd != -1)
                close (gpg->status_fd);
-       if (gpg->secret_fd != -1)
-               close (gpg->secret_fd);
+       if (gpg->multipurpose_fd != -1)
+               close (gpg->multipurpose_fd);
        
        g_free (gpg->statusbuf);
        
@@ -599,7 +601,7 @@ gpg_digest_str (GMimeDigestAlgo digest)
 }
 
 static char **
-gpg_ctx_get_argv (struct _GpgCtx *gpg, int status_fd, int secret_fd, char ***strv)
+gpg_ctx_get_argv (struct _GpgCtx *gpg, int status_fd, int multipurpose_fd, char ***strv)
 {
        const char *digest_str;
        char **argv, *buf;
@@ -623,7 +625,7 @@ gpg_ctx_get_argv (struct _GpgCtx *gpg, int status_fd, int secret_fd, char ***str
        g_ptr_array_add (args, buf);
        
        if (gpg->need_passwd) {
-               (*strv)[v++] = buf = g_strdup_printf ("--command-fd=%d", secret_fd);
+               (*strv)[v++] = buf = g_strdup_printf ("--command-fd=%d", multipurpose_fd);
                g_ptr_array_add (args, buf);
        }
        
@@ -656,7 +658,7 @@ gpg_ctx_get_argv (struct _GpgCtx *gpg, int status_fd, int secret_fd, char ***str
                g_ptr_array_add (args, "--");
                
                /* signature stream must come first */
-               (*strv)[v++] = buf = g_strdup_printf ("-&%d", secret_fd);
+               (*strv)[v++] = buf = g_strdup_printf ("-&%d", multipurpose_fd);
                g_ptr_array_add (args, buf);
                
                /* followed by the content stream (in this case, stdin) */
@@ -740,11 +742,23 @@ gpg_ctx_op_start (struct _GpgCtx *gpg)
        int i, maxfd, errnosave, fds[10];
        char **argv, **strv = NULL;
        int flags;
+
+       /* fds is a series of pairs of pipe(2) file descriptors.
+          from gpg's perspective, they are (in pairs):
+          * stdin
+          * stdout
+          * stderr
+          * status-fd
+          * multipurpose: use for at most one of: passphrase or signature stream
+          */
        
-       for (i = 0; i < 10; i++)
+       for (i = 0; i < sizeof (fds)/sizeof (*fds); i++)
                fds[i] = -1;
-       
-       maxfd = (gpg->need_passwd || gpg->sigstream) ? 10 : 8;
+
+       maxfd = sizeof (fds)/sizeof (*fds);
+       /* don't create the multipurpose stream if we don't need it */
+       if (!(gpg->need_passwd || gpg->sigstream))
+               maxfd -= 2;
        for (i = 0; i < maxfd; i += 2) {
                if (pipe (fds + i) == -1)
                        goto exception;
@@ -799,7 +813,7 @@ gpg_ctx_op_start (struct _GpgCtx *gpg)
        if (fds[8] != -1) {
                flags = (flags = fcntl (fds[9], F_GETFL)) == -1 ? O_WRONLY : flags;
                fcntl (fds[9], F_SETFL, flags | O_NONBLOCK);
-               gpg->secret_fd = fds[9];
+               gpg->multipurpose_fd = fds[9];
                close (fds[8]);
        }
        
@@ -1187,7 +1201,7 @@ gpg_ctx_parse_status (struct _GpgCtx *gpg, GError **err)
                }
                
                /* create a stream for the application to write the passwd to */
-               passwd = g_mime_stream_pipe_new (gpg->secret_fd);
+               passwd = g_mime_stream_pipe_new (gpg->multipurpose_fd);
                g_mime_stream_pipe_set_owner ((GMimeStreamPipe *) passwd, FALSE);
                
                if (!gpg->utf8) {
@@ -1434,7 +1448,7 @@ enum {
        GPG_STDOUT_FD,
        GPG_STDERR_FD,
        GPG_STATUS_FD,
-       GPG_VERIFY_FD,
+       GPG_MULTIPURPOSE_FD,
        GPG_N_FDS
 };
 
@@ -1553,8 +1567,8 @@ gpg_ctx_op_step (struct _GpgCtx *gpg, GError **err)
        pfds[GPG_STDIN_FD].events = POLLOUT;
        
        if (gpg->mode == GPG_CTX_MODE_VERIFY) {
-               pfds[GPG_VERIFY_FD].fd = gpg->secret_fd;
-               pfds[GPG_VERIFY_FD].events = POLLOUT;
+               pfds[GPG_MULTIPURPOSE_FD].fd = gpg->multipurpose_fd;
+               pfds[GPG_MULTIPURPOSE_FD].events = POLLOUT;
        }
        
        do {
@@ -1641,7 +1655,7 @@ gpg_ctx_op_step (struct _GpgCtx *gpg, GError **err)
                }
        }
        
-       if ((pfds[GPG_VERIFY_FD].revents & (POLLOUT | POLLHUP))) {
+       if ((pfds[GPG_MULTIPURPOSE_FD].revents & (POLLOUT | POLLHUP))) {
                char buffer[4096];
                ssize_t nread;
                
@@ -1654,7 +1668,7 @@ gpg_ctx_op_step (struct _GpgCtx *gpg, GError **err)
                        
                        do {
                                do {
-                                       w = write (gpg->secret_fd, buffer + nwritten, nread - nwritten);
+                                       w = write (gpg->multipurpose_fd, buffer + nwritten, nread - nwritten);
                                } while (w == -1 && (errno == EINTR || errno == EAGAIN));
                                
                                if (w > 0)
@@ -1666,8 +1680,8 @@ gpg_ctx_op_step (struct _GpgCtx *gpg, GError **err)
                }
                
                if (g_mime_stream_eos (gpg->sigstream)) {
-                       close (gpg->secret_fd);
-                       gpg->secret_fd = -1;
+                       close (gpg->multipurpose_fd);
+                       gpg->multipurpose_fd = -1;
                }
        }
        
-- 
2.10.2



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]