xref: /openbmc/docs/anti-patterns.md (revision 2c17ba1e)
1# OpenBMC Anti-patterns
2
3From [Wikipedia](https://en.wikipedia.org/wiki/Anti-pattern):
4
5
6"An anti-pattern is a common response to a recurring problem that is usually
7ineffective and risks being highly counterproductive."
8
9
10The developers of OpenBMC do not get 100% of decisions right 100% of the time.
11That, combined with the fact that software development is often an exercise in
12copying and pasting, results in mistakes happening over and over again.
13
14
15This page aims to document some of the anti-patterns that exist in OpenBMC to
16ease the job of those reviewing code.  If an anti-pattern is spotted, rather
17that repeating the same explanations over and over, a link to this document can
18be provided.
19
20
21<!-- begin copy/paste on next line -->
22
23## Anti-pattern template [one line description]
24
25### Identification
26(1 paragraph) Describe how to spot the anti-pattern.
27
28### Description
29(1 paragraph) Describe the negative effects of the anti-pattern.
30
31### Background
32(1 paragraph) Describe why the anti-pattern exists.  If you don't know, try
33running git blame and look at who wrote the code originally, and ask them on the
34mailing list or in IRC what their original intent was, so it can be documented
35here (and you may possibly discover it isn't as much of an anti-pattern as you
36thought).  If you are unable to determine why the anti-pattern exists, put:
37"Unknown" here.
38
39### Resolution
40(1 paragraph) Describe the preferred way to solve the problem solved by the
41anti-pattern and the positive effects of solving it in the manner described.
42
43<!-- end copy/paste on previous line -->
44
45## Custom ArgumentParser object
46
47### Identification
48
49The ArgumentParser object is typically present to wrap calls to get options.
50It abstracts away the parsing and provides a `[]` operator to access the
51parameters.
52
53### Description
54
55Writing a custom ArgumentParser object creates nearly duplicate code in a
56repository.  The ArgumentParser itself is the same, however, the options
57provided differ.  Writing a custom argument parser re-invents the wheel on
58c++ command line argument parsing.
59
60### Background
61
62The ArgumentParser exists because it was in place early and then copied into
63each new repository as an easy way to handle argument parsing.
64
65### Resolution
66
67The CLI11 library was designed and implemented specifically to support modern
68argument parsing.  It handles the cases seen in OpenBMC daemons and has some
69handy built-in validators, and allows easy customizations to validation.
70
71## Explicit AC_MSG_ERROR on PKG_CHECK_MODULES failure
72
73### Identification
74```
75PKG_CHECK_MODULES(
76    [PHOSPHOR_LOGGING],
77    [phosphor-logging],
78    [],
79    [AC_MSG_ERROR([Could not find phosphor-logging...openbmc/phosphor-logging package required])])
80```
81
82### Description
83
84The autotools PKG_CHECK_MODULES macro provides the ability to specify an
85"if found" and "if not found" behavior.  By default, the "if not found"
86behavior will list the package not found.  In many cases, this is sufficient
87to a developer to know what package is missing.  In most cases, it's another
88OpenBMC package.
89
90If the library sought's name isn't related to the package providing it, then
91the failure message should be set to something more useful to the developer.
92
93### Resolution
94
95Use the default macro behavior when it is clear that the missing package is
96another OpenBMC package.
97
98```
99PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging])
100```
101
102## Explicit listing of shared library packages in RDEPENDS in bitbake metadata
103
104### Identification
105```
106RDEPENDS_${PN} = "libsystemd"
107```
108
109### Description
110Out of the box bitbake examines built applications, automatically adds runtime
111dependencies and thus ensures any library packages dependencies are
112automatically added to images, sdks, etc.  There is no need to list them
113explicitly in a recipe.
114
115Dependencies change over time, and listing them explicitly is likely prone to
116errors - the net effect being unnecessary shared library packages being
117installed into images.
118
119Consult
120https://www.yoctoproject.org/docs/latest/mega-manual/mega-manual.html#var-RDEPENDS
121for information on when to use explicit runtime dependencies.
122
123### Background
124The initial bitbake metadata author for OpenBMC was not aware that bitbake
125added these dependencies automatically.  Initial bitbake metadata therefore
126listed shared library dependencies explicitly, and was subsequently copy pasted.
127
128### Resolution
129Do not list shared library packages in RDEPENDS.  This eliminates the
130possibility of installing unnecessary shared library packages due to
131unmaintained library dependency lists in bitbake metadata.
132
133## Use of /usr/bin/env in systemd service files
134
135### Identification
136In systemd unit files:
137```
138[Service]
139
140ExecStart=/usr/bin/env some-application
141```
142
143### Description
144Outside of OpenBMC, most applications that provide systemd unit files don't
145launch applications in this way.  So if nothing else, this just looks strange
146and violates the [princple of least
147astonishment](https://en.wikipedia.org/wiki/Principle_of_least_astonishment).
148
149### Background
150This anti-pattern exists because a requirement exists to enable live patching of
151applications on read-only filesystems.  Launching applications in this way was
152part of the implementation that satisfied the live patch requirement.  For
153example:
154
155```
156/usr/bin/phosphor-hwmon
157```
158
159on a read-only filesystem becomes:
160
161```
162/usr/local/bin/phosphor-hwmon`
163```
164
165on a writeable /usr/local filesystem.
166
167### Resolution
168The /usr/bin/env method only enables live patching of applications.  A method
169that supports live patching of any file in the read-only filesystem has emerged.
170Assuming a writeable filesystem exists _somewhere_ on the bmc, something like:
171
172```
173mkdir -p /var/persist/usr
174mkdir -p /var/persist/work/usr
175mount -t overlay -o lowerdir=/usr,upperdir=/var/persist/usr,workdir=/var/persist/work/usr overlay /usr
176```
177can enable live system patching without any additional requirements on how
178applications are launched from systemd service files.  This is the preferred
179method for enabling live system patching as it allows OpenBMC developers to
180write systemd service files in the same way as most other projects.
181
182To undo existing instances of this anti-pattern remove /usr/bin/env from systemd
183service files and replace with the fully qualified path to the application being
184launched.  For example, given an application in /usr/bin:
185
186```
187sed -i s,/usr/bin/env ,/usr/bin/, foo.service
188```
189
190## Placement of applications in /sbin or /usr/sbin
191
192### Identification
193OpenBMC applications that are installed in /usr/sbin.  $sbindir in bitbake
194metadata, makefiles or configure scripts.
195
196### Description
197Installing OpenBMC applications in /usr/sbin violates the [principle of least
198astonishment](https://en.wikipedia.org/wiki/Principle_of_least_astonishment) and
199more importantly violates the
200[FHS](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard).
201
202### Background
203The sbin anti-pattern exists because the FHS was misinterpreted at an early point
204in OpenBMC project history, and has proliferated ever since.
205
206From the hier(7) man page:
207
208```
209/usr/bin This is the primary directory for executable programs.  Most programs
210executed by normal users which are not needed for booting or for repairing the
211system and which are not installed locally should be placed in this directory.
212
213/usr/sbin This directory contains program binaries for system administration
214which are not essential for the boot process, for mounting /usr, or for system
215repair.
216```
217The FHS description for /usr/sbin refers to "system administration" but the
218de-facto interpretation of the system being administered refers to the OS
219installation and not a system in the OpenBMC sense of managed system.  As such
220OpenBMC applications should be installed in /usr/bin.
221
222### Resolution
223Install OpenBMC applications in /usr/bin/.
224