42 Commits

Author SHA1 Message Date
shivanayak
8d4d35c604 ocaml: fix memory leak in guestfs_int_ocaml_strings_val on strdup failure (#324)
When strdup() fails partway through the loop, caml_raise_out_of_memory()
longjmps without freeing the previously allocated strings or the array.
Free all prior allocations before raising the exception.

Co-authored-by: Claude <noreply@anthropic.com>
2026-03-15 09:55:02 +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
e93fd7e8ac ocaml: Nullify custom block before releasing runtime lock
Avoids a potential, though if possible then very rare, double free
path.

Suggested-by: Guillaume Munch-Maccagnoni
See: https://github.com/ocaml/ocaml/issues/12820
2023-12-14 08:35:46 +00:00
Richard W.M. Jones
61418535ad ocaml: Use Gc.finalise instead of a C finalizer
Since OCaml 5.1.1, changes to custom blocks caused C finalizers that
call caml_enter_blocking_section to stop working (if they ever did
before).  They are relatively inflexible compared to registering an
OCaml finalizer (Gc.finalise) to call Guestfs.close, so use that
instead.

Suggested-by: Guillaume Munch-Maccagnoni
See: https://github.com/ocaml/ocaml/issues/12820
See: db48794fa8
2023-12-13 22:55:03 +00:00
Richard W.M. Jones
cade0b1aeb ocaml: Use Caml_state_opt in preference to caml_state
Link: https://discuss.ocaml.org/t/test-caml-state-and-conditionally-caml-acquire-runtime-system-good-or-bad/12489/7
Thanks: Guillaume Munch-Maccagnoni
2023-06-27 16:33:09 +01:00
Richard W.M. Jones
16464878cf ocaml: Conditionally acquire the lock in callbacks
This fix was originally suggested by Jürgen Hötzel (link below) which
I have lightly modified so it works with OCaml <= 4 too.

Link: https://listman.redhat.com/archives/libguestfs/2023-May/031640.html
Link: https://discuss.ocaml.org/t/test-caml-state-and-conditionally-caml-acquire-runtime-system-good-or-bad/12489
2023-06-27 12:46:41 +01:00
Richard W.M. Jones
4a79c023e5 ocaml: Release runtime lock around guestfs_close
When finalizing the handle we call guestfs_close.  This function could
be long-running (eg. it may have to shut down the qemu subprocess), so
release the runtime lock.
2023-06-27 12:46:18 +01:00
Richard W.M. Jones
7e1d7c1330 ocaml: Replace old enter/leave_blocking_section calls
Since OCaml 4 the old and confusing caml_enter_blocking_section and
caml_leave_blocking_section calls have been replaced with
caml_release_runtime_system and caml_acquire_runtime_system (in that
order).  Use the new names.
2023-06-27 11:34:21 +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
ea04d6b878 ocaml/guestfs-c.c: Avoid bogus -fanalyzer warning
This warning is bogus, caused by the analyzer cannot track that len ==
0 if roots == NULL.  I just changed the code to make it easier to
analyze, this doesn't fix any real bug.

guestfs-c.c: In function 'guestfs_finalize':
guestfs-c.c:85:9: error: dereference of NULL '0B' [CWE-476] [-Werror=analyzer-null-dereference]
   85 |         caml_remove_generational_global_root (roots[i]);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2021-09-07 16:45:23 +01:00
Richard W.M. Jones
0e17236d7d Update copyright dates to 2020. 2020-03-06 19:32:32 +00:00
Richard W.M. Jones
9f3148c791 ocaml: Use caml_alloc_initialized_string instead of memcpy.
See this commit in libguestfs-common:
398dc56a6c
2020-02-06 10:32:08 +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
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
Pino Toscano
9bb1e52253 Fully initialize the custom_operations structs
Use also custom_compare_ext_default for the compare_ext field.

According to the git logs, this was introduced in OCaml 3.12.1, which is
earlier than out minimum required version.

mlaugeas is not touched by this change, since it is a copy of a 3rd
party library (and thus it will be fixed there first).
2017-10-09 12:53:43 +02:00
Richard W.M. Jones
ffdcb7cfeb build: Require <caml/unixsupport.h>
Require <caml/unixsupport.h> (an OCaml header file) and remove
alternate defintions of ‘Nothing’ and ‘unix_error’ which are defined
in this header file.

We require OCaml >= 3.11 which has this header file, so there is no
need to test for it or provide alternative definitions.

Thanks: Pino Toscano.
2017-07-18 18:42:23 +01:00
Richard W.M. Jones
e6c89f9631 utils: Rename ‘guestfs-internal-frontend.h’ to ‘guestfs-utils.h’.
The reason it's not just ‘utils.h’ is because Pino is worried that we
might pick up /usr/include/utils.h from a rogue library.
2017-07-10 17:01:59 +01:00
Pino Toscano
64e1101631 ocaml: do not try to malloc 0 elements in get_all_event_callbacks
In case there are no event handlers registered with the handle,
get_all_event_callbacks will count 0 elements, trying to malloc a buffer
of that size.  POSIX says that this can result in either a null pointer,
or an unusable pointer.

Short-circuit get_all_event_callbacks to allocate nothing when there are
no events, making sure to use its results only when there were events.
2017-03-06 09:02:02 +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
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
Richard W.M. Jones
0445284dfb ocaml: Stop using the safe_malloc, etc. functions. 2016-02-05 13:15:48 +00:00
Richard W.M. Jones
53403fbdef ocaml, virt tools: More consistent naming for C functions.
For functions linked to virt tools, make sure the name of the function
actually matches the virt tool.  Because of the history of moving
functions across tools, some names no longer matched.

For mllib, use `guestfs_int_mllib_' prefix.

For OCaml bindings, use `guestfs_int_ocaml_' prefix.

This commit is by no means complete.  There are many other C functions
in other language bindings which could do with being more consistently
named.

There is no functional change, this is just refactoring.
2016-01-28 21:30:20 +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
8bbc5e73cb ocaml: Allow Guestfs.t handle to be garbage collected.
** NB: This is an API break for OCaml programs using Guestfs.event_callback. **

Because of the way I implemented Guestfs.event_callback which had the
Guestfs.t handle as the first parameter, we had to store the (OCaml)
Guestfs.t handle in the C handle's private data area.  To do that, we
had to create a global root pointing to the handle.

This of course meant that the handle could not be garbage collected
(thanks Roman Kagan for spotting this).

This changes the API of Guestfs.event_callback so that a handle is no
longer passed.  The OCaml handle can now be garbage collected again.

For programs that need the Guestfs.t handle in the callback function
(which turns out to be *none* of the OCaml programs we have written),
you can do:

  g#set_event_callback (callback_fn g) [Guestfs.EVENT_FOO];

In this case, since the closure passed to Guestfs.set_event_callback
is still registered as a global root, that will capture a reference to
the handle, so the handle won't be able to be garbage collected until
you delete the callback.
2015-10-06 22:28:27 +01:00
Richard W.M. Jones
3858db9d58 ocaml: Use generational global roots.
These are considerably more efficient than ordinary global roots, but
with the caveat that the program is not allowed to modify them without
calling a special function.  We don't modify them, so this change is
safe.

This requires OCaml >= 3.11, but we have that on RHEL 6
(since we dropped support for RHEL 5).

See also:
http://caml.inria.fr/pub/ml-archives/caml-list/2008/03/c3bf86990088236ceeb9a0f0f4c35390.en.html
2015-10-06 15:53:31 +01: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
Pino Toscano
649f439cb7 ocaml: dynamically generate the content of Guestfs.Errno
Put in a list the errnos to expose, filling the content of the
Guestfs.Errno submodule from that.
Also, generate a separate guestfs-c-errnos.c with the implementations of
the functions returning the errno codes.

Only code motion and refactoring, no actual changes on the content of
the ocaml Guestfs module.
2015-08-24 17:57:10 +02:00
Richard W.M. Jones
94ea635fb1 ocaml: Add handling for errno EINVAL. 2015-06-18 10:26:53 +01:00
Richard W.M. Jones
6297534091 ocaml: Add Guestfs.Errno.errno_EPERM. 2015-03-26 09:16:44 +00:00
Richard W.M. Jones
bfbcc01403 Change guestfs___* to guestfs_int_*
libguestfs has used double and triple underscores in identifiers.
These aren't valid for global names in C++.

The first step is to replace all guestfs___* (3 underscores) with
guestfs_int_*.  We've used guestfs_int_* elsewhere already as a prefix
for internal identifiers.

This is an entirely mechanical change done using:
  git ls-files | xargs perl -pi.bak -e 's/guestfs___/guestfs_int_/g'

Reference: http://stackoverflow.com/a/228797
2015-02-14 18:46:04 +00:00
Richard W.M. Jones
c5800dc97d Update copyright dates for 2015. 2015-01-17 09:08:15 +00:00
Richard W.M. Jones
5d4e4e7eca ocaml: Add ESRCH to Guestfs.Errno module. 2014-03-31 14:21:45 +01:00
Richard W.M. Jones
0563405d2e ocaml: Add Guestfs.Errno submodule exposing useful raw errno numbers.
For use when calling G.last_errno.
2014-03-17 12:22:50 +00:00
Richard W.M. Jones
6c971faecf Update copyright dates for 2014. 2014-01-02 16:53:34 +00:00
Richard W.M. Jones
d08f5ebd9a generator: Generate guestfs_user_cancel.
This function is now generated, so bindings in various languages
are made automatically.

Note that the function previously returned void, but now it returns
int (although always 0).  We don't believe that this is an ABI break
since existing programs will continue to work.
2013-03-04 15:24:26 +00:00
Richard W.M. Jones
4ede94ab7b ocaml: Use guestfs___free_string_list instead of custom-coded one.
It should be possible to add the guestfs___free_string_list to
dllmlguestfs.so, but I cannot work out exactly how to do this.  As a
result we end up using src/utils.c directly.
2013-02-20 22:18:10 +00:00
Richard W.M. Jones
5c513060b1 ocaml: Add binding for Guestfs.event_to_string and use it in events test. 2013-02-19 13:41:57 +00:00
Richard W.M. Jones
ec3b75e5ff Rearrange internal header files.
This commit rearranges the internal header files.

"src/guestfs-internal.h" is just for the library, as before.

"src/guestfs-internal-frontend.h" is for use by all library, bindings,
tools C code, but NOT the daemon.

"src/guestfs-internal-all.h" is for use by all C code including the
daemon.

This is just code motion, but it has some important consequences:

(1) We can use the CLEANUP_* macros in bindings and tools code.

(2) We can get rid of TMP_TEMPLATE_ON_STACK.

(3) We will (in future) be able to stop bindings and tools code from
using the safe_* allocation functions (which are NOT safe to use
outside the library alone).
2013-02-01 14:07:25 +00:00
Richard W.M. Jones
62e775c350 Change the handling of private functions, safe_malloc etc.
Rename guestfs_safe_malloc et al to guestfs___safe_malloc etc.

To use the private functions, code now has to define
-DGUESTFS_PRIVATE_FUNCTIONS=1.  This will make it easier for us in
future to work out which programs are using these functions and to
minimize both the number of programs and the functions they are
calling.

Note that the Perl, Python, OCaml, Ruby and Java bindings use
guestfs_safe_* calls.  None of the other bindings do.  This is a bug
(in the bindings using those functions): these functions will call the
out of memory callback on failure.  This function defaults to abort(),
and since this happens from a language binding, there is no way to
change this default.
2012-12-15 19:41:29 +00:00
Richard W.M. Jones
3d46f7dc03 ocaml: Get rid of CAMLprim.
It's wrong to use it, and in any case it doesn't do anything on Linux.
2012-10-15 22:38:27 +01:00
Richard W.M. Jones
9466060201 New APIs: guestfs_create_flags, guestfs_parse_environment,
guestfs_parse_environment_list.

Add a new function for creating a handle:

 guestfs_h *guestfs_create_flags (unsigned flags [, ...]);

This variant lets you supply flags and extra arguments, although extra
arguments are not used at the moment.

Of particular interest is the ability to separate the creation of the
handle from the parsing of environment variables like
LIBGUESTFS_DEBUG.  guestfs_create does both together, which prevents
us from propagating errors from parsing environment variables back to
the caller (guestfs_create has always printed any errors on stderr and
then just ignored them).

If you are interested in these errors, you can now write:

 g = guestfs_create_flags (GUESTFS_CREATE_NO_ENVIRONMENT);
 if (!g)
   exit (EXIT_FAILURE);
 r = guestfs_parse_environment (g);
 if (!r)
   exit (EXIT_FAILURE);

Also you can *omit* the call to guestfs_parse_environment, which
creates a handle unaffected by the environment (which was not possible
before).

This commit also includes new (backwards compatible) changes to the
OCaml, Perl, Python, Ruby and Java constructors that let you use the
flags.
2012-10-15 15:04:43 +01:00
Richard W.M. Jones
2e4089f300 build: Rename most C files that contain underscore with dash.
This is just code motion.

Some files cannot be renamed.  Notably rpcgen input and output files
must not contain dash characters, else rpcgen breaks.
2012-07-19 16:11:47 +01:00