summarylogtreecommitdiffstats
path: root/PKGBUILD
blob: fc0f7767696108ef42f2b12e709917d89bb73198 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# Maintainer: Chris Severance aur.severach aATt spamgourmet dott com

# Todo: Update addlogins and rmlogins for systemd* getty
# Todo: Console utility like drpadmin: trueportadmin or ptyxadmin or ???

# Bugs:
# The Statistics/Serial Screen on an SDS does not show the proper baud rate when a TruePort is set by stty -F.
# tpadm -d removes the config entry but does not kill the daemon.

_opt_DKMS=1           # This can be toggled between installs
_opt_MAXINSTPORTS=16  # Maximum install ports. Ranges from 1 to 255999.
                      # See instructions in release notes for more than a few thousand.
                      # Default=256 which is plenty for normal size installations.
                      # It's best to use a small number. Nodes are created for
                      # /dev/{tps,txc,txm}{0..n-1}
_opt_SSL=1            # 1 for SSL support, 0 for no SSL support
                      # Each device is independantly configured with or without SSL
_opt_defaultmode='666' # default: 660

_opt_Debug=0          # 0 for no symbols, 1 for symbols

#export KERNELRELEASE="$(basename $(dirname /usr/lib/modules/5.10.*/modules.alias))"

# The following two must be different and exactly two letters, and likely
# lowercase.
# This is experimental and changed at your own risk!
_opt_slavettypfx='tx'  # default tx
_opt_masterttypfx='px' # default px (this isn't very useful to users)

# Once installed you need to enable and start the service. Starting the service
# loads the kernel driver (ptyx) and loads a daemon (trueportd) for each
# configured device.

# systemctl enable trueport.service
# systemctl start trueport.service

# As long as the service is enabled, subsequent installs and upgrades will 
# start the service automatically.

# There are no man pages. For instructions download the 
# "TruePort Linux User Guide" linux_ug-6_2.pdf from Perle.
# Ignore the sections on install and remove. These functions are handled by 
# this PKGBUILD.

# 4 port IOLan STS 4D example
# Log into the STS 4D and switch all ports to TruePort
# addports -hup -k 2500 -opmode optimize_lan -client 192.168.10.229 0 3

# Is the kernel module loaded?
# lsmod | grep ptyx
# ptyx                   24576  14

# Are the daemons running?
# ps -e | grep trueportd
# 8534 tpc0     00:00:00 trueportd
# 8535 tpm1     00:00:00 trueportd
# 8536 tpm2     00:00:00 trueportd
# 8537 tpm3     00:00:00 trueportd

# Were the tty's created?
# ls /dev/[pt]x[0-9]*
# /dev/tx0000  /dev/tx0001  /dev/tx0002  /dev/tx0003

# Set baud rate.
# stty -F /dev/tx0000 57600
# Default baud rate at boot should be set in the IOLan web interface.

# Watch the RX/TX leds on the terminal server
# echo "Hello" > /dev/tx0000
# echo "Hello" > /dev/tx0001
# echo "Hello" > /dev/tx0002
# echo "Hello" > /dev/tx0003

# What ports are running?
# tpadm -l ALL

# Deactivate all ports
# tpadm -d 192.168.10.229:10001
# tpadm -d 192.168.10.229:10002
# tpadm -d 192.168.10.229:10003
# tpadm -d 192.168.10.229:10004

# Remove orphaned tty nodes. These disappear on the next reboot.
# rm -f /dev/tx[0-9]*

# 1 port IOlan DS1 as Trueport example
# Log into the DS1 and switch the port profile to TruePort
# addports -hup -k 2500 -opmode optimize_lan -client 192.168.10.229:10001 0 0

# 1 port IOlan DS1 or 3rd party brand as TCP Socket example
# Log into the DS1 and switch the port profile to TCP Sockets
# addports -l -hup -k 2500 -io mb_ascii -opmode optimize_lan -client 192.168.10.229:10001 0 0

# Uninstall cleanup: rm -rf /etc/trueport

set -u
pkgname='trueport'
_dlver='6.10.0-1'
pkgver="${_dlver//-/.}"
#_dlver='6.8.0-2' # only use this with a version change patch set
_srcdir="${pkgname}-${_dlver%%-*}"
pkgrel='5'
pkgdesc='tty driver for Perle IOLan+ DS TS SDS STS SCS JetStream LanStream LinkStream and 3rd party serial console terminal device servers'
_pkgdescshort='Perle TruePort driver for Ethernet serial servers'
arch=('i686' 'x86_64')
url='https://www.perle.com/'
license=('GPL' 'custom')
depends=('glibc' 'systemd')
if [ "${_opt_SSL}" -ne 0 ]; then
  depends+=('openssl')
