components/pcsc-lite/patches/09-signalhandler.patch
author Stephen Gaul Jr <steve.gaul@oracle.com>
Fri, 03 Jun 2016 09:30:42 -0700
changeset 6133 73641150a4de
permissions -rw-r--r--
PSARC/2016/217 Smartcard Reintroduction PSARC/2016/218 USB CCID PSARC/2016/221 PC/SC Lite smartcard middleware 22017759 Add pcsclite v1.8.14 to Userland consolidation 22017751 Add libccid v1.4.20 to Userland consolidation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6133
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     1
Upstream fix that will be included in the another release of pcsclite.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     2
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     3
From abe436e38aa58cb1140eff0d497ba721474c7703 Mon Sep 17 00:00:00 2001
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     4
From: Ludovic Rousseau <[email protected]>
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     5
Date: Sun, 24 Apr 2016 18:46:53 +0200
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     6
Subject: [PATCH] Fix signal handler by using only allowed functions
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     7
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     8
The signals are now treated in a special thread created just for that purpose.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
     9
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    10
Thanks to Andre Florath for the bug report
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    11
https://lists.alioth.debian.org/pipermail/pcsclite-muscle/Week-of-Mon-20160404/000561.html
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    12
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    13
[Pcsclite-muscle] pcscd jams when using '--auto-exit'
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    14
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    15
Andre Florath andre at florath.net
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    16
Sat Apr 9 06:06:44 UTC 2016
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    17
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    18
Hello!
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    19
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    20
Since some time I have problems with pcscd. I'm using pcscd in
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    21
conjunction with online banking and after a short period of working it
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    22
stops and jams the banking application.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    23
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    24
A 'strace' to the pcscd showed that it is still running somewhere
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    25
deep in the USB stack.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    26
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    27
The problem is, when manually running the the pcscd, there is no
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    28
problem at all - only when running from systemd.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    29
Therefore I searched for the differences and found one: the
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    30
'--auto-exit'. Downloaded the source and had a closer look.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    31
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    32
What I understand from the source code is, that when '--auto-exit' is
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    33
given, a SIGALRM is generated which (should) terminate the process.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    34
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    35
I have noticed that the signal handler 'signal_trap()' uses some
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    36
function calls that are not allowed in signal handlers; like:
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    37
* syslog()
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    38
* gettimeofday()
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    39
* remove()
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    40
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    41
Using this creates undefined behavior.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    42
(Please see 'man 7 signal' for a complete list of system calls that
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    43
are not allowed in signal handlers.)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    44
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    45
I found a workaround for the issue.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    46
Changed the service file to:
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    47
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    48
===
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    49
[Unit]
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    50
Description=PC/SC Smart Card Daemon
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    51
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    52
[Service]
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    53
ExecStart=/usr/sbin/pcscd --foreground --debug -a
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    54
ExecReload=/usr/sbin/pcscd --hotplug
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    55
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    56
[Install]
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    57
Also=pcscd.socket
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    58
===
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    59
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    60
and disabling the pcscd.socket gives me a stable system.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    61
(Yes - pcscd is now started at boot time and runs the whole time
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    62
 - which is fine for me.)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    63
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    64
If you need more information, please drop me a note.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    65
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    66
Kind regards
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    67
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    68
Andre
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    69
---
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    70
 src/pcscdaemon.c | 170 ++++++++++++++++++++++++++++++++++++-------------------
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    71
 1 file changed, 111 insertions(+), 59 deletions(-)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    72
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    73
diff --git a/src/pcscdaemon.c b/src/pcscdaemon.c
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    74
index 791f7f6..624e759 100644
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    75
--- a/src/pcscdaemon.c
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    76
+++ b/src/pcscdaemon.c
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    77
@@ -81,6 +81,7 @@ char SocketActivated = FALSE;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    78
 static int ExitValue = EXIT_FAILURE;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    79
 int HPForceReaderPolling = 0;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    80
 static int pipefd[] = {-1, -1};
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    81
+static int signal_handler_fd[] = {-1, -1};
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    82
 char Add_Serial_In_Name = TRUE;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    83
 char Add_Interface_In_Name = TRUE;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    84
 
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    85
@@ -89,7 +90,6 @@ char Add_Interface_In_Name = TRUE;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    86
  */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    87
 static void at_exit(void);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    88
 static void clean_temp_files(void);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    89
