170
167
/* Name of the communication socket */
171
168
static char *socket_name;
170
/* We need to keep track of the server's nonces (these are dummies for
172
static assuan_sock_nonce_t socket_nonce;
174
174
/* Debug flag to disable the ticker. The ticker is in fact not
175
175
disabled but it won't perform any ticker specific actions. */
180
180
static char *create_socket_name (int use_standard_socket,
181
181
char *standard_name, char *template);
182
static int create_server_socket (int is_standard_name, const char *name);
182
static gnupg_fd_t create_server_socket (int is_standard_name, const char *name,
183
assuan_sock_nonce_t *nonce);
184
185
static void *start_connection_thread (void *arg);
185
186
static void handle_connections (int listen_fd);
356
358
/* Set default options. */
357
359
opt.pcsc_driver = DEFAULT_PCSC_DRIVER;
361
#ifdef HAVE_W32_SYSTEM
362
standard_socket = 1; /* Under Windows we always use a standard
360
367
shell = getenv ("SHELL");
361
368
if (shell && strlen (shell) >= 3 && !strcmp (shell+strlen (shell)-3, "csh") )
548
555
char *filename = NULL;
549
556
char *filename_esc;
551
if (!config_filename)
552
filename = make_filename (opt.homedir, "scdaemon.conf", NULL );
559
filename = xstrdup (config_filename);
561
filename = make_filename (opt.homedir, "scdaemon.conf", NULL);
553
562
filename_esc = percent_escape (filename, NULL);
555
564
printf ("gpgconf-scdaemon.conf:%lu:\"%s\n",
621
630
back the name of that socket. */
622
631
if (multi_server)
624
socket_name = create_socket_name (0,
633
socket_name = create_socket_name (standard_socket,
626
635
"/tmp/gpg-XXXXXX/S.scdaemon");
628
fd = create_server_socket (0, socket_name);
637
fd = FD2INT(create_server_socket (standard_socket,
638
socket_name, &socket_nonce));
631
641
tattr = pth_attr_new();
640
650
strerror (errno) );
643
ctrl->thread_startup.fd = -1;
653
ctrl->thread_startup.fd = GNUPG_INVALID_FD;
644
654
if ( !pth_spawn (tattr, start_connection_thread, ctrl) )
646
656
log_error ("error spawning pipe connection handler: %s\n",
662
674
{ /* Regular server mode */
676
#ifndef HAVE_W32_SYSTEM
667
681
/* Create the socket. */
668
socket_name = create_socket_name (0,
682
socket_name = create_socket_name (standard_socket,
670
684
"/tmp/gpg-XXXXXX/S.scdaemon");
672
fd = create_server_socket (0, socket_name);
686
fd = FD2INT (create_server_socket (standard_socket,
687
socket_name, &socket_nonce));
930
945
/* Create a Unix domain socket with NAME. IS_STANDARD_NAME indicates
931
946
whether a non-random socket is used. Returns the file descriptor
932
947
or terminates the process in case of an error. */
934
create_server_socket (int is_standard_name, const char *name)
949
create_server_socket (int is_standard_name, const char *name,
950
assuan_sock_nonce_t *nonce)
936
952
struct sockaddr_un *serv_addr;
941
#ifdef HAVE_W32_SYSTEM
942
fd = _w32_sock_new (AF_UNIX, SOCK_STREAM, 0);
944
fd = socket (AF_UNIX, SOCK_STREAM, 0);
957
fd = assuan_sock_new (AF_UNIX, SOCK_STREAM, 0);
958
if (fd == GNUPG_INVALID_FD)
948
960
log_error (_("can't create socket: %s\n"), strerror (errno));
957
969
len = (offsetof (struct sockaddr_un, sun_path)
958
970
+ strlen (serv_addr->sun_path) + 1);
960
#ifdef HAVE_W32_SYSTEM
961
rc = _w32_sock_bind (fd, (struct sockaddr*) serv_addr, len);
962
if (is_standard_name && rc == -1 )
965
rc = bind (fd, (struct sockaddr*) serv_addr, len);
968
rc = bind (fd, (struct sockaddr*) serv_addr, len);
972
rc = assuan_sock_bind (fd, (struct sockaddr*) serv_addr, len);
969
973
if (is_standard_name && rc == -1 && errno == EADDRINUSE)
972
rc = bind (fd, (struct sockaddr*) serv_addr, len);
976
rc = assuan_sock_bind (fd, (struct sockaddr*) serv_addr, len);
979
&& (rc=assuan_sock_get_nonce ((struct sockaddr*)serv_addr, len, nonce)))
980
log_error (_("error getting nonce for the socket\n"));
977
983
log_error (_("error binding socket to `%s': %s\n"),
978
serv_addr->sun_path, strerror (errno));
985
gpg_strerror (gpg_error_from_syserror ()));
986
assuan_sock_close (fd);
983
if (listen (fd, 5 ) == -1)
990
if (listen (FD2INT(fd), 5 ) == -1)
985
log_error (_("listen() failed: %s\n"), strerror (errno));
992
log_error (_("listen() failed: %s\n"),
993
gpg_strerror (gpg_error_from_syserror ()));
994
assuan_sock_close (fd);
1002
1010
ctrl_t ctrl = arg;
1012
if (ctrl->thread_startup.fd != GNUPG_INVALID_FD
1013
&& assuan_sock_check_nonce (ctrl->thread_startup.fd, &socket_nonce))
1015
log_info (_("error reading nonce on fd %d: %s\n"),
1016
FD2INT(ctrl->thread_startup.fd), strerror (errno));
1017
assuan_sock_close (ctrl->thread_startup.fd);
1004
1022
scd_init_default_ctrl (ctrl);
1005
1023
if (opt.verbose)
1006
log_info (_("handler for fd %d started\n"), ctrl->thread_startup.fd);
1024
log_info (_("handler for fd %d started\n"),
1025
FD2INT(ctrl->thread_startup.fd));
1008
scd_command_handler (ctrl, ctrl->thread_startup.fd);
1027
scd_command_handler (ctrl, FD2INT(ctrl->thread_startup.fd));
1010
1029
if (opt.verbose)
1011
log_info (_("handler for fd %d terminated\n"), ctrl->thread_startup.fd);
1030
log_info (_("handler for fd %d terminated\n"),
1031
FD2INT (ctrl->thread_startup.fd));
1013
1033
/* If this thread is the pipe connection thread, flag that a
1014
1034
shutdown is required. With the next ticker event and given that
1015
1035
no other connections are running the shutdown will then
1017
if (ctrl->thread_startup.fd == -1)
1037
if (ctrl->thread_startup.fd == GNUPG_INVALID_FD)
1018
1038
shutdown_pending = 1;
1020
1040
scd_deinit_default_ctrl (ctrl);
1160
1180
snprintf (threadname, sizeof threadname-1, "conn fd=%d", fd);
1161
1181
threadname[sizeof threadname -1] = 0;
1162
1182
pth_attr_set (tattr, PTH_ATTR_NAME, threadname);
1163
ctrl->thread_startup.fd = fd;
1183
ctrl->thread_startup.fd = INT2FD (fd);
1164
1184
if (!pth_spawn (tattr, start_connection_thread, ctrl))
1166
1186
log_error ("error spawning connection handler: %s\n",