fi
makedepends=('awk' 'sed' 'diffutils' 'patch')
backup=(etc/trueport/{config.tp,pktfwdcfg.tp,sslcfg.tp})
options=('!docs' '!emptydirs' '!strip')
install="${pkgname}-install.sh"
_verwatch=('https://www.perle.com/downloads/server_ds_ts.shtml' '\s\+<a.*trueport-linux-tgz">\([^<]\+\)<.*' 'f')
source=(
  "https://www.perle.com/downloads/drivers/trueport/linux/${pkgname}-${_dlver}.tgz"
  'tty_default_permissions.patch'
  #'trueport-patch-6.8.0-6.8.5.patch'
  # http://dpdk.org/dev/patchwork/patch/22003/ [dpdk-dev] kni: fix build with kernel 4.11 lib/librte_eal/linuxapp/kni/compat.h lib/librte_eal/linuxapp/kni/kni_dev.h
  # http://dpdk.org/dev/patchwork/patch/22037/
  # http://rglinuxtech.com/?p=1930
  # https://forum.manjaro.org/t/error-with-rtl8812au/24066
  #'trueport-patch-signal_pending-kernel-4-11.patch'
  '0000-tty_unregister_driver-void.patch'
  '0001-kernel-5.13-dropped-tty_check_change.patch'
  '0002-kernel-5.14-task_struct.state-unsigned-tty-flow-tty.patch'
  '0003-kernel-5.15-alloc_tty_driver-put_tty_driver.patch'
  '0004-kernel-6.1-TTY_DRIVER_MAGIC-remove-dead-code.patch'
  '0005-kernel-6.1-kernel_termios_to_user_termios_1-copy_to_user.patch'
  '0006-kernel-6.0-set_termios-const-ktermios.patch'
  '0007-kernel-6.6-struct-tty_operations-write-size_t.patch'
)
md5sums=('5a529676de30706133255ba4e8dae5b0'
         '56444e2f404aa2e6a2c9e8e2bd919fcf'
         'fb798f306553cb253b30ff5af5ba2f40'
         '91a070a4d13c13108dd980ce39ba36b9'
         '5206e863cf6340c05325d86935d4b40c'
         'f464a0217b85a76657bcb7aa022f9a95'
         '835219e7c692cc699ec23f4e183f70bf'
         '2cc11aa436180b3daabad46d1b6bf3fb'
         '291a8307fa6a6e651ee0a98eb1cba50c'
         'a06df58cd27782179eb2b75f965df62a')
sha256sums=('c21340a7523593da3e229b79cfbcf9e656772b2039e972dbca3947d138d55ffa'
            '28863731fd99e447dc456312ef33e40f93623b56da0d345e45f40e238ca49639'
            '5f806246751d3a91c59bd97273221d1066006bafc7ed598c3d93f9b7bdae65a1'
            '8c84111b927912ee002ba7e0f398ff02e067bffa02e6bedcf1cd08138362040c'
            'ee64f971753fb4fd8a488e32e8fe3de9c468a00a1d1b995329bcfe87c93cedf7'
            '04025f2dc6fe868e890853e355d1d31fff6d3c463ad89db1cedda5f3843078a3'
            '31ffbbe3de9605aa5c974e92b24c22876c8091eac3c1ada8bb83c76ad829cc8c'
            '1ed2794858d1268e53d73f3d4a34a4d2bf84a24ffbfa8babca7ce10e97e60d6c'
            'ad25f2d413ddd05c5561afea3e8d5b9e5b7859e5855bdb1da63c7a6f26f69599'
            'c4e7b750ac573ec035c2da643d73736feae741fabd86e460efe51d8086cd3138')

if [ "${_opt_DKMS}" -ne 0 ]; then
  depends+=('linux' 'dkms' 'linux-headers')
else
  makedepends+=('linux-headers')
fi