-static void signal_reload(int sig);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    90
 static void signal_trap(int);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    91
 static void print_version (void);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    92
 static void print_usage (char const * const);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    93
@@ -152,6 +152,10 @@ static void SVCServiceRunLoop(void)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    94
 			/* Nothing to do in case of a syscall interrupted
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    95
 			 * It happens when SIGUSR1 (reload) or SIGINT (Ctrl-C) is received
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    96
 			 * We just try again */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    97
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    98
+			/* we wait a bit so that the signal handler thread can do
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
    99
+			 * its job and set AraKiri if needed */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   100
+			SYS_USleep(1000);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   101
 			break;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   102
 
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   103
 		default:
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   104
@@ -162,6 +166,93 @@ static void SVCServiceRunLoop(void)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   105
 	}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   106
 }
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   107
 
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   108
+/**
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   109
+ * thread dedicated to handle signals
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   110
+ *
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   111
+ * a signal handler can not call any function. See signal(7) for a list
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   112
+ * of function that are safe to call from a signal handler.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   113
+ * The functions syslog(), gettimeofday() and remove() are NOT safe.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   114
+ */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   115
+static void *signal_thread(void *arg)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   116
+{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   117
+	(void)arg;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   118
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   119
+	while (TRUE)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   120
+	{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   121
+		int r;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   122
+		int sig;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   123
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   124
+		r = read(signal_handler_fd[0], &sig, sizeof sig);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   125
+		if (r < 0)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   126
+		{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   127
+			Log2(PCSC_LOG_ERROR, "read failed: %s", strerror(errno));
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   128
+			return NULL;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   129
+		}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   130
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   131
+		Log2(PCSC_LOG_INFO, "Received signal: %d", sig);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   132
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   133
+		/* signal for hotplug */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   134
+		if (SIGUSR1 == sig)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   135
+		{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   136
+#ifdef USE_USB
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   137
+			if (! AraKiri)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   138
+				HPReCheckSerialReaders();
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   139
+#endif
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   140
+			/* Reenable the signal handler.
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   141
+			 * This is needed on Solaris and HPUX. */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   142
+			(void)signal(SIGUSR1, signal_trap);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   143
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   144
+			continue;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   145
+		}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   146
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   147
+		/* do not wait if asked to terminate
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   148
+		 * avoids waiting after the reader(s) in shutdown for example */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   149
+		if (SIGTERM == sig)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   150
+		{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   151
+			Log1(PCSC_LOG_INFO, "Direct suicide");
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   152
+			at_exit();
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   153
+		}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   154
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   155
+		if (SIGALRM == sig)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   156
+		{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   157
+			/* normal exit without error */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   158
+			ExitValue = EXIT_SUCCESS;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   159
+		}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   160
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   161
+		/* the signal handler is called several times for the same Ctrl-C */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   162
+		if (AraKiri == FALSE)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   163
+		{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   164
+			Log1(PCSC_LOG_INFO, "Preparing for suicide");
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   165
+			AraKiri = TRUE;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   166
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   167
+			/* if still in the init/loading phase the AraKiri will not be
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   168
+			 * seen by the main event loop
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   169
+			 */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   170
+			if (Init)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   171
+			{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   172
+				Log1(PCSC_LOG_INFO, "Suicide during init");
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   173
+				at_exit();
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   174
+			}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   175
+		}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   176
+		else
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   177
+		{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   178
+			/* if pcscd do not want to die */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   179
+			static int lives = 2;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   180
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   181
+			lives--;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   182
+			/* no live left. Something is blocking the normal death. */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   183
+			if (0 == lives)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   184
+			{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   185
+				Log1(PCSC_LOG_INFO, "Forced suicide");
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   186
+				at_exit();
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   187
+			}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   188
+		}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   189
+	}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   190
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   191
+	return NULL;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   192
+}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   193
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   194
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   195
 int main(int argc, char **argv)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   196
 {
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   197
 	int rv;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   198
@@ -515,6 +606,20 @@ int main(int argc, char **argv)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   199
 	/* exits on SIGALARM to allow pcscd to suicide if not used */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   200
 	(void)signal(SIGALRM, signal_trap);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   201
 
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   202
+	if (pipe(signal_handler_fd) == -1)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   203
+	{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   204
+		Log2(PCSC_LOG_CRITICAL, "pipe() failed: %s", strerror(errno));
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   205
+		return EXIT_FAILURE;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   206
+	}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   207
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   208
+	pthread_t signal_handler_thread;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   209
+	rv = pthread_create(&signal_handler_thread, NULL, signal_thread, NULL);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   210
+	if (rv)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   211
+	{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   212
+		Log2(PCSC_LOG_CRITICAL, "pthread_create failed: %s", strerror(rv));
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   213
+		return EXIT_FAILURE;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   214
+	}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   215
+
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   216
 	/*
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   217
 	 * If PCSCLITE_IPC_DIR does not exist then create it
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   218
 	 */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   219
@@ -610,7 +715,7 @@ int main(int argc, char **argv)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   220
 	/*
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   221
 	 * Hotplug rescan
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   222
 	 */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   223
-	(void)signal(SIGUSR1, signal_reload);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   224
+	(void)signal(SIGUSR1, signal_trap);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   225
 
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   226
 	/*
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   227
 	 * Initialize the comm structure
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   228
@@ -730,66 +835,13 @@ static void clean_temp_files(void)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   229
 			strerror(errno));
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   230
 }
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   231
 
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   232
-static void signal_reload(/*@unused@*/ int sig)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   233
-{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   234
-	(void)signal(SIGUSR1, signal_reload);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   235
-
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   236
-	(void)sig;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   237
-
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   238
-	if (AraKiri)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   239
-		return;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   240
-
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   241
-#ifdef USE_USB
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   242
-	HPReCheckSerialReaders();
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   243
-#endif
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   244
-} /* signal_reload */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   245
-
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   246
 static void signal_trap(int sig)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   247
 {
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   248
-	Log2(PCSC_LOG_INFO, "Received signal: %d", sig);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   249
-
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   250
-	/* do not wait if asked to terminate
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   251
-	 * avoids waiting after the reader(s) in shutdown for example */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   252
-	if (SIGTERM == sig)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   253
-	{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   254
-		Log1(PCSC_LOG_INFO, "Direct suicide");
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   255
-		at_exit();
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   256
-	}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   257
-
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   258
-	if (SIGALRM == sig)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   259
-	{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   260
-		/* normal exit without error */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   261
-		ExitValue = EXIT_SUCCESS;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   262
-	}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   263
-
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   264
-	/* the signal handler is called several times for the same Ctrl-C */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   265
-	if (AraKiri == FALSE)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   266
-	{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   267
-		Log1(PCSC_LOG_INFO, "Preparing for suicide");
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   268
-		AraKiri = TRUE;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   269
-
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   270
-		/* if still in the init/loading phase the AraKiri will not be
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   271
-		 * seen by the main event loop
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   272
-		 */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   273
-		if (Init)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   274
-		{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   275
-			Log1(PCSC_LOG_INFO, "Suicide during init");
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   276
-			at_exit();
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   277
-		}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   278
-	}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   279
-	else
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   280
-	{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   281
-		/* if pcscd do not want to die */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   282
-		static int lives = 2;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   283
+	int r;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   284
 
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   285
-		lives--;
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   286
-		/* no live left. Something is blocking the normal death. */
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   287
-		if (0 == lives)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   288
-		{
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   289
-			Log1(PCSC_LOG_INFO, "Forced suicide");
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   290
-			at_exit();
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   291
-		}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   292
-	}
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   293
+	r = write(signal_handler_fd[1], &sig, sizeof sig);
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   294
+	if (r < 0)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   295
+		Log2(PCSC_LOG_ERROR, "write failed: %s", strerror(errno));
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   296
 }
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   297
 
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   298
 static void print_version (void)
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   299
-- 
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   300
1.9.1
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   301
73641150a4de PSARC/2016/217 Smartcard Reintroduction
Stephen Gaul Jr <steve.gaul@oracle.com>
parents:
diff changeset
   302