From: W. Trevor King Date: Thu, 11 Oct 2012 20:12:33 +0000 (-0400) Subject: gpgme-tool: use Assuan wrappers for portable socket handling. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5fbecd54293128e41d08e07c3b0779c3e9c44be9;p=gpgme.git gpgme-tool: use Assuan wrappers for portable socket handling. Signed-off-by: W. Trevor King --- diff --git a/src/gpgme-tool.c b/src/gpgme-tool.c index d5ce1f7..ec22a91 100644 --- a/src/gpgme-tool.c +++ b/src/gpgme-tool.c @@ -50,6 +50,16 @@ #include +/* from libassuan/src/assuan-defs.h */ +#if HAVE_W32_SYSTEM +#define SOCKET2HANDLE(s) ((void *)(s)) +#define HANDLE2SOCKET(h) ((unsigned int)(h)) +#else +#define SOCKET2HANDLE(s) (s) +#define HANDLE2SOCKET(h) (h) +#endif +/* end from libassuan/src/assuan-defs.h */ + #include "gpgme.h" /* GCC attributes. */ @@ -633,16 +643,16 @@ peek_membuf (membuf_t *mb, size_t *len) FILE *log_stream; char *program_name = "gpgme-tool"; char *socket_path = NULL; -static int socket_fd = -1; +static assuan_fd_t socket_fd = ASSUAN_INVALID_FD; #define spacep(p) (*(p) == ' ' || *(p) == '\t') void cleanup_handler (int signum) { - if (socket_fd >= 0) + if (socket_fd != ASSUAN_INVALID_FD) { - close (socket_fd); /* unix only */ + assuan_sock_close (socket_fd); socket_fd = -1; } if (socket_path) @@ -651,6 +661,7 @@ cleanup_handler (int signum) free (socket_path); socket_path = NULL; } + assuan_sock_deinit (); exit (EXIT_SUCCESS); } @@ -3476,7 +3487,7 @@ gpgme_server (gpgme_tool_t gt, int socket_server) { gpg_error_t err; pid_t pid; - int sock = -1; + assuan_fd_t sock = ASSUAN_INVALID_FD; assuan_fd_t filedes[2]; struct server server; static const char hello[] = ("GPGME-Tool " VERSION " ready"); @@ -3505,21 +3516,25 @@ gpgme_server (gpgme_tool_t gt, int socket_server) { struct sockaddr_un addr; - socket_fd = socket (AF_UNIX, SOCK_STREAM, 0); - if (socket_fd == -1) + err = assuan_sock_init (); + if (err) + log_error (1, err, "can't initialize socket wrappers"); + + socket_fd = assuan_sock_new (AF_UNIX, SOCK_STREAM, 0); + if (socket_fd == ASSUAN_INVALID_FD) { - perror ("can't create stream socket"); - exit (EXIT_FAILURE); + err = gpg_error_from_syserror (); + log_error (1, err, "can't create stream socket"); } addr.sun_family = AF_UNIX; - socket_path = expand_path ("~/.gnupg/S.gpgme-tool"); + socket_path = expand_path ("~/.gnupg/S.gpgme-tool"); strcpy (addr.sun_path, socket_path); - if (bind (socket_fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un))) + if (assuan_sock_bind (socket_fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un))) { - perror ("can't bind to stream socket"); - exit (EXIT_FAILURE); + err = gpg_error_from_syserror (); + log_error (1, err, "can't create stream socket"); } #ifdef HAVE_SIGNAL_H @@ -3527,21 +3542,27 @@ gpgme_server (gpgme_tool_t gt, int socket_server) signal (SIGTERM, &cleanup_handler); #endif - listen (socket_fd, 5); + if (listen (HANDLE2SOCKET (socket_fd), 5)) + { + err = gpg_error_from_syserror (); + log_error (1, err, "listen() failed"); + } for (;;) { - sock = accept (socket_fd, NULL, 0); - if (sock == -1) + sock = SOCKET2HANDLE (accept (HANDLE2SOCKET (socket_fd), NULL, 0)); + if (sock == ASSUAN_INVALID_FD) { - perror("accept"); + err = gpg_error_from_syserror (); + log_error (0, err, "accept() failed"); continue; } if ((pid = fork ()) == -1) { - perror ("fork"); - close (sock); - sock = -1; + err = gpg_error_from_syserror (); + log_error (1, err, "fork() failed"); + assuan_sock_close (sock); + sock = ASSUAN_INVALID_FD; continue; } else if (pid == 0)