prepare() {
  set -u
  cd "${_srcdir}"

  # diff -pNaru5 'trueport-6.8.0' 'trueport-6.8.3' > 'trueport-patch-6.8.0-6.8.3-stack-smashing.patch'
  #patch -Nup1 < '../trueport-patch-6.8.0-6.8.3-stack-smashing.patch'
  # diff -pNau5 ptyx/ptys.c{.orig,} > '../trueport-patch-signal_pending-kernel-4-11.patch'
  #patch -Nup0 < '../trueport-patch-signal_pending-kernel-4-11.patch'
  # diff -pNaru5 'trueport-6.8.0' 'trueport-6.8.5' > 'trueport-patch-6.8.0-6.8.5.patch'
  #patch -Nup1 -i '../trueport-patch-6.8.0-6.8.5.patch'

  #cd ..; cp -pr trueport-6.10.0{,.orig}; false
  # diff -pNaru5 trueport-6.10.0{.orig,} > '0000-tty_unregister_driver-void.patch'
  patch -Nup1 -i "${srcdir}/0000-tty_unregister_driver-void.patch"

  #cd ..; cp -pr trueport-6.10.0{,.orig}; false
  # diff -pNaru5 trueport-6.10.0{.orig,} > '0001-kernel-5.13-dropped-tty_check_change.patch'
  patch -Nup1 -i "${srcdir}/0001-kernel-5.13-dropped-tty_check_change.patch"

  # tty.stopped https://lore.kernel.org/lkml/20210505091928.22010-13-jslaby@suse.cz/
  # unsigned write_room https://www.spinics.net/lists/linux-serial/msg42297.html
  # unsigned chars_in_buffer https://www.spinics.net/lists/linux-serial/msg42299.html
  #cd ..; cp -pr trueport-6.10.0{,.orig}; false
  # diff -pNaru5 trueport-6.10.0{.orig,} > '0002-kernel-5.14-task_struct.state-unsigned-tty-flow-tty.patch'
  patch -Nup1 -i "${srcdir}/0002-kernel-5.14-task_struct.state-unsigned-tty-flow-tty.patch"

  # http://lkml.iu.edu/hypermail/linux/kernel/2107.2/08799.html [PATCH 5/8] tty: drop alloc_tty_driver
  # http://lkml.iu.edu/hypermail/linux/kernel/2107.2/08801.html [PATCH 7/8] tty: drop put_tty_driver
  #cd ..; cp -pr trueport-6.10.0{,.orig}; false
  # diff -pNaru5 trueport-6.10.0{.orig,} > '0003-kernel-5.15-alloc_tty_driver-put_tty_driver.patch'
  patch -Nup1 -i "${srcdir}/0003-kernel-5.15-alloc_tty_driver-put_tty_driver.patch"

  #cd '..'; cp -pr "${_srcdir}" 'a'; ln -s "${_srcdir}" 'b'; false
  # diff -pNaru5 'a' 'b' > '0004-kernel-6.1-TTY_DRIVER_MAGIC-remove-dead-code.patch'
  patch -Nup1 -i "${srcdir}/0004-kernel-6.1-TTY_DRIVER_MAGIC-remove-dead-code.patch"

  #cd '..'; cp -pr "${_srcdir}" 'a'; ln -s "${_srcdir}" 'b'; false
  # diff -pNaru5 'a' 'b' > '0005-kernel-6.1-kernel_termios_to_user_termios_1-termios_internal.badpatch'
  # diff -pNaru5 'a' 'b' > '0005-kernel-6.1-kernel_termios_to_user_termios_1-copy_to_user.patch'
  patch -Nup1 -i "${srcdir}/0005-kernel-6.1-kernel_termios_to_user_termios_1-copy_to_user.patch"

  # https://lore.kernel.org/linux-arm-kernel/20220816115739.10928-9-ilpo.jarvinen@linux.intel.com/T/
  #cd '..'; cp -pr "${_srcdir}" 'a'; ln -s "${_srcdir}" 'b'; false
  # diff -pNaru5 'a' 'b' > '0006-kernel-6.0-set_termios-const-ktermios.patch'
  patch -Nup1 -i "${srcdir}/0006-kernel-6.0-set_termios-const-ktermios.patch"

  #cd '..'; cp -pr "${_srcdir}" 'a'; ln -s "${_srcdir}" 'b'; false
  # diff -pNaru5 'a' 'b' > '0007-kernel-6.6-struct-tty_operations-write-size_t.patch'
  patch -Nup1 -i "${srcdir}/0007-kernel-6.6-struct-tty_operations-write-size_t.patch"

  # insert parameters and make install script non interactive.
  set +u; msg2 'Checking SSL with rpm_build'; set -u
  sed -e 's:^\(DONE\)=.*$:'"\1='done';SSL='${_opt_SSL}':g" \
      -e 's:^\(MAXINSTPORTS\)=.*$:'"DONE='done';MAXINSTPORTS='${_opt_MAXINSTPORTS}':g" \
    'tar_install.sh' | \
  NEED_SSL= \
  sh -e -u -s -- 'rpm_build'

  sed -e '# Stripping symbols is the job of PKGBUILD. We need these for crash analysis.' \
      -e '/install/ s:-s:  :g' \
      -e '# Try clang' \
      -e '#s:^CC=.*$:CC=clang:g' \
    -i 'Makefile'

  # switch to kernel clean
  sed -e 's/^clean:.*$/clean:\n\t$(MAKE) -C $(KDIR) M=$$PWD clean\n\nold_&/g' \
      -e '# Switch SUBDIRS= to M= for Kernel 5.4' \
      -e '#s:SUBDIRS=:M=$(PWD) &:g' \
      -e '# Add back SUBDIRS for our detection' \
      -e 's:M=\([^ ]\+\):& SUBDIRS=\1 :g' \
      -e '# Remove cruft' \
      -e 's:^bindir=:#&:g' \
      -e '# Fix /lib' \
      -e 's:/lib/modules:/usr&:g' \
      -e '# No DKMS instructions say to do this but it works and keeps the MAKE line real simple' \
      -e 's:$(shell uname -r):$(KERNELRELEASE):g' \
      -e 's:`uname -r`:$(KERNELRELEASE):g' \
      -e '# DKMS sets KERNELRELEASE which accidentally launches phase 2 of this Makefile' \
      -e '# Fix by changing the detection var.' \
      -e '# SUBDIRS makes more sense to me because I can see it in the Makefile!' \
      -e 's:^ifeq ($(KERNELRELEASE):ifeq ($(SUBDIRS):g' \
      -e '1i KERNELRELEASE?=$(shell uname -r)' \
    -i 'ptyx/Makefile'

  # Remove CRLF line endings from some files
  sed -e 's:\r$::g' -i 'tp_ver.h' 'ptyx/ptyx.h' 'README'

  # Change the default ttys from tx and px (experimental)
  sed -e 's:\( fixed_ttyname,"\)px\(" \):'"\1${_opt_masterttypfx}\2:g" \
      -e 's:\( slave_ttyname, "/dev/\%s\%04d","\)tx\(",\):'"\1${_opt_slavettypfx}\2:g" \
    -i 'trueportd.c'
  sed -e 's:^\(FORMAT\)=tx:'"\1=${_opt_slavettypfx}:g" -i 'addports'
  sed -e 's:^\(FULLTTYNAME\="\)tx:'"\1${_opt_slavettypfx}:g" \
      -e 's:\(grep "\)tx:'"\1${_opt_slavettypfx}:g" \
    -i 'tplogin'

  # Add code for default permissions and chown group to uucp
  # I tried to change system("mknod") to mknod() but the permissions
  # wouldn't stick without a separate call to chmod()
  sed -e "s:@TRUEPORT_TTY_PERMISSIONS@:${_opt_defaultmode}:g" \
      -e "s:@TRUEPORT_TTY_GROUP@:uucp:g" \
    "${srcdir}/tty_default_permissions.patch" | \
  patch -Nbup0
  # diff -pNau5 trueportd.c{.orig,} > '../tty_default_permissions.patch' # Then change the two @@ items above

  'ma'ke -s -j1 clean # keep git-aurcheck quiet
  set +u
}

