babelouest / ulfius

Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
https://babelouest.github.io/ulfius
GNU Lesser General Public License v2.1
1.07k stars 183 forks source link

Websocket_example gives segmentation fault, continued #123 #172

Closed Knutselsmurf1 closed 3 years ago

Knutselsmurf1 commented 3 years ago

I have installed Ubuntu 20.04.1 LTS in Hyper-V virtual environment.

I installed Ulfius: $ sudo apt install libulfius-dev uwsc

I downloaded the examples 2.6 and compiled them: $ cd example_programs $ mkdir build $ cd build $ cmake .. $ make

$ ./websocket_server

If I open http://localhost:9275/static/index.html then it crashes “Segmentation fault (core dumped)”? However I tried as suggested in #123, the 2.6 code. Did I miss something?

Can or should I use a later Ulfius, or will become a later Ulfius the default version for Ubuntu 20.04 LTS?

babelouest commented 3 years ago

It's not recommended to mix different Ulfius versions.

Ubuntu 20.04 provides Ulfius 2.5.2 while Ulfius' current version is 2.6.9.

The package libulfius-dev in Ubuntu provides the example programs.

So I suggest you use the Ubuntu example source code or install a pre-compiled package in the release page.

Check the install documentation for more details.

babelouest commented 3 years ago

Erratum: I just realized the cmake script for websocket server example is incomplete, the static files are missing when you run the program. You can add a link to the static files in the build directory, that would fix the issue:

$ cd example_programs
$ mkdir build
$ cd build
$ cmake ..
$ make
$ ln -s ../websocket_example/static/ .
$ ./websocket_server

I will fix the cmake script in the next release, thanks!

Knutselsmurf1 commented 3 years ago

I originally started with source code for Ulfius 2.5.2. However I ran into exactly the same issue as #123 (and therefore I tried 2.6 as suggested).

Unfortunately this addition does not solve the segmentation fault: $ ln -s ../websocket_example/static/ .

The example programs are deployed in /usr/share/doc/libulfius-dev/examples. However I am not sure how and if I can use directly.

babelouest commented 3 years ago

That's right, I forgot about this old bug, therefore I suggest you use the example_programs from the source package 2.6.9.

The point of the link is because the program websocket_server expects a directory static that contains the static html, css and javascript files. In the end, you should have the executable websocket_server and a directory static in the same path and run websocket_server from this path.

The example programs are deployed in /usr/share/doc/libulfius-dev/examples. However I am not sure how and if I can use directly.

Not directly from /usr/share/doc/libulfius-dev/ but you can copy this folder into your working directory.

Knutselsmurf1 commented 3 years ago

Unfortunately it does not matter if I take the websocket_example from 2.6.9 or 2.6 instead 2.5.2. All results in Segmentation fault if I enter in a browser http://localhost:9275/static/index.html

I also tried to copy folder /static to my build directory instead using the link.

babelouest commented 3 years ago

There might be several reasons for this problem to happen so let's restart the process if you're ok.

Then you should see an output like this:

cd ../../src && make debug JANSSONFLAG=1
make[1]: Entering directory '/opt/ulfius/src'
Config file ../include/ulfius-cfg.h generated
JANSSON SUPPORT    DISABLED
CURL SUPPORT       ENABLED
GNUTLS SUPPORT     ENABLED
WEBSOCKET SUPPORT  ENABLED
YDER SUPPORT       ENABLED
FREERTOS SUPPORT   DISABLED
LWIP SUPPORT       DISABLED
gcc -c -pedantic -std=gnu99 -fPIC -Wall -Werror -Wextra -D_REENTRANT -I../include -DDEBUG -g -O0  ulfius.c
gcc -c -pedantic -std=gnu99 -fPIC -Wall -Werror -Wextra -D_REENTRANT -I../include -DDEBUG -g -O0  u_map.c
gcc -c -pedantic -std=gnu99 -fPIC -Wall -Werror -Wextra -D_REENTRANT -I../include -DDEBUG -g -O0  u_request.c
gcc -c -pedantic -std=gnu99 -fPIC -Wall -Werror -Wextra -D_REENTRANT -I../include -DDEBUG -g -O0  u_response.c
gcc -c -pedantic -std=gnu99 -fPIC -Wall -Werror -Wextra -D_REENTRANT -I../include -DDEBUG -g -O0  u_send_request.c
gcc -c -pedantic -std=gnu99 -fPIC -Wall -Werror -Wextra -D_REENTRANT -I../include -DDEBUG -g -O0  u_websocket.c
gcc -c -pedantic -std=gnu99 -fPIC -Wall -Werror -Wextra -D_REENTRANT -I../include -DDEBUG -g -O0  yuarel.c
gcc -shared -fPIC -Wl,-soname,libulfius.so -o libulfius.so.2.6.9 ulfius.o u_map.o u_request.o u_response.o u_send_request.o u_websocket.o yuarel.o -L/usr/local/lib -lc -lmicrohttpd -lorcania -lpthread  -lyder  -lcurl -lgnutls
ln -sf libulfius.so.2.6.9 libulfius.so
make[1]: Leaving directory '/media/donjon/workspace/angharad/ulfius/src'
gcc -c -Wall -I../../include -I../include -I../../example_callbacks/static_file   ../../example_callbacks/static_file/static_file_callback.c
gcc -c -Wall -I../../include -I../include -I../../example_callbacks/static_file   websocket_server.c
gcc -o websocket_server websocket_server.o static_file_callback.o -lc -lulfius -lorcania -L../../src -lyder
LD_LIBRARY_PATH=../../src: ./websocket_server 
2020-10-12T19:48:48Z - websocket_example INFO: Starting websocket_example
2020-10-12T19:48:48Z - websocket_example INFO: Start framework on port 9275 http mode

There, open http://localhost:9275/static/index.html and try again the websocket example.

If you still have the segfault, install the package valgrind and run make memcheck_server, try to crash the program again, if you still have the segfault, open the file valgrind_server.txt which is valgrind's log output. There you should see a message like Process terminated somewhere which might help us pointing out where the problem is.

Knutselsmurf1 commented 3 years ago

If I run from /example_programs/websocket_example/ then it works: $ make test_server

So I looked further and I know what I have done wrong:

The full steps to make it workable:

Issue solved, thank you.