Skip to content

Commit 5c0f9b0

Browse files
authored
Update Makefile and Add explanation (#133)
Add `PWD := $(CURDIR)` in Makefile and the explanation about `sudo make` when only having `PWD`.
1 parent 785c2fe commit 5c0f9b0

File tree

1 file changed

+103
-1
lines changed

1 file changed

+103
-1
lines changed

lkmpg.tex

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,116 @@ \subsection{The Simplest Module}
219219
\begin{code}
220220
obj-m += hello-1.o
221221

222+
PWD := $(CURDIR)
223+
222224
all:
223225
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
224226
225227
clean:
226228
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
227229
\end{code}
228230
229-
And finally just:
231+
In \verb|Makefile|, \verb|$(CURDIR)| can set to the absolute pathname of the current working directory(after all \verb|-C| options are processed, if any).
232+
See more about \verb|CURDIR| in \href{https://www.gnu.org/software/make/manual/make.html}{GNU make manual}.
233+
234+
And finally, just run \verb|make| directly.
235+
230236
\begin{codebash}
231237
make
232238
\end{codebash}
233239

240+
If there is no \verb|PWD := $(CURDIR)| statement in Makefile, then it may not compile correctly with \verb|sudo make|.
241+
Because some environment variables are specified by the security policy, they can't be inherited.
242+
The default security policy is \verb|sudoers|.
243+
In the \verb|sudoers| security policy, \verb|env_reset| is enabled by default, which restricts environment variables.
244+
Specifically, path variables are not retained from the user environment, they are set to default values (For more information see: \href{https://www.sudo.ws/docs/man/sudoers.man/}{sudoers manual}).
245+
You can see the environment variable settings by:
246+
247+
\begin{verbatim}
248+
$ sudo -s
249+
# sudo -V
250+
\end{verbatim}
251+
252+
Here is a simple Makefile as an example to demonstrate the problem mentioned above.
253+
254+
\begin{code}
255+
all:
256+
echo $(PWD)
257+
\end{code}
258+
259+
Then, we can use \verb|-p| flag to print out the environment variable values from the Makefile.
260+
261+
\begin{verbatim}
262+
$ make -p | grep PWD
263+
PWD = /home/ubuntu/temp
264+
OLDPWD = /home/ubuntu
265+
echo $(PWD)
266+
\end{verbatim}
267+
268+
The \verb|PWD| variable won't be inherited with \verb|sudo|.
269+
270+
\begin{verbatim}
271+
$ sudo make -p | grep PWD
272+
echo $(PWD)
273+
\end{verbatim}
274+
275+
However, there are three ways to solve this problem.
276+
277+
\begin{enumerate}
278+
\item {
279+
You can use the \verb|-E| flag to temporarily preserve them.
280+
281+
\begin{codebash}
282+
$ sudo -E make -p | grep PWD
283+
PWD = /home/ubuntu/temp
284+
OLDPWD = /home/ubuntu
285+
echo $(PWD)
286+
\end{codebash}
287+
}
288+
289+
\item {
290+
You can set the \verb|env_reset| disabled by editing the \verb|/etc/sudoers| with root and \verb|visudo|.
291+
292+
\begin{code}
293+
## sudoers file.
294+
##
295+
...
296+
Defaults env_reset
297+
## Change env_reset to !env_reset in previous line to keep all environment variables
298+
\end{code}
299+
300+
Then execute \verb|env| and \verb|sudo env| individually.
301+
302+
\begin{codebash}
303+
# disable the env_reset
304+
echo "user:" > non-env_reset.log; env >> non-env_reset.log
305+
echo "root:" >> non-env_reset.log; sudo env >> non-env_reset.log
306+
# enable the env_reset
307+
echo "user:" > env_reset.log; env >> env_reset.log
308+
echo "root:" >> env_reset.log; sudo env >> env_reset.log
309+
\end{codebash}
310+
311+
You can view and compare these logs to find differences between \verb|env_reset| and \verb|!env_reset|.
312+
}
313+
314+
\item {You can preserve environment variables by appending them to \verb|env_keep| in \verb|/etc/sudoers|.
315+
316+
\begin{code}
317+
## sudoers file.
318+
##
319+
...
320+
Defaults env_keep += ``ftp_proxy http_proxy https_proxy no_proxy PWD''
321+
\end{code}
322+
323+
After finishing setting modification, you can check the environment variable settings by:
324+
325+
\begin{verbatim}
326+
$ sudo -s
327+
# sudo -V
328+
\end{verbatim}
329+
}
330+
\end{enumerate}
331+
234332
If all goes smoothly you should then find that you have a compiled \verb|hello-1.ko| module.
235333
You can find info on it with the command:
236334
\begin{codebash}
@@ -327,6 +425,8 @@ \subsection{Hello and Goodbye}
327425
obj-m += hello-1.o
328426
obj-m += hello-2.o
329427
428+
PWD := $(CURDIR)
429+
330430
all:
331431
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
332432

@@ -468,6 +568,8 @@ \subsection{Modules Spanning Multiple Files}
468568
obj-m += startstop.o
469569
startstop-objs := start.o stop.o
470570

571+
PWD := $(CURDIR)
572+
471573
all:
472574
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
473575

0 commit comments

Comments
 (0)