Commit Graph

53 Commits

Author SHA1 Message Date
Thomas Haller
1ed3854402 checkpatch: discourage deprecated "(allow-none)" annotation 2023-03-29 11:46:48 +02:00
Thomas Haller
ad7d5887cd all: cleanup close() handling and clarify nm_close()/nm_close_with_error()
Cleanup the handling of close().

First of all, closing an invalid (non-negative) file descriptor (EBADF) is
always a serious bug. We want to catch that. Hence, we should use nm_close()
(or nm_close_with_error()) which asserts against such bugs. Don't ever use
close() directly, to get that additional assertion.

Also, our nm_close() handles EINTR internally and correctly. Recent
POSIX defines that on EINTR the close should be retried. On Linux,
that is never correct. After close() returns, the file descriptor is
always closed (or invalid). nm_close() gets this right, and pretends
that EINTR is a success (without retrying).

The majority of our file descriptors are sockets, etc. That means,
often an error from close isn't something that we want to handle. Adjust
nm_close() to return no error and preserve the caller's errno. That is
the appropriate reaction to error (ignoring it) in most of our cases.

And error from close may mean that there was an IO error (except EINTR
and EBADF). In a few cases, we may want to handle that. For those
cases we have nm_close_with_error().

TL;DR: use almost always nm_close(). Unless you want to handle the error
code, then use nm_close_with_error(). Never use close() directly.

There is much reading on the internet about handling errors of close and
in particular EINTR. See the following links:

https://lwn.net/Articles/576478/
https://askcodes.net/coding/what-to-do-if-a-posix-close-call-fails-
https://www.austingroupbugs.net/view.php?id=529
https://sourceware.org/bugzilla/show_bug.cgi?id=14627
https://news.ycombinator.com/item?id=3363819
https://peps.python.org/pep-0475/
2022-10-25 13:12:48 +02:00
Thomas Haller
c7bc4e0c67 checkpatch: suggest nm_memdup() instead of g_memdup() 2022-10-18 20:31:21 +02:00
Thomas Haller
6cbad14721 contrib: discourage g_type_class_add_private() via "checkpatch.pl"
Our GObject structs should be internal API. In which case, we should
embed the private data in the struct themselves (`_priv`) and use the
_NM_GET_PRIVATE() macro. The advantage is better debugability because
following G_TYPE_INSTANCE_GET_PRIVATE() in the debugger is very
cumbersome. Another (less relevant) advantage is better performance.

Thus, warn about uses of g_type_class_add_private() and
G_TYPE_INSTANCE_GET_PRIVATE().

Note that if the struct and is in a header file (which is usually only
necessary when subclassing the type), then the private data should be
an opaque pointer `_priv` instead, and we should use the _NM_GET_PRIVATE_PTR()
macro. In that case, the use of g_type_class_add_private() and
G_TYPE_INSTANCE_GET_PRIVATE() is correct and the warning is false. But
this is only a warning, for the unusual case where we have deep object
hierarchies.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1396
2022-10-03 17:23:29 +02:00
Thomas Haller
e6b9f6ecd0 contrib: discourage g_array_index() in "checkpatch.pl" 2022-09-15 12:39:08 +02:00
Thomas Haller
98c3ac1480 checkpatch.pl: discourage g_str_hash()/g_direct_hash() and g_direct_equal()
- instead of g_str_hash()/g_direct_hash(), use our own functions
  nm_str_hash()/nm_direct_hash(). Those use siphash24 with a random
  seed.

- don't pass g_direct_equal() to GHashTable. When omitting the equal
  function, it falls back to direct pointer comparison, which is likely
  faster. In any case, it's consistent to not use g_direct_hash()
  when using pointer equality.

- instead of g_int_hash()/g_int64_hash()/g_double_hash(), use
  our nm_pint_hash()/nm_pint64_hash()/nm_pdouble_hash(). The latter
  two don't exist yet.
  The reason is that we want to use siphash24.
  Yes, our name differs from glib's. Our naming seems to make sense
  to me however, because we also have nm_pstr_hash(), nm_pdirect_hash()
  and even nm_ppdirect_hash() for following the pointers. Naming is hard.

