Commit Graph

34 Commits

Author SHA1 Message Date
Lucas De Marchi 8ab15eceaf util: Add exponential backoff sleep
Add simple functions to put the current thread to sleep using
exponential backoff to split the interval in smaller pieces.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2022-06-26 23:23:46 -07:00
Lucas De Marchi ba105fafed util: Add msec variants for time-related functions
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2022-06-26 23:23:46 -07:00
Lucas De Marchi 5622f1dae1 util: Add time-related functions from testsuite
This will be useful in future not only to testsuite, but also to tools
and library.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
2022-06-26 23:23:46 -07:00
Lucas De Marchi e4a72e4754 shared: fix UNIQ definition
We need a macro indirection for UNIQ to work. Otherwise it won't be
unique at all since it will just append "UNIQ" to the name:

In file included from testsuite/test-init.c:30:

testsuite/testsuite.h:142:27: error: redefinition of ‘stest_load_resourcesUNIQ’
2020-12-27 16:40:08 -08:00
Torge Matthies 3821e1971e add Zstandard compression support
I changed the style of the hackargs variable in autogen.sh to multiline
because said line was becoming a bit long with the new --with-zstd arg
added.

A previous version of this patch has been running on my two Arch Linux
installations (with an accompanying mkinitcpio patch) for several months
over many kernel updates without any issues.
Any additional testing and/or patch review would of course be appreciated.

Signed-off-by: Torge Matthies <openglfreak@googlemail.com>
2020-09-10 21:55:01 -07:00
Thomas Petazzoni ecab65b35c shared/util.c: assert_cc() can only be used inside functions
shared/macro.h has two versions of assert_cc, one that uses gcc
_Static_assert(), which requires recent enough gcc versions, and one
that uses a fake array to trigger a build error. The latter can only
work inside functions, so assert_cc() should only be used inside
functions.

Fixes the following build failure when building kmod with old gcc
versions such as gcc 4.3.x:

shared/util.c:52: error: expected identifier or '(' before 'do'
shared/util.c:52: error: expected identifier or '(' before 'while'

Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
2017-06-05 10:07:00 -07:00
Yauheni Kaliuta b34819bc0e shared: make scratchbuf_str static
It fixes linking problem

tools/depmod.o: In function `output_symbols_bin':
depmod.c:(.text.output_symbols_bin+0x135): undefined reference to `scratchbuf_str'

for -O0 build, where gcc doesn't actually inline it.

