#include <assuan.h>
+/* 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. */
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)
free (socket_path);
socket_path = NULL;
}
+ assuan_sock_deinit ();
exit (EXIT_SUCCESS);
}
{
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");
{
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
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)