console-server: Fix pointer arithmetic in container_of() implementation

Pointer arithmetic is performed in units of the size of the pointed-to
object. Cast to char to ensure that both the pointer arithmetic is
defined, and that subtracting the result of the offsetof() stays
in-bounds.

The issue was detected by clang-tidy:

```
>>> /usr/bin/clang-tidy --use-color -export-fixes .../obmc-console/buildc92dibdo/meson-private/clang-tidy-fix/log-handler.c.m2f5figo.yaml -quiet -p .../obmc-console/buildc92dibdo .../obmc-console/log-handler.c
9557 warnings generated.
../log-handler.c:50:9: error: suspicious usage of 'offsetof(...)' in pointer arithmetic; this scaled value will be scaled again by the '-' operator [bugprone-sizeof-expression,-warnings-as-errors]
   50 |         return container_of(handler, struct log_handler, handler);
      |                ^
../console-server.h:272:27: note: expanded from macro 'container_of'
  272 |         ((type *)((void *)((ptr) - offsetof(type, member))))
      |                                  ^ ~~~~~~~~~~~~~~~~~~~~~~
../log-handler.c:50:9: note: '-' in pointer arithmetic internally scales with 'sizeof(struct handler)' == 8
   50 |         return container_of(handler, struct log_handler, handler);
      |                ^
../console-server.h:272:27: note: expanded from macro 'container_of'
  272 |         ((type *)((void *)((ptr) - offsetof(type, member))))
      |                                  ^
```

Change-Id: I808428c0a751abeb4409cf28dd5e95588ae5c0e2
Fixes: 1a0e03b4385e ("Split IO handling code into separate handler modules")
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
2 files changed
tree: 5c2846ccea19d37a6dbf2d0382bd9f1bce5c25d6
  1. conf/
  2. docs/
  3. subprojects/
  4. test/
  5. .clang-format
  6. .clang-tidy
  7. .gitignore
  8. .travis.yml
  9. CHANGELOG.md
  10. config-internal.h
  11. config.c
  12. config.h
  13. console-client.c
  14. console-dbus.c
  15. console-mux.c
  16. console-mux.h
  17. console-server.c
  18. console-server.h
  19. console-socket.c
  20. LICENSE
  21. log-handler.c
  22. meson.build
  23. meson.options
  24. OWNERS
  25. README.md
  26. ringbuffer.c
  27. socket-handler.c
  28. tty-handler.c
  29. util.c
  30. util.h
README.md

obmc-console

To Build

To build this project, run the following shell commands:

meson setup build
meson compile -C build

To test:

dbus-run-session meson test -C build

To Run Server

Running the server requires a serial port (e.g. /dev/ttyS0):

touch obmc-console.conf
./obmc-console-server --config obmc-console.conf ttyS0

To Connect Client

To connect to the server, simply run the client:

./obmc-console-client

To disconnect the client, use the standard ~. combination.

Underlying design

This shows how the host UART connection is abstracted within the BMC as a Unix domain socket.

            +---------------------------------------------------------------------------------------------+
            |                                                                                             |
            |       obmc-console-client       unix domain socket         obmc-console-server              |
            |                                                                                             |
            |     +----------------------+                           +------------------------+           |
            |     |   client.2200.conf   |  +---------------------+  | server.ttyVUART0.conf  |           |
        +---+--+  +----------------------+  |                     |  +------------------------+  +--------+-------+
Network    | 2200 +-->                      +->+ @obmc-console.host0 +<-+                        <--+ /dev/ttyVUART0 |   UARTs
        +---+--+  | console-id = "host0" |  |                     |  |  console-id = "host0"  |  +--------+-------+
            |     |                      |  +---------------------+  |                        |           |
            |     +----------------------+                           +------------------------+           |
            |                                                                                             |
            |                                                                                             |
            |                                                                                             |
            +---------------------------------------------------------------------------------------------+

This supports multiple independent consoles. The console-id is a unique portion for the unix domain socket created by the obmc-console-server instance. The server needs to know this because it needs to know what to name the pipe; the client needs to know it as it needs to form the abstract socket name to which to connect.

Mux Support

In some hardware designs, multiple UARTS may be available behind a Mux. Please reference docs/mux-support.md in that case.

Sample Development Setup

For developing obmc-console, we can use pseudo terminals (pty's) in Linux.

The socat command will output names of 2 pty's, one of which is the master and the other one is the slave. The master pty can be used to emulate a UART.

$ socat -d -d pty,raw,echo=0,link=pty1 pty,raw,echo=0,link=pty2

$ obmc-console-server --console-id dev $(realpath pty2)

$ obmc-console-client -i dev

# this message should appear for the client
$ echo "hi" > pty1