Commit Graph

17 Commits

Author SHA1 Message Date
Xavier Del Campo Romero 91ff743df9
server.c: Fix wrong priority for do_exit
Under some specific circumstances, poll(2) would return a positive
integer, but do_exit might had been previously set. This caused libweb
to ignore SIGTERM, with the potential risk for an endless loop.
2024-01-20 01:19:57 +01:00
xavi 8f1ad3124e Merge pull request 'Fix double-free on failed `server_client_close`' (#2) from midokura-xavi/libweb:fix-double-free into master
Reviewed-on: #2
2023-11-20 16:27:28 +01:00
Xavier Del Campo 59a67a7541
Fix double-free on failed server_client_close
Even if server_client_close fails, it is needed for client_free to
remove the dangling reference from h->clients.
2023-11-20 16:25:58 +01:00
Xavier Del Campo 98f5f52461
Split handler_loop from handler_listen
Some applications might set up a struct handler object to listen on any
port i.e., 0, but still need a way to determine which port number was
eventually selected by the implementation.

Therefore, handler_listen has been reduced to the server initialization
bit, whereas the main loop has been split into its own function, namely
handler_loop.

Because of these changes, it no longer made sense for libweb to write
the selected port to standard output, as this is something now
applications can do on their own.
2023-11-20 16:06:19 +01:00
Xavier Del Campo Romero 0222b75e85
Rename project from slweb to libweb
It was found out there was another project of the same name around
(https://git.sr.ht/~strahinja/slweb/), also related to website
generation.

In order to avoid confusion, a new name has been chosen for this
project. Surprisingly, libweb was not in use by any distributions
(according to https://repology.org and AUR index), and it should
reflect well the intention behind this project i.e., being a library
to build web-related stuff.
2023-10-11 00:07:13 +02:00
Xavier Del Campo Romero 832e198f8c
server.c: Replace sequential calls with loop 2023-10-10 23:16:11 +02:00
Xavier Del Campo Romero 8b39bc5db9
server.c: Fix build on FreeBSD 2023-09-26 22:58:11 +02:00
Xavier Del Campo Romero 8c3ba33ced
Move header files to subdirectory
Since slweb is meant as a library, it is advisable to keep public header
files under their own directory in order to avoid name clashing i.e.,

 #include "something.h"

Now becomes:

 #include "slweb/something.h"
2023-07-21 01:28:38 +02:00
Xavier Del Campo Romero 01cebe5917
server.c: Fix typo 2023-07-20 23:52:55 +02:00
Xavier Del Campo Romero a527164cb5
server.c: Fix undefined behaviour on >1 clients
server.c kept an array of all of its active clients, calling realloc(3)
everytime its size had to be modified. However, reallocating this array
had the undesired consequence of moving other active clients to other
memory locations.

Potentially, this would result in dangling pointers from other
components that also kept pointers to struct server_client instances
e.g.: handler.c.

For this reason, the array-based approach has been completely dropped,
in favour of a doubly-linked list.
2023-07-20 23:52:55 +02:00
Xavier Del Campo Romero f75ff13b31
Avoid crashing on SIGPIPE
Under some circumstances, clients could cause SIGPIPE to slcl. Since
this signal was not handled by server.c (i.e., via sigaction(3)), slcl
would crash without any error messages printed to stderr.

In such situation, SIGPIPE should not be usually considered a fatal
error, so it is preferrable to close the connection and keep working.
2023-07-20 23:52:54 +02:00
Xavier Del Campo Romero 2e8485d687
Replace select(2) with poll(2)
select(2) has a number of well-known issues (e.g.: FD_SETSIZE limiting
the maximum amount of file descriptors to watch) that are mostly solved
by poll(2) and thus can be used as a drop-in replacement.
2023-07-20 23:52:53 +02:00
Xavier Del Campo Romero af16aa6702
Define _POSIX_C_SOURCE
This allows using the default compiler defined by make(1) (i.e.,
c99(1)), thus improving POSIX compatibility.
2023-07-20 23:52:53 +02:00
Xavier Del Campo Romero fbaa7b9bcf
server.c: Fix wrong size for memcpy(3) 2023-07-20 23:52:52 +02:00
Xavier Del Campo Romero 5b0faff8e6
server.c: Minor const-correctness improvement 2023-07-20 23:52:52 +02:00
Xavier Del Campo Romero 67ffb772b7
Fix memory leak on failed realloc(3)
According to C99 §7.20.3.4:

If memory for the new object cannot be allocated, the old object is not
deallocated and its value is unchanged.

Therefore, a temporary pointer must be used to ensure the original
object can still be deallocated should realloc(3) return a null pointer.
2023-07-20 23:52:51 +02:00
Xavier Del Campo Romero 2968c5f67d
Initial commit 2023-07-20 23:52:47 +02:00