Signed-off-by: Yauheni Kaliuta <yauheni.kaliuta@redhat.com>
2016-11-10 22:14:27 -02:00
Lucas De Marchi 780a4e97e2 Add scratchbuf implementation
This should fill the requirements for "we need to loop over a lot of
strings that usually are small enough to remain on stack, but we want to
protect ourselves against huge strings not fitting in the static
buffer we estimated as sufficient"
2016-08-15 10:26:42 -03:00
Lucas De Marchi a6ede6c7ad util: fix warning of equal values on logical OR
shared/util.c: In function ‘read_str_safe’:
shared/util.c:211:24: warning: logical ‘or’ of equal expressions [-Wlogical-op]
    if (errno == EAGAIN || errno == EWOULDBLOCK ||
                        ^~
shared/util.c: In function ‘write_str_safe’:
shared/util.c:237:24: warning: logical ‘or’ of equal expressions [-Wlogical-op]
    if (errno == EAGAIN || errno == EWOULDBLOCK ||
                        ^~

This is because EAGAIN and EWOULDBLOCK have the same value. Prefer
EAGAIN, but add a static assert to catch if it's not the same in another
architecture.
2016-08-10 12:45:36 -03:00
Lucas De Marchi a8c73b8609 util: use UINT64_MAX
Since we are checking the size of long and long long, use UINT64_MAX in
the fallback to be sure thre right size is being used.
2015-03-23 23:47:23 -03:00
Lucas De Marchi 16a62c7ab3 shared: avoid checking for __STDC_VERSION__
Since we pass -std=gnu99 to the compiler __STDC_VERSION__ is defined to
199901. Even in this case we can use _Noreturn. Delegate the testing to
the configure phase with a compile test.

In future we want to stop using AC_PROG_CC_99 and start using the new
AC_PROG_CC from autoconf 2.70, which tries to choose the most recent
standard.
2015-02-26 13:15:14 -03:00
Lucas De Marchi aac5f4514f shared: add helper function to add and check for overflow
Use _builtin_uaddll_overflow/_builtin_uaddl_overflow when available,
abstracting the type to use it with uint64_t.

Otherwise fallback to the implementation as added in 67466f2 ("Prevent
offset + size overflow.").

This also adds the tests for this new helper in the testsuite.
2015-02-10 10:43:44 -02:00
Caio Marcelo de Oliveira Filho 88f6ffe449 util: remove dead code in freadline_wrapped
Each loop iteration starts with 'i < size'. When i is changed
the invariant is kept by checking if 'i == size' and growing the
buffer.
2015-01-21 10:18:44 -02:00
Lucas De Marchi bf2fbabb9e shared: add STRINGFY macro 2015-01-14 14:32:09 -02:00
Lucas De Marchi f357866d97 Fix includes after change to build-sys
Make the includes be libkmod/libkmod.h for code outside of library. This
fixes the broken build after 1315123 ('build-sys: Don't add libkmod
subdirectory to include path').
2015-01-02 12:41:01 -02:00
Lucas De Marchi dea2dfee9b Remove FSF mailing address
It has changed in the past, and these days, anyone can get a copy of the
LGPL via the web rather than by post.

Like 657a122 (Remove FSF mailing address) in libabc by Josh Tripplet,
but let the FSF website in which the license can be found.
2014-12-25 23:41:34 -02:00
Natanael Copa efb5bfaca9 util: fix build on systems missing strndupa
we need include shared/missing.h to be able to compile on sysmtes that
lacks strndupa.
2014-11-17 13:00:19 -02:00
Lucas De Marchi e62d8c725b strbuf: make strbuf_pushchars() a little less dumb
Do not push one char at a time. We have strlen() and memcpy() to rely
on.
2014-10-16 21:39:33 -03:00
Lucas De Marchi 8863154e44 strbuf: do not calculate next step in size on all calls
We only need to check if the new size is less or equal than the current
size. We don't really need to calculate the next step.
2014-10-16 21:22:32 -03:00
Lucas De Marchi 405b8927fe strbuf: reduce default buffer size
Using 2048 as buffer sizer for strbuf is a bit exaggerated. strbuf is
used much more when we are not using mmapped indexes, but it's used for
mmapped when for example searching for an alias. A quick and dirty hack
to output the size of our strbufs is to print buf->used inside
strbuf_str(). Doing this and creating some statistics with:

while read xxx alias xxx; do
    tools/modprobe -R "$alias" > /dev/null;
done < /lib/modules/$(uname -r)/modules.alias 2>&1 | \
	     Rscript -e 'summary (as.numeric (readLines ("stdin")))'

   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
   1.00   29.00   31.00   31.55   38.00   92.00      26

So, reduce the step to 128, which is still greater than the maximum in
these cases. In the worst case this can only create a few calls to
realloc(), while keeping the memory footprint low for the common cases.
2014-10-16 21:02:13 -03:00
Lucas De Marchi 15a7ae30b3 shared: rename prefixes of strbuf functions
Use strbuf_ prefix instead of buf_.
2014-10-11 13:25:51 -03:00
Lucas De Marchi b4d1f44af1 Move strbuf implementation to shared/
Just move the strbuf-like implementation to shared/. No renames were
made yet to avoid cluttering the diff. It will come in a separate patch.
2014-10-11 13:09:42 -03:00
Lucas De Marchi 4328982058 testsuite: remove now unused array of tests
Remove the arrays and let each test with a guaranteed unique name.
2014-10-09 14:29:04 -03:00
Lucas De Marchi 9a437531af Change parameter from int to char
It's a char not an int.
2014-10-09 11:03:29 -03:00
Lucas De Marchi 52c9c99056 Log error on failed underscores(), moving it to shared/
Move underscores() to shared/. It's the same as alias_normalize(), but
it rather operates in place, with the same string being passed.

The difference now that it's in shared/ is that it's a non-logging
function.

This makes us a little bit more verbose: we don't accept partially
correct module and aliases names in kcmdline and in configuration files.
We log an error instead.
2014-10-09 11:00:21 -03:00
Lucas De Marchi f4e8c16291 Move remaining functions from libkmod-util to shared 2014-10-09 01:26:39 -03:00
Lucas De Marchi 2b0104fe3c Move alias_normalize() to shared 2014-10-09 01:26:39 -03:00
Lucas De Marchi aafd38359a Rename getline_wrapped() to freadline_wrapped() 2014-10-03 03:25:06 -03:00
Lucas De Marchi c2e4286bb9 Reorder and reorganize header files
Let the includes in the following order:

< system headers >
< libkmod >
< tool >
< local headers >
2014-10-03 01:43:15 -03:00
Lucas De Marchi 0db718edcf Move hash implementation to shared directory 2014-10-03 00:40:11 -03:00
Lucas De Marchi 74d1df6682 Move array implementation to shared directory 2014-10-03 00:33:25 -03:00
Lucas De Marchi 96573a0220 Move generic util functions to shared directory 2014-10-03 00:33:25 -03:00
Lucas De Marchi 8b7189bc25 Move missing.h to shared directory 2014-10-03 00:31:10 -03:00
Lucas De Marchi 576dd4393d Move macro.h to shared directory
It's not really related to libkmod, so move it to a directory in which
we keep common stuff.
2014-10-02 22:03:19 -03:00