1From 5ed3b4ded6cf3e4de6fc8c8739b84231b0285b0e Mon Sep 17 00:00:00 2001
2From: Dario Binacchi <dario.binacchi@amarulasolutions.com>
3Date: Fri, 5 May 2023 08:57:45 +0200
4Subject: [PATCH] Don't compile programs using fork() on MMU-less systems
5
6Systems that lack a MMU cannot use fork() to create the child process.
7The patch does not compile the affected programs on MMU-less systems.
8
9Co-developed-by: Marc Kleine-Budde <mkl@pengutronix.de>
10Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
11Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
12Upstream: https://github.com/linux-can/can-utils/commit/5ed3b4ded6cf3e4de6fc8c8739b84231b0285b0e
13---
14 CMakeLists.txt | 15 ++++++++++++---
15 GNUmakefile.am | 10 +++++++---
16 Makefile       | 16 +++++++++++++---
17 check_cc.sh    | 16 ++++++++++++++++
18 configure.ac   |  2 ++
19 fork_test.c    | 27 +++++++++++++++++++++++++++
20 6 files changed, 77 insertions(+), 9 deletions(-)
21 create mode 100755 check_cc.sh
22 create mode 100644 fork_test.c
23
24diff --git a/CMakeLists.txt b/CMakeLists.txt
25index 09ccd805de66..aee8ff7fca02 100644
26--- a/CMakeLists.txt
27+++ b/CMakeLists.txt
28@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.3)
29
30 project(can-utils LANGUAGES C)
31
32+include (CheckFunctionExists)
33 include (CheckSymbolExists)
34 include (GNUInstallDirs)
35
36@@ -25,12 +26,13 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSCM_TXTIME=SO_TXTIME")
37 include_directories (.)
38 include_directories (./include)
39
40+check_function_exists(fork HAVE_FORK)
41+
42 set(PROGRAMS_CANLIB
43     asc2log
44     canbusload
45     candump
46     cangen
47-    canlogserver
48     canplayer
49     cansend
50     cansequence
51@@ -39,6 +41,10 @@ set(PROGRAMS_CANLIB
52     slcanpty
53 )
54
55+if(HAVE_FORK)
56+    list(APPEND PROGRAMS_CANLIB canlogserver)
57+endif()
58+
59 set(PROGRAMS_J1939
60     j1939acd
61     j1939cat
62@@ -49,7 +55,6 @@ set(PROGRAMS_J1939
63
64 set(PROGRAMS
65     ${PROGRAMS_CANLIB}
66-    bcmserver
67     canfdtest
68     cangw
69     cansniffer
70@@ -57,13 +62,17 @@ set(PROGRAMS
71     isotpperf
72     isotprecv
73     isotpsend
74-    isotpserver
75     isotpsniffer
76     isotptun
77     slcan_attach
78     slcand
79 )
80
81+if(HAVE_FORK)
82+    list(APPEND PROGRAMS bcmserver)
83+    list(APPEND PROGRAMS isotpserver)
84+endif()
85+
86 add_executable(can-calc-bit-timing
87     calc-bit-timing/can-calc-bit-timing.c
88 )
89diff --git a/GNUmakefile.am b/GNUmakefile.am
90index 5a7ad75f682e..e818754db3a4 100644
91--- a/GNUmakefile.am
92+++ b/GNUmakefile.am
93@@ -75,14 +75,12 @@ EXTRA_DIST += \
94
95 bin_PROGRAMS = \
96 	asc2log \
97-	bcmserver \
98 	can-calc-bit-timing \
99 	canbusload \
100 	candump \
101 	canfdtest \
102 	cangen \
103 	cangw \
104-	canlogserver \
105 	canplayer \
106 	cansend \
107 	cansequence \
108@@ -91,7 +89,6 @@ bin_PROGRAMS = \
109 	isotpperf \
110 	isotprecv \
111 	isotpsend \
112-	isotpserver \
113 	isotpsniffer \
114 	isotptun \
115 	j1939acd \
116@@ -106,6 +103,13 @@ bin_PROGRAMS = \
117 	slcanpty \
118 	testj1939
119
120+if HAVE_FORK
121+bin_PROGRAMS += \
122+	bcmserver \
123+	canlogserver \
124+	isotpserver
125+endif
126+
127 j1939acd_LDADD = libj1939.la
128 j1939cat_LDADD = libj1939.la
129 j1939spy_LDADD = libj1939.la
130diff --git a/Makefile b/Makefile
131index 29eef997b290..a26ff3d75e67 100644
132--- a/Makefile
133+++ b/Makefile
134@@ -45,6 +45,8 @@ MAKEFLAGS := -k
135
136 CFLAGS := -O2 -Wall -Wno-parentheses
137
138+HAVE_FORK := $(shell ./check_cc.sh "$(CC)" fork_test.c)
139+
140 CPPFLAGS += \
141 	-I. \
142 	-Iinclude \
143@@ -66,10 +68,14 @@ PROGRAMS_ISOTP := \
144 	isotpperf \
145 	isotprecv \
146 	isotpsend \
147-	isotpserver \
148 	isotpsniffer \
149 	isotptun
150
151+ifeq ($(HAVE_FORK),1)
152+PROGRAMS_ISOTP += \
153+	isotpserver
154+endif
155+
156 PROGRAMS_J1939 := \
157 	j1939acd \
158 	j1939cat \
159@@ -87,14 +93,12 @@ PROGRAMS := \
160 	$(PROGRAMS_J1939) \
161 	$(PROGRAMS_SLCAN) \
162 	asc2log \
163-	bcmserver \
164 	can-calc-bit-timing \
165 	canbusload \
166 	candump \
167 	canfdtest \
168 	cangen \
169 	cansequence \
170-	canlogserver \
171 	canplayer \
172 	cansend \
173 	cansniffer \
174@@ -103,6 +107,12 @@ PROGRAMS := \
175 	mcp251xfd-dump \
176 	slcanpty
177
178+ifeq ($(HAVE_FORK),1)
179+PROGRAMS += \
180+	canlogserver \
181+	bcmserver
182+endif
183+
184 all: $(PROGRAMS)
185
186 clean:
187diff --git a/check_cc.sh b/check_cc.sh
188new file mode 100755
189index 000000000000..d85ad129da9d
190--- /dev/null
191+++ b/check_cc.sh
192@@ -0,0 +1,16 @@
193+#!/bin/sh
194+# SPDX-License-Identifier: GPL-2.0-only
195+# check_cc.sh - Helper to test userspace compilation support
196+# Copyright (c) 2015 Andrew Lutomirski
197+
198+CC="$1"
199+TESTPROG="$2"
200+shift 2
201+
202+if [ -n "$CC" ] && $CC -o /dev/null "$TESTPROG" -O0 "$@"; then
203+    echo 1
204+else
205+    echo 0
206+fi
207+
208+exit 0
209diff --git a/configure.ac b/configure.ac
210index 5493c9c7ccdf..9bf62a5c6409 100644
211--- a/configure.ac
212+++ b/configure.ac
213@@ -76,6 +76,8 @@ AC_CHECK_FUNCS([ \
214 	strtoul \
215 ])
216
217+AM_CONDITIONAL(HAVE_FORK, test "$ac_cv_func_fork_works" = "yes")
218+
219 # glibc versions before 2.17 needs to link with -lrt for clock_nanosleep
220 AC_SEARCH_LIBS([clock_nanosleep], [rt])
221
222diff --git a/fork_test.c b/fork_test.c
223new file mode 100644
224index 000000000000..036692392483
225--- /dev/null
226+++ b/fork_test.c
227@@ -0,0 +1,27 @@
228+/* SPDX-License-Identifier: GPL-2.0-only */
229+/*
230+ * Copyright (C) 2023 Dario Binacchi <dario.binacchi@amarulasolutions.com>
231+ *
232+ * This program is free software; you can redistribute it and/or modify
233+ * it under the terms of the version 2 of the GNU General Public License
234+ * as published by the Free Software Foundation
235+ *
236+ * This program is distributed in the hope that it will be useful,
237+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
238+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
239+ * GNU General Public License for more details.
240+ *
241+ * You should have received a copy of the GNU General Public License
242+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
243+ */
244+
245+#include <stdio.h>
246+#include <sys/types.h>
247+#include <unistd.h>
248+
249+int main(int argc, char **argv)
250+{
251+	fork();
252+
253+	return 0;
254+}
255--
2562.32.0
257
258