build() {
  set -u
  cd "${_srcdir}"
  local _cflags=('-s' '-g -rdynamic')
  #_cflags[0]+=' -fno-stack-protector'
  local _makeflags=('-s' '')
  CFLAGS="${CFLAGS} ${_cflags[${_opt_Debug}]}" \
  make ${_makeflags[${_opt_Debug}]} -j1 # Too small to benefit from multi processor compile
  set +u
}

package() {
  set -u
  cd "${_srcdir}"

  if [ "${_opt_DKMS}" -eq 0 ]; then
    # I don't want Linux version info showing on AUR web. After a few months 'linux<0.0.0' makes it look like an out of date package.
    local _kernelversionsmall="$(uname -r)"
    _kernelversionsmall="${_kernelversionsmall%%-*}"
    _kernelversionsmall="${_kernelversionsmall%\.0}" # trim 4.0.0 -> 4.0, 4.1.0 -> 4.1
    # prevent the mksrcinfo bash emulator from getting these vars!
    eval 'conf''licts=("linux>${_kernelversionsmall}" "linux<${_kernelversionsmall}")'
    eval 'dep''ends+=("linux=${_kernelversionsmall}")'
  fi

  make -s -j1 DESTDIR="${pkgdir}" install
  rm -f '/tmp/files' # Not used by postinstall. Probably used in the rpm_build.

  # Fix postinstall to generate modprobe.conf. Stop after the first section.
  install -dm755 "${pkgdir}/etc/modprobe.d"
  sed -e 's:/etc/modprobe:"${DESTDIR}"&:g' \
      -e 's:^if \[ -x /sbin/depmod:exit 0\n&:g' \
      -e '#s:-x /sbin/depmod:-x "/sbin/depmod":g' \
      -e '#s:/sbin/depmod -a:true:g' \
      -e '#s:^CHKCONFIG_BIN=""$:exit 0:g' \
    'postinstall.sh' > 'postinstall.sh.Arch'
  MAXINSTPORTS="${_opt_MAXINSTPORTS}" \
  DESTDIR="${pkgdir}" \
  sh -e -u 'postinstall.sh.Arch'
  rm 'postinstall.sh.Arch'

  # Postinstall functions are either handled by our install or not valid with systemd
  rm -f "${pkgdir}/etc/trueport"/{postinstall.sh,uninstall.sh}

  # We use the SysV init script but we don't want it in init.d where 
  # systemd SysV compatibility might try to run it.
  mv -f "${pkgdir}/etc/init.d/trueport" "${pkgdir}/etc/trueport/trueport"
  rmdir "${pkgdir}/etc/init.d"

  # systemctl start loads the kernel module and any number of daemons. No daemon
  # is loaded if no ports are configured. systemd stops the service if nothing forks.
  # Notify was the only way I could find to keep the service running.
  sed -e 's/[^a-z];;/systemd-notify --ready;;/' -i "${pkgdir}/etc/trueport/trueport"

  # systemd service
  install -Dm644 <(cat << EOF
# Automatically generated by ${pkgname}-${pkgver} PKGBUILD from Arch Linux AUR
# https://aur.archlinux.org/

[Unit]
Description=${_pkgdescshort}
After=network.target

[Service]
Type=notify
ExecStart=/etc/trueport/trueport start
ExecStop=/etc/trueport/trueport stop
ExecReload=/etc/trueport/trueport restart
RemainAfterExit=yes
TimeoutStopSec=15

[Install]
WantedBy=multi-user.target
EOF
  ) "${pkgdir}/usr/lib/systemd/system/trueport.service"

  if [ "${_opt_DKMS}" -ne 0 ]; then
    rm -rf "${pkgdir}/usr/lib/modules/"
    local _dkms="${pkgdir}/usr/src/${pkgname}-${pkgver}"
    install -Dm644 <(cat << EOF
# Automatically generated by ${pkgname}-${pkgver} PKGBUILD from Arch Linux AUR
# https://aur.archlinux.org/

PACKAGE_NAME="${pkgname}"
PACKAGE_VERSION="${pkgver}"
AUTOINSTALL="yes"

BUILT_MODULE_NAME[0]="ptyx"
BUILT_MODULE_LOCATION[0]="ptyx"
# Using all processors doesn't compile this tiny module any faster.
MAKE[0]="make -j1 -C 'ptyx'"
CLEAN[0]="make -j1 -C 'ptyx' clean"
# Placing the DKMS generated module in a different location than the standard install prevents conflicts when PKGBUILD _opt_DKMS is toggled
DEST_MODULE_LOCATION[0]="/kernel/drivers/misc"
EOF
    ) "${_dkms}/dkms.conf"
    install -Dpm644 ptyx/* -t "${_dkms}/ptyx/"
    rm "${_dkms}/ptyx/modules.order"
    install -pm644 'tp_ver.h' 'tp.h' -t "${_dkms}/"
    make -s -C "${_dkms}/ptyx/" KERNELRELEASE="$(uname -r)" clean
  fi
  set +u
}

set +u