195 Commits

Author SHA1 Message Date
Susant Sahani
276897c4df daemon: Use modern compound literal for SIGPIPE ignore
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-21 18:58:00 +00:00
Susant Sahani
4f19d68565 daemon: Use designated initializer for struct sockaddr_un
Signed-off-by: Susant Sahani <ssahani@redhat.com>
2025-11-21 14:02:14 +00:00
Richard W.M. Jones
72cfaff5c5 Update copyright dates for 2025
Automated using this command:

perl -pi.bak -e 's/(20[012][0-9])-20[12][01234]/$1-2025/g' `git ls-files`
2025-02-16 17:00:46 +00:00
Richard W.M. Jones
e55eac12d4 docs: Update libguestfs email address in a couple more places
Reported-by: Julian Euler
2024-09-12 12:45:54 +01:00
Richard W.M. Jones
e2c7bddf10 Update copyright dates for 2023
Run this command across the source:

  perl -pi.bak -e 's/(20[012][0-9])-20[12][012]/$1-2023/g' `git ls-files`

and remove changes to po{,-docs}/*.po{,t} (these will be regenerated
later when we run 'make dist').
2023-02-07 10:50:48 +00:00
Richard W.M. Jones
001683e885 appliance: Remove LD_PRELOAD=libSegFault.so
This feature was removed in glibc 2.35:
https://savannah.gnu.org/forum/forum.php?forum_id=10111
2022-10-24 10:41:09 +01:00
Richard W.M. Jones
0b3c6cc0c0 daemon: Remove remaining uses of custom printf %Q and %R
We have traditionally used custom printf formatters %Q and %R, where
%Q replaces the argument with a shell-quoted string, and %R replaces
the argument with a sysroot-prefixed shell-quoted string.  They are
actually pretty useful, but unfortunately only supported by glibc.

We only used them in about a dozen places in the daemon (much code
having been replaced by OCaml which does not need them).

In every remaining case we were constructing a command using code like
this:

  asprintf_nowarn (&cmd,
         "cd %Q && find -print0 | %s -0 -o -H %s --quiet", ...);

We can replace this with:

  char *cmd;
  size_t cmd_size;
  fp = open_memstream (&cmd, &cmd_size);
  fprintf (fp, "cd ");
  shell_quote (dir, fp);
  fprintf (fp, " && find -print0 | %s -0 -o -H %s --quiet", ...);
  fclose (fp);
2022-08-16 10:39:01 +01:00
Richard W.M. Jones
0f54df53d2 build: Remove gnulib.
As part of our efforts to clean up and simplify libguestfs, removing
gnulib deletes a large dependency that we mostly no longer use and
causes problems for new users trying to build the library from source.

A few modules from gnulib are still used (under a compatible license)
and these are copied into gnulib/lib/
2021-04-08 11:36:40 +01:00
Richard W.M. Jones
0e17236d7d Update copyright dates to 2020. 2020-03-06 19:32:32 +00:00
Richard W.M. Jones
bca9b94fc5 daemon: Translate device names if Linux device ordering is unstable (RHBZ#1804207).
Linux from around 5.6 now enumerates individual disks in any order
(whereas previously it enumerated only drivers in parallel).  This
means that /dev/sdX ordering is no longer stable - in particular we
cannot be sure that /dev/sda inside the guest is the first disk that
was attached to the appliance, /dev/sdb the second disk and so on.

However we can still use SCSI PCI device numbering as found in
/dev/disk/by-path.  Use this to translate device names in and out of
the appliance.

Thanks: Vitaly Kuznetsov, Paolo Bonzini, Dan Berrangé.
2020-03-06 19:03:03 +00:00
Richard W.M. Jones
05d4fcb64d Update copyright dates for 2019.
This command run over the source:

perl -pi.bak -e 's/(20[01][0-9])-2018/$1-2019/g' `git ls-files`
2019-01-08 11:58:30 +00:00
Richard W.M. Jones
dd162d2cd5 daemon: Move lvmetad to early in the appliance boot process.
When the daemon starts up it creates a fresh (empty) LVM configuration
and starts up lvmetad (which depends on the LVM configuration).

However this appears to cause problems: Some types of PV seem to
require lvmetad and don't work without it
(https://bugzilla.redhat.com/show_bug.cgi?id=1581810).  If we don't
start lvmetad earlier, the device nodes are not created.

Therefore move the whole initialization step into appliance/init.

Two further changes had to be made:

Now we are using lvmetad all the time, using vgchange is incorrect.
With lvmetad activated early we must use ‘pvscan --cache --activate ay’
to scan all disks for PVs and activate any VGs on them (although the
documentation is complex, confusing and contradictory so I'm not
completely sure about this).

The ‘lvm_system_dir’ local variable in ‘daemon/lvm-filter.c’
previously contained the path of the directory above $LVM_SYSTEM_DIR
(eg. $LVM_SYSTEM_DIR = "/etc/lvm", lvm_system_dir = "/etc").  As this
was highly confusing, I have changed it so the local variable and the
environment variable have identical contents.  This involved removing
the ‘lvm/’ component from a couple of paths since it is now included
in the local variable.
2018-05-29 20:38:40 +01:00
Richard W.M. Jones
212762c593 Update copyright dates for 2018.
Run the following command over the source:

  perl -pi.bak -e 's/(20[01][0-9])-2017/$1-2018/g' `git ls-files`
2018-01-04 15:30:10 +00:00
Richard W.M. Jones
403692bdac daemon: Move utility functions to a separate file.
This allows us to share certain utility functions with OCaml code.
2017-08-08 16:23:42 +01:00
Richard W.M. Jones
bedb6c1322 daemon: Reimplement several devsparts APIs in OCaml.
The reimplemented APIs are:

* list_devices
* list_partitions
* part_to_dev
* part_to_partnum
* is_whole_device
2017-07-27 22:31:22 +01:00
Richard W.M. Jones
d5b6f1df5f daemon: Allow parts of the daemon and APIs to be written in OCaml.
This change allows parts of the daemon to be written in the OCaml
programming language.  I am using the ‘Main Program in C’ method along
with ‘-output-obj’ to create an object file from the OCaml code /
runtime, as described here:
https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html

Furthermore, change the generator to allow individual APIs to be
implemented in OCaml.  This is picked by setting:

  impl = OCaml <ocaml_function>;

The generator creates ‘do_function’ (the same one you would have to
write by hand in C), with the function calling the named
‘ocaml_function’ and dealing with marshalling/unmarshalling the OCaml
parameters.
2017-07-27 22:31:22 +01:00
Richard W.M. Jones
10cf01419a daemon: Reimplement handling of lvm.conf and filters.
LVM is fine with a completely empty configuration file (meaning "all
defaults"), so start with one instead of copying the system
configuration file.

Also this means we can very easily implement lvm_set_filter
functionality without using Augeas, since we no longer have to worry
about existing filters being present.

Thanks: Alasdair Kergon, Zdenek Kabelac.
2017-07-27 22:31:22 +01:00
Richard W.M. Jones
381c8b68c4 daemon: Remove GUESTFSD_EXT_CMD.
GUESTFSD_EXT_CMD was used by OpenSUSE to track which external commands
are run by the daemon and package those commands into the appliance.

It is no longer used by recent SUSE builds, so remove it.

Thanks: Pino Toscano, Olaf Hering.
2017-07-27 17:31:41 +01:00
Richard W.M. Jones
707f5bcfe0 daemon: Link guestfsd with libutils.
After the previous refactoring, we are able to link the daemon to
common/utils, and also remove some of the "duplicate" functions that
the daemon carried ("duplicate" in quotes because they were often not
exact duplicates).

Also this removes the duplicate reimplementation of (most) cleanup
functions in the daemon, since those are provided by libutils now.

It also allows us in future (but not in this commit) to move utility
functions from the daemon into libutils.
2017-07-10 17:01:59 +01:00
Richard W.M. Jones
8a545c0b1e daemon: Move device_name_translation function to its own file.
Just code motion.

(cherry picked from commit c75d873568e95fb7845c6e3913347c4bf267f4e9)
2017-05-08 11:14:45 +01:00
Richard W.M. Jones
9013a93d0e lib: Remove internal details of device name translation from API docs.
Remove much of the text detailing how device name translation
happened.  Since we removed support for virtio-blk
(commit 9e0294f88f) and deprecated the
‘iface’ parameter, only /dev/sdX device names should be visible
through the public APIs, both in parameters and in return values from
calls like guestfs_list_devices and guestfs_list_partitions.

Note the above is in fact not true for the UML backend, but UML is
broken in the kernel and in any case this will be fixed later.

(cherry picked from commit 2727e589db216bf0731385966889a4f66dbfe225)
2017-05-08 11:14:45 +01:00
Pavel Butsykin
684aa26115 daemon: add udev_settle_file to is_root_device
It fixes a fail with ENOENT when device is not yet present after system boot.

Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>
2017-04-14 11:08:07 +01:00
Pavel Butsykin
93664d38d2 daemon: run 'udevadm settle' with --exit-if-exists option
Add udev_settle_file() to run 'udevadm settle' with --exit-if-exists option. It
will slightly reduce the waiting-time for pending events if we need to wait
for events related to a particular device/file.

Signed-off-by: Pavel Butsykin <pbutsykin@virtuozzo.com>

RWMJ:
 - Use local variable for MAX_ARGS.
 - Use commandv instead of commandrv, fix checking of return code.
2017-04-14 11:07:58 +01:00
Pino Toscano
35d97daa5a daemon: move make_exclude_from_file as common helper
It will be useful also for APIs different than tar-out, so move it to
guestfsd.c, and add it a parameter to specify the function name that
invoked it.

This is mostly code motion.
2017-02-15 14:44:12 +01:00
Pino Toscano
55bf7de97c Update copyright dates for 2017
Run the following command over the source:

  perl -pi.bak -e 's/(20[01][0-9])-2016/$1-2017/g' `git ls-files`

(Thanks Rich for the perl snippet, as used in past years.)
2017-01-03 16:48:21 +01:00
Pino Toscano
12c605f8fa daemon: drop program_name definition
It was previously needed by the 'error' module of gnulib; recent
versions are able to get the name of the current program on their own.
2016-09-08 09:57:15 +02:00
Richard W.M. Jones
20f190617a daemon: Split up very large daemon/stubs.c file. 2016-09-02 23:14:08 +01:00
Pino Toscano
2c4f154b91 daemon: lvm-filter: start lvmetad better
Currently lvmetad is started in init, and thus using the system
(= appliance) configuration of lvm.  Later on, in the daemon, a local
copy of the lvm configuration is setup, and set it for use using the
LVM_SYSTEM_DIR environment variable: this means only the programmes
executed by the daemon will use the local lvm configuration, and not
lvmetad.

Thus manually start lvmetad from the daemon, right after having setup
the local lvm configuration, and still without failing if it cannot be
executed.

Additionally, since lvmetad now respects the right configuration, make
sure to update its cache when rescanning the VGs by passing --cache to
vgscan.
2016-07-26 18:24:31 +02:00
Richard W.M. Jones
d5a8f82887 Use 'const' for stack integers where possible.
May improve optimization possibilities in a few cases.
2016-07-26 10:43:45 +01:00
Pino Toscano
8e57268dd4 static const char *str -> static const char str[]
Make all the static constant strings as char arrays, so they can be
fully stored in read-only memory.
2016-07-22 13:16:02 +02:00
Pino Toscano
9fa8e07465 daemon: fix cleanup of stringsbuf usages
Declare most of the stringsbuf as CLEANUP_FREE_STRINGSBUF, so they are
freed completely on stack unwind: use take_stringsbuf() in return
places to take away from the stringsbuf its content, and remove all the
manual calls to free_stringslen (no more needed now).
This requires to not use free_stringslen anymore on failure in the
helper functions of stringsbuf, which now leave the content as-is (might
be still useful even on error).

This allows us to simplify the memory management of stringsbuf's, which
are not properly fully freed, fixing memory leaks in some error paths
(which were not calling free_stringslen).
2016-07-07 16:28:18 +02:00
Pino Toscano
6a06b87559 daemon: free the string on stringsbuf add failure
If add_string_nodup fails free the passed string instead of leaking it,
as that string would have been owned by the stringbuf.

Adapt few places to this behaviour.
2016-07-07 16:27:36 +02:00
Richard W.M. Jones
82aa5bd25e docs: Add internal documentation for a few daemon functions. 2016-04-12 20:59:40 +01:00
Richard W.M. Jones
fdfedcb4ef Use 'error' function for fprintf followed by exit.
Like with the previous commit, this replaces instances of:

  if (something_bad) {
    fprintf (stderr, "%s: error message\n", guestfs_int_program_name);
    exit (EXIT_FAILURE);
  }

with:

  if (something_bad)
    error (EXIT_FAILURE, 0, "error message");

(except in a few cases were errno was incorrectly being ignored, in
which case I have fixed that).

It's slightly more complex than the previous commit because we must be
careful to:

 - Remove the program name (since error(3) prints it).

 - Remove any trailing \n character from the message.

Candidates for replacement were found using:

  pcregrep --buffer-size 10M -M '\bfprintf\b.*\n.*\bexit\b' `git ls-files`
2016-04-04 17:57:38 +01:00
Richard W.M. Jones
129e4938ba Use 'error' function consistently throughout.
Wherever we had code which did:

  if (something_bad) {
    perror (...);
    exit (EXIT_FAILURE);
  }

replace this with use of the error(3) function:

  if (something_bad)
    error (EXIT_FAILURE, errno, ...);

The error(3) function is supplied by glibc, or by gnulib on platforms
which don't have it, and is much more flexible than perror(3).  Since
we already use error(3), there seems to be no downside to mandating it
everywhere.

Note there is one nasty catch with error(3): error (EXIT_SUCCESS, ...)
does *not* exit!  This is also the reason why error(3) cannot be
marked as __attribute__((noreturn)).

Because the examples can't use gnulib, I did not change them.

To search for multiline patterns of the above form, pcregrep -M turns
out to be very useful:

  pcregrep --buffer-size 10M -M '\bperror\b.*\n.*\bexit\b' `git ls-files`
2016-04-04 13:14:26 +01:00
Richard W.M. Jones
07c496c53c Use less stack.
GCC has two warnings related to large stack frames.  We were already
using the -Wframe-larger-than warning, but this reduces the threshold
from 10000 to 5000 bytes.

However that warning only covers the static part of frames (not
alloca).  So this change also enables -Wstack-usage=10000 which covers
both the static and dynamic usage (alloca and variable length arrays).

Multiple changes are made throughout the code to reduce frames to fit
within these new limits.

Note that stack allocation of large strings can be a security issue.
For example, we had code like:

 size_t len = strlen (fs->windows_systemroot) + 64;
 char software[len];
 snprintf (software, len, "%s/system32/config/software",
           fs->windows_systemroot);

where fs->windows_systemroot is guest controlled.  It's not clear what
the effects might be of allowing the guest to allocate potentially
very large stack frames, but at best it allows the guest to cause
libguestfs to segfault.  It turns out we are very lucky that
fs->windows_systemroot cannot be set arbitrarily large (see checks in
is_systemroot).

This commit changes those to large heap allocations instead.
2016-03-07 17:36:24 +00:00
Richard W.M. Jones
e23b44e2de daemon: Link with cleanups.c.
And remove the cleanup_* functions from guestfsd.c.

This fixes commit d94860d7e8.
2016-01-22 13:08:24 +00:00
Richard W.M. Jones
d94860d7e8 daemon: Split out command* functions and CLEANUP_* macros.
This allows the command* functions and CLEANUP_* macros to be used
independently from the daemon.
2016-01-22 09:12:17 +00:00
Richard W.M. Jones
307c83177c Update copyright dates for 2016.
Run the following command over the source:

  perl -pi.bak -e 's/(20[01][0-9])-2015/$1-2016/g' `git ls-files`
2016-01-02 21:19:51 +00:00
Richard W.M. Jones
7ecad8d4f7 daemon: Improve debug information from command*.
Do some fake quoting so people don't get worried that we aren't
quoting the commands we run.  See:
https://www.redhat.com/archives/libguestfs/2015-December/msg00080.html
2015-12-17 11:27:04 +00:00
Richard W.M. Jones
6d90cf5eb2 daemon: Make command*f flags parameter unsigned.
It's a bitmask, so unsigned is the right choice.  eg. We can more
easily print it using %x.

This patch changes the size of the fd mask and the values of the
COMMAND_FLAG_* constants, but since these are internal definitions
that doesn't matter.
2015-12-17 11:15:14 +00:00
Pino Toscano
9c3b4f6ad8 daemon: improve internal commandrvf
- add a flag to request chroot for the process, which is done only as
  very last (before chdir) operation before exec'ing the process in the
  child: this avoids using CHROOT_IN & CHROOT_OUT around command*
  invocations, and reduces the code spent in chroot mode
- add failure checks for dup2, open, and chdir done in child, not
  proceeding to executing the process if they fail
- open /dev/null without O_CLOEXEC, so it stays available for the
  exec'ed process, and thus we don't need to provide an own fd for stdin

Followup of commit fd2f175ee7, thanks also
to the notes and hints provided by Mateusz Guzik.
2015-12-15 15:00:11 +01:00
Richard W.M. Jones
2ee2003d56 daemon: Allow --channel fd:N
In this case, the daemon has been passed an open file descriptor which
it uses directly for the channel.
2015-11-24 19:13:39 +00:00
Richard W.M. Jones
d07515b947 docs: Move 'extending libguestfs' documentation to guestfs-hacking(1).
Start to split the huge, monolithic guestfs(3) manual page.
2015-10-30 09:45:02 +00:00
Pino Toscano
29e6368f4c daemon: use str_udevadm in udev_settle
There is GUESTFSD_EXT_CMD defining a string for udevadm (so it is marked
as "used tool" in the appliance), but it is not actually used when
starting udevadm.

There should be no behaviour change.
2015-10-05 15:43:35 +02:00
Richard W.M. Jones
677c721e85 Fix whitespace.
Because of previous automated commits, such as changing 'guestfs___'
-> 'guestfs_int_', several function calls no longer lined up with
their parameters, and some lines were too long.

The bulk of this commit was done using emacs batch mode and the
technique described here:

  http://www.cslab.pepperdine.edu/warford/BatchIndentationEmacs.html

The changes suggested by emacs were then reviewed by hand.
2015-10-05 14:28:33 +01:00
Chen Hanxiao
d29337605a daemon: add a space after func/macro to fit code-style
more daemon codes covered

Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-07-17 13:27:13 +01:00
Richard W.M. Jones
ccdbbc7fe4 Fix various -Wformat problems.
Updating gnulib has caused -Wformat-signedness to be enabled.  This
has revealed many problems in C format strings.  The fixes here fall
into the following main categories:

 - Using %d with an unsigned parameter.

 - %x and %o expect an unsigned argument.

 - uid_t and gid_t are unsigned on Linux.  The safe way to print these
   is to cast them to uintmax_t and then print them using the %ju
   modifier (see http://stackoverflow.com/a/1401581).

 - Using %d to print an enum.  Since enums may be either char or int,
   I fixed this by casting the enum to int.

 - strtol_error & lzma_ret are both unsigned types.
2015-07-02 16:08:44 +01:00
Chen Hanxiao
d0c067df87 daemon: add get_random_uuid
Signed-off-by: Chen Hanxiao <chenhanxiao@cn.fujitsu.com>
2015-06-30 15:54:57 +02:00
Richard W.M. Jones
8cb1a35d75 daemon: Move all the kernel command line parsing to the init script.
Instead of parsing /proc/cmdline from the daemon, move all of that
parsing into the init script, and pass the argument via the daemon
command line.

For example, previously the daemon and init script both looked for
guestfs_network=1 in /proc/cmdline.  Now the init script still looks
for it, and if found it runs `guestfsd --network'.
2015-06-29 23:08:43 +01:00