aboutsummaryrefslogtreecommitdiffstats
path: root/tools/squashfs4/patches/005-Don-t-use-sigwaitinfo-sigtimedwait-if-not-supported.patch
blob: 80e8824c46b4c367ed915fd3fa3442cd754f6d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
From dbe9747b4f09bd2f4d63af06e55c2c3ed35bfca1 Mon Sep 17 00:00:00 2001
From: Phillip Lougher <phillip@squashfs.org.uk>
Date: Tue, 7 Feb 2023 23:09:30 +0000
Subject: [PATCH] Don't use sigwaitinfo()/sigtimedwait() if not supported

If sigwaitinfo() and sigtimedwait() are not supported,
use sigwait() instead.

This will disable the queue/caches dump if ^\ (SIGQUIT)
is hit twice within a second.

But the queue/caches dump is still available if SIGHUP
is sent to the program.

Currently this check is applied to MAC OS X.  FreeBSD and
NetBSD appear to have these functions.

Signed-off-by: Phillip Lougher <phillip@squashfs.org.uk>
---
 squashfs-tools/info.c            | 25 ++-------------
 squashfs-tools/signals.h         | 54 ++++++++++++++++++++++++++++++++
 squashfs-tools/unsquashfs_info.c | 25 ++-------------
 3 files changed, 60 insertions(+), 44 deletions(-)
 create mode 100644 squashfs-tools/signals.h

--- a/squashfs-tools/info.c
+++ b/squashfs-tools/info.c
@@ -2,7 +2,7 @@
  * Create a squashfs filesystem.  This is a highly compressed read only
  * filesystem.
  *
- * Copyright (c) 2013, 2014, 2019, 2021
+ * Copyright (c) 2013, 2014, 2019, 2021, 2022, 2023
  * Phillip Lougher <phillip@squashfs.org.uk>
  *
  * This program is free software; you can redistribute it and/or
@@ -42,6 +42,7 @@
 #include "mksquashfs_error.h"
 #include "progressbar.h"
 #include "caches-queues-lists.h"
+#include "signals.h"
 
 static int silent = 0;
 static struct dir_ent *ent = NULL;
@@ -144,7 +145,6 @@ void dump_state()
 void *info_thrd(void *arg)
 {
 	sigset_t sigmask;
-	struct timespec timespec = { .tv_sec = 1, .tv_nsec = 0 };
 	int sig, waiting = 0;
 
 	sigemptyset(&sigmask);
@@ -152,26 +152,7 @@ void *info_thrd(void *arg)
 	sigaddset(&sigmask, SIGHUP);
 
 	while(1) {
-		if(waiting)
-			sig = sigtimedwait(&sigmask, NULL, &timespec);
-		else
-			sig = sigwaitinfo(&sigmask, NULL);
-
-		if(sig == -1) {
-			switch(errno) {
-			case EAGAIN:
-				/* interval timed out */
-				waiting = 0;
-				/* FALLTHROUGH */
-			case EINTR:
-				/* if waiting, the wait will be longer, but
-				   that's OK */
-				continue;
-			default:
-				BAD_ERROR("sigtimedwait/sigwaitinfo failed "
-					"because %s\n", strerror(errno));
-			}
-		}
+		sig = wait_for_signal(&sigmask, &waiting);
 
 		if(sig == SIGQUIT && !waiting) {
 			print_filename();
--- /dev/null
+++ b/squashfs-tools/signals.h
@@ -0,0 +1,54 @@
+#ifndef SIGNALS_H
+#define SIGNALS_H
+/*
+ * Create a squashfs filesystem.  This is a highly compressed read only
+ * filesystem.
+ *
+ * Copyright (c) 2023
+ * Phillip Lougher <phillip@squashfs.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * signals.h
+ */
+
+static inline int wait_for_signal(sigset_t *sigmask, int *waiting)
+{
+	int sig;
+
+#if defined(__APPLE__) && defined(__MACH__)
+	sigwait(sigmask, &sig);
+	*waiting = 0;
+#else
+	struct timespec timespec = { .tv_sec = 1, .tv_nsec = 0 };
+
+	while(1) {
+		if(*waiting)
+			sig = sigtimedwait(sigmask, NULL, &timespec);
+		else
+			sig = sigwaitinfo(sigmask, NULL);
+
+		if(sig != -1)
+			break;
+
+		if(errno == EAGAIN)
+			*waiting = 0;
+		else if(errno != EINTR)
+			BAD_ERROR("sigtimedwait/sigwaitinfo failed because %s\n", strerror(errno));
+	}
+#endif
+	return sig;
+}
+#endif
--- a/squashfs-tools/unsquashfs_info.c
+++ b/squashfs-tools/unsquashfs_info.c
@@ -2,7 +2,7 @@
  * Create a squashfs filesystem.  This is a highly compressed read only
  * filesystem.
  *
- * Copyright (c) 2013, 2021
+ * Copyright (c) 2013, 2021, 2023
  * Phillip Lougher <phillip@squashfs.org.uk>
  *
  * This program is free software; you can redistribute it and/or
@@ -40,6 +40,7 @@
 #include "squashfs_fs.h"
 #include "unsquashfs.h"
 #include "unsquashfs_error.h"
+#include "signals.h"
 
 char *pathname = NULL;
 
@@ -96,7 +97,6 @@ void dump_state()
 void *info_thrd(void *arg)
 {
 	sigset_t sigmask;
-	struct timespec timespec = { .tv_sec = 1, .tv_nsec = 0 };
 	int sig, waiting = 0;
 
 	sigemptyset(&sigmask);
@@ -104,26 +104,7 @@ void *info_thrd(void *arg)
 	sigaddset(&sigmask, SIGHUP);
 
 	while(1) {
-		if(waiting)
-			sig = sigtimedwait(&sigmask, NULL, &timespec);
-		else
-			sig = sigwaitinfo(&sigmask, NULL);
-
-		if(sig == -1) {
-			switch(errno) {
-			case EAGAIN:
-				/* interval timed out */
-				waiting = 0;
-				/* FALLTHROUGH */
-			case EINTR:
-				/* if waiting, the wait will be longer, but
-				   that's OK */
-				continue;
-			default:
-				BAD_ERROR("sigtimedwait/sigwaitinfo failed "
-					"because %s\n", strerror(errno));
-			}
-		}
+		sig = wait_for_signal(&sigmask, &waiting);
 
 		if(sig == SIGQUIT && !waiting) {
 			if(pathname)