- instead of g_int_equal()/g_int64_equal()/g_double_equal() use
  our nm_pint_equal()/nm_pint64_equal()/nm_pdouble_equal(). The latter
  two don't exist yet. The reason is purely naming consistency since
  our hash variants follow the other name.
2022-08-31 10:59:22 +02:00
Thomas Haller
a628a35e80 contrib/checkpatch: try to warn about uninitialized GError variables
When we have a GError* variable on the stack, we usually want to pass
it on to function that can fail. In that case, the variable MUST be
initialized to NULL. This is an easy mistake to make.

Note that this check still can have lots of false positives, for
example, if you have a struct with an GError field. In that case, you
would need to ensure that the entire struct is initialized. Ignore the
warning then.

Also, the check misses if you declare multiple variables on one line.
But that is already discouraged by our style.
2022-03-09 23:14:37 +01:00
Thomas Haller
2b449694e5 checkpatch: complain about tabs in source file
There are very few places left where we would accept tabs in a source
file. Warn about that, even if it might cause some false positives.

I think this line was commented out due to a mistake.
2022-01-07 07:32:04 +01:00
Thomas Haller
3e6c18e9af checkpatch: suggest to use _nm_setting_property_define_direct_*() for setting properties
We have multiple ways to define properties (like, GVariant based
nm_setting_option_*() or GObject based properties). For the latter,
they nowadays should all be implemented via _nm_setting_property_define_direct_*()
API.
2021-11-04 20:25:19 +01:00
Lubomir Rintel
ae4412b2fc contrib/checkpatch: recognize git subtree merges
Make checkpatch.pl identify subtree merges in "git am"-formatted
patches and reconstruct the full path names based in the subtree root.

This fixes some spurious warnings for parts of the tree that use
different coding style from what we usually do.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/989
2021-10-12 15:13:44 +02:00
Thomas Haller
b9ff90c87d checkpatch: discourage use of g_clear_pointer()
We have nm_clear_pointer() instead, which does not cast the function
argument, and thus the compiler is better at checking the arguments.
2021-09-22 17:26:02 +02:00
Thomas Haller
3a39ce6a99 checkpatch: encourage g_snprintf() over snprintf()
The only reason is consistency. The majority of times we
do use g_snprintf(). As there are no strong reasons to
prefer one over the other, prefer the one that use use
most of the time.
2021-08-26 14:59:53 +02:00
Thomas Haller
a99ac7ccd8 glib-aux: add nm_g_idle_add()
g_idle_add() is discouraged, and the checkpatch.pl script warns
about it.

Sometimes there is a legitimate use of it, when you want to always
schedule an idle action (without intent to cancel or track it). That
makes more sense for g_idle_add() than it does for g_timeout_add(),
because a timeout really should be tracked and cancelled if necessary.

Add a wrapper to rename the legitimate uses. This way, we can avoid the
checkpatch.pl warnings, and can grep for the remaining illegitimate uses.
2021-07-26 15:30:04 +02:00
Thomas Haller
5388542fc0 checkpatch: discourage use of API that uses numeric source IDs
The numeric source IDs exist from a time before 2000, when there
was only one "GMainContext" singleton instance. Nowadays, the source
ID is only relative to one GMainContext, and you'd have to track
that association yourself. Als, g_source_remove() requires an additional
hash lookup, when you could simply track the GSource instance from the
start.

This API should not be used anymore. Operate on GSouce instances
direclty and use API like

  nm_clear_g_source_inst()
  nm_g_idle_add_source()
  nm_g_idle_souce_new()
  nm_g_source_attach()
  g_source_attach
  g_source_destroy
  g_source_unref
  etc.

Note that if you don't care about to ever remove a source again, like
scheduling an idle action that should not be cancelled, then

  g_idle_add(callback, user_data);

is fine. It is only problematic to do something with those numeric IDs.
checkpatch.pl would also flag those uses, but these are just warnings
and in the few cases where such a warning is emitted wrongly, it's find
to ignore them.
2021-06-28 13:31:33 +02:00
Thomas Haller
23a200d19e checkpatch: warn about uses of strcmp()/g_strcmp0()
Using strcmp()/g_strcmp0() for checking for string equality is hard
to read. We should prefer our streq variants -- unless, you really
mean cmp.
2021-04-26 09:53:11 +02:00
Thomas Haller
344fd187cd checkpatch: update check for SPDX license identifier tag 2021-01-05 09:46:24 +01:00
Thomas Haller
977ea352a0 all: update deprecated SPDX license identifiers
These SPDX license identifiers are deprecated ([1]). Update them.

[1] https://spdx.org/licenses/

  sed \
     -e '1 s%^/\* SPDX-License-Identifier: \(GPL-2.0\|LGPL-2.1\)+ \*/$%/* SPDX-License-Identifier: \1-or-later */%' \
     -e '1,2 s%^\(--\|#\|//\) SPDX-License-Identifier: \(GPL-2.0\|LGPL-2.1\)+$%\1 SPDX-License-Identifier: \2-or-later%' \
     -i \
     $(git grep -l SPDX-License-Identifier -- \
         ':(exclude)shared/c-*/' \
         ':(exclude)shared/n-*/' \
         ':(exclude)shared/systemd/src' \
         ':(exclude)src/systemd/src')
2021-01-05 09:46:21 +01:00
Thomas Haller
6677480a2d checkpatch: ignore warning about g_assert*() also for files like "shared/nm-utils/nm-test-utils.h" 2020-10-22 09:29:28 +02:00
Thomas Haller
96a3d664cb contrib/checkpatch: complain about patch format with "Reverts:" tag 2020-10-09 15:55:48 +02:00
Thomas Haller
419be5d0e7 checkpatch.pl: adjust checking for indentation/tabs
Our new format gets enforced by clang-format, and we now only use
four space indentation, instead of tabs.

Adjust the checkpatch script to account for that.

Also, now there are probably no cases left where we want to see any
tabs in our sources. Complain about any tabs we find.
2020-09-28 15:08:41 +02:00
Thomas Haller
2fa4827eb9 checkpatch: catch "gs_free GError *" declations
(cherry picked from commit ec0adbfaf0)
2019-12-16 17:45:16 +01:00
Thomas Haller
3b69f02164 all: unify format of our Copyright source code comments
```bash

readarray -d '' FILES < <(
  git ls-files -z \
    ':(exclude)po' \
    ':(exclude)shared/c-rbtree' \
    ':(exclude)shared/c-list' \
    ':(exclude)shared/c-siphash' \
    ':(exclude)shared/c-stdaux' \
    ':(exclude)shared/n-acd' \
    ':(exclude)shared/n-dhcp4' \
    ':(exclude)src/systemd/src' \
    ':(exclude)shared/systemd/src' \
    ':(exclude)m4' \
    ':(exclude)COPYING*'
  )

sed \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[-–] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C1pyright#\5 - \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[,] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C2pyright#\5, \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C3pyright#\5#\7/' \
  -e 's/^Copyright \(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/C4pyright#\1#\3/' \
  -i \
  "${FILES[@]}"

echo ">>> untouched Copyright lines"
git grep Copyright "${FILES[@]}"

echo ">>> Copyright lines with unusual extra"
git grep '\<C[0-9]pyright#' "${FILES[@]}" | grep -i reserved

sed \
  -e 's/\<C[0-9]pyright#\([^#]*\)#\(.*\)$/Copyright (C) \1 \2/' \
  -i \
  "${FILES[@]}"

```

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/298
2019-10-02 17:03:52 +02:00
Thomas Haller
abff46cacf all: manually drop code comments with file description 2019-10-01 07:50:52 +02:00
Lubomir Rintel
ae6668ae85 contrib/checkpatch: fix the systemd code path 2019-09-10 11:20:03 +02:00
Lubomir Rintel
1b0ee8756b contrib/checkpatch: check for SPDX tags 2019-09-10 11:20:03 +02:00
Lubomir Rintel
24028a2246 all: SPDX header conversion
$ find * -type f |xargs perl contrib/scripts/spdx.pl
  $ git rm contrib/scripts/spdx.pl
