[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Throwing an Unix.unix_error from C code



Hi,

this might be off topic but maybe someone here can help.

I have some C code to calculate the md5sum of a file:

MD5.file: string -> string
value md5file(vlaue filename);

The C code uses open() and read() which both can give an error. To
report them I would like to throw an Unix.unix_error exception and the
question is how.

In detail I have the following problems:

1)

- I create a 3 tupple exp
- fill it with (Val_int(errno), caml_copy_string("MD5.file"), filename)
- call caml_raise_with_arg(tag, exp)

If I don't catch the exception the programs fails printing the
Unix.unix_error correctly (so everything points to where it should).
If I instead catch and print the exception ocaml segfaults on
"MD5.file". Maybe the GC frees some memory or moves stuff around
destroying the pointers in the exception.


2)

- I create a 4 tuple bucket
- fill it with (tag, Val_int(errno), caml_copy_string("MD5.file"), filename)
- caml_raise(bucket)

No segfault, so I believe my C code to build tuples is correct.

But errno does not map 1:1 to Unix.error so the first part of
Unix.unix_error is screwed up.

I would have to call the private function 'value unix_error_of_code
(int errcode)' to convert errno into Unix.error.


3)

- call unix_error(errno, "MD5.file", filename)
  or simpler uerror("MD5.file", filename)

Now everything works. But again unix_error and uerror are private
functions.



So here are my big questions:

Is there maybe a bug in caml_raise_with_arg when called with complex
types?

Is there any (sane) way to create an Unix.unix_error without resorting
to private functions?

Could we have a public 'caml/unix.h' file exporting the functionality
from the Unix module for use in C code? Make the functions public?

MfG
        Goswin



Reply to: