Modifying the Hello Application

Modifying the Hello Application#

After modifying the hello scenario, which starts the hello application, we will now modify this application.

The Hello Package#

hello is an L4Re Package. That means, it is part of the L4Re source tree and gets built by the L4Re build system. It is located at $L4RE_SRCDIR/pkg/hello.

The actual source code for the application is in server/src/main.c:

pkg/hello/server/src/main.c#
 9#include <stdio.h>
10#include <unistd.h>
11
12int
13main(void)
14{
15  for (;;)
16    {
17      puts("Hello World!");
18      sleep(1);
19    }
20}

This is trivial C code. The instructions for the build system can be found in server/src/Makefile:

pkg/hello/server/src/Makefile#
1PKGDIR           ?= ../..
2L4DIR            ?= $(PKGDIR)/../..
3
4TARGET           = hello
5SRC_C            = main.c
6
7include $(L4DIR)/mk/prog.mk

This tells the build system to build a target binary named hello from the source file main.c. The include makes sure that the output is a standalone L4Re application.

Hello User#

L4Re applications can be arbitrarily expanded like any other application. In this guide we will add a new header file, a new source file and will make use of command line arguments. This will show you how to add new sources and header files to be built and that command line arguments just work how you would expect.

Let’s not greet the whole world, but only a specific creature. Let’s also encapsulate this greeting functionality. Let’s add a C source file named server/src/greeting.c as well as a header file server/src/greeting.h:

pkg/hello/server/include/greeting.h#
1#pragma once
2
3void greet(char* name);
pkg/hello/server/src/greeting.c#
1#include <stdio.h>
2
3#include "greeting.h"
4
5void greet(char* name)
6{
7  printf("Hello %s!\n", name);
8}

For this functionality to be used, we have to adjust the main.c file:

pkg/hello/server/src/main.c#
 9#include <unistd.h>
10
11#include "greeting.h"
12
13int
14main(int argc, char* argv[])
15{
16  char* name = "";
17  if (argc < 2)
18    {
19      name = "World";
20    }
21  else
22    {
23      name = argv[1];
24    }
25  for (;;)
26    {
27      greet(name);
28      sleep(1);
29    }
30}

For it to be properly build, we have to adjust the Makefile. We need to add the source file to the list of source files to compile and we have to give a hint on where to get the included header file from:

pkg/hello/server/src/Makefile#
1PKGDIR           ?= ../..
2L4DIR            ?= $(PKGDIR)/../..
3
4TARGET           = hello
5SRC_C            = main.c greeting.c
6
7include $(L4DIR)/mk/prog.mk

Lastly, we have to actually use the new feature in our scenario:

conf/example/hello.cfg#
1-- vim:ft=lua
2-- this is a configuration to start 'hello'
3
4local L4 = require("L4");
5
6L4.default_loader:start({ log = { "hello-1", "red" } }, "rom/hello user1");
7L4.default_loader:start({ log = { "hello-2", "cyan" } }, "rom/hello user2");

Now we can rebuild an run the scenario to see two different users be greeted once a cecond.

Next Steps#