2019-09-10 11:19:56 +02:00
Tom Gundersen
6adade6f21 dhcp: add nettools dhcp4 client
This is inspired by the existing systemd integration, with a few differences:

* This parses the WPAD option, which systemd requested, but did not use.
* We hook into the DAD handling, only making use of the configured address
  once DAD has completed successfully, and declining the lease if it fails.

There are still many areas of possible improvement. In particular, we need
to ensure the parsing of all options are compliant, as n-dhcp4 treats all
options as opaque, unlike sd-dhcp4. We probably also need to look at how
to handle failures and retries (in particular if we decline a lease).

We need to query the current MTU at client startu, as well as the hardware
broadcast address. Both these are provided by the kernel over netlink, so
it should simply be a matter of hooking that up with NM's netlink layer.

Contribution under LGPL2.0+, in addition to stated licenses.
2019-07-05 11:04:32 +02:00
Lubomir Rintel
a5dd31afeb contrib/checkpatch: allow empty lines within continuations
This chunk from nm-device.c is, in fact, okay:

               |<-tab->nm_assert (   !new_config
               |<-tab->           || (   new_config
               |<-tab->               && ({
               |<-tab->                    int ip_ifindex = ...
 empty line -> |
               |<-tab->                    (   ip_ifindex > 0
               |<-tab->                     && ip_ifindex == ...
               |<-tab->                  })));
2019-06-25 20:27:39 +02:00
Lubomir Rintel
da312e6220 contrib/checkpatch: be a bit stricter about whitespace
In continations (that use spaces for alignment), don't allow the number
of leading tabs to change. Previously only removal of tabs was
disallowed, but addition doesn't make sense either, as only spaces
should be used for further alignemnt.

This catches situations like this:

  |<-tab->all_work_and_no_play (makes,
  |<-tab->                      jack,
  |<-tab-><-tab->               a dull boy);
2019-06-25 20:27:39 +02:00
Lubomir Rintel
5ff19ea8d2 contrib/checkpatch: discourage g_assert*() 2019-06-25 20:27:39 +02:00
Lubomir Rintel
f3f8e21bd3 contrib/checkpatch: properly determine the commit id boundary
It doesn't have to be at the end of line, there may be more words
following.

Fixes: d66a1ace23 ('contrib/checkpatch: avoid command injection in checkpatch.pl script')
2019-05-20 16:31:52 +02:00
Thomas Haller
d66a1ace23 contrib/checkpatch: avoid command injection in checkpatch.pl script
The capture variables, $1, etc, are not valid unless the match
succeeded, and they're not cleared, either.

    $ git checkout -B C origin/master && \
        echo XXXXX > f.txt && \
        git add f.txt && \
        git commit -m 'this commit does something()'
    Branch 'C' set up to track remote branch 'master' from 'origin'.
    Reset branch 'C'
    Your branch is up to date with 'origin/master'.
    sh: -c: line 0: syntax error near unexpected token `('
    sh: -c: line 0: `git log --abbrev=12 --pretty=format:"%h ('%s')" -1 does something() 2>/dev/null'

    >>> VALIDATE "a169a98e14 this commit does something()"
    (commit message):4: Commit 'does something()' does not seem to exist:
    > Subject: [PATCH] this commit does something()

    (commit message):4: Refer to the commit id properly: :
    > Subject: [PATCH] this commit does something()

    The patch does not validate.
2019-03-18 11:57:04 +01:00
Lubomir Rintel
f2a5b6336d contrib/checkpatch: check that we refer to commits properly
(cherry picked from commit f8578ddc2e)
2019-03-07 22:29:41 +01:00
Thomas Haller
e498f594bb checkpatch: warn if there is a file "TODO.txt"
This allows us to add a file "TODO.txt" in the top level directory.
This file is not intended to be merged to master, but keep track of
stuff that is still to do before merging a branch.

Let checkpatch.pl warn about the presence of such a file.
2018-12-30 15:17:11 +01:00
Thomas Haller
fc052494d1 checkpatch: warn about suspicious gtk-doc annotations
It's

    (allow-none):

and

    (transfer none):

That's confusing enough. Add a check.
2018-12-30 15:17:11 +01:00
Thomas Haller
f8fed7dd52 checkpatch: complain about XXX markers in code
We have a few source code tags like "TODO" and "FIXME".
"XXX" is not intended to be merged, it is for marking
places in code while still working on it.
2018-10-25 11:20:10 +02:00
Thomas Haller
369446eae6 checkpatch: add "contrib/scripts/checkpatch-feature-branch.sh" script
This takes current HEAD branch, and finds all the commits what
are not on master or one of the nm-1-* branches, and runs
checkpatch.pl on each.

The use is to run checkpatch.pl on all patches of a feature
branch.
2018-10-22 13:19:15 +02:00
Thomas Haller
8221f824f3 checkpatch: complain about Emacs file variables in source code 2018-10-18 12:16:55 +02:00
Lubomir Rintel
0d3c6072f5 contrib/checkpatch: remove the first character off a diff
Otherwise the leading whitespace checks won't work on patches.
2018-10-07 15:46:02 +02:00
Lubomir Rintel
587f006690 contrib/checkpatch: check some more whitespace trouble 2018-10-07 15:46:02 +02:00
Lubomir Rintel
1d57aefa41 contrib/checkpatch: correctly separate indentation across hunks 2018-10-07 15:46:02 +02:00
Lubomir Rintel
66ddc92135 checkpatch: detect some whitespace errors
Vim's trademark.
2018-09-24 13:21:12 +02:00
Thomas Haller
cf02b9c5df checkpatch.pl: complain about space in elvis operator ?: 2018-08-09 17:07:23 +02:00
Javier Arteaga
6c3174f6e0 checkpatch: fix perldoc heading
The script does not actually emulate a serial modem (yet).

https://github.com/NetworkManager/NetworkManager/pull/165
2018-07-12 07:40:02 +02:00
Lubomir Rintel
7e98b4cad2 checkpatch: skip foreign code 2018-07-11 12:02:06 +02:00
Lubomir Rintel
2b152a69c4 checkpatch: add a licensing hint 2018-07-11 12:02:06 +02:00
Lubomir Rintel
26910ebdd7 checkpatch: reset line counter on next file 2018-07-11 12:02:06 +02:00
Thomas Haller
24082ad09e checkpatch: check against using "unsigned int" and "$INT_TYPE unsigned|signed"
Don't use the integer type before signed/unsigned, but the
other way around. That is,

    unsigned long var;

instead of

    long unsigned var;

Also, just use "unsigned" instead of "unsigned int".
2018-07-11 12:02:06 +02:00
Thomas Haller
a9d81031f4 checkpatch: skip over generated files from glib-mkenums 2018-07-11 12:02:06 +02:00
Thomas Haller
2d28d5d5d4 checkpatch: warn about non-leading tabs
Tabs are not only wrong after a space, they are always
wrong if they don't appear at the beginning of a line.
That would happen usually, when trying to align multiple
lines like

  enum {
      VALUE1		= 1;
      OTHER_VALUE	= 2;
  };

When doing that, the alignment will only be correct, if the
reader later uses the same tab-width. Note that in NetworkManager
we recommend the tab-width to be 4 characters, but with our "smart
tab" indentation style, it wouldn't actually matter and the reader
is free to choose any other tab-width -- as long as we don't use
non-leading tabs.

Don't allow non-leading tabs.
2018-07-11 12:02:06 +02:00