1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2017, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(c_error,
   36          [ posix_status/1,                     % +Status
   37            posix_status/4,                     % +Status, +Op, +Type, +Arg
   38            posix_ptr_status/1,                 % +Status
   39            posix_ptr_status/4,                 % +Status, +Op, +Type, +Arg
   40            posix_raise_error/0,
   41            posix_raise_error/3			% +Op, +Type, +Arg
   42          ]).   43:- use_module(ffi).

C interface error handling

This module provides common routines to map error codes into apropriate actions in Prolog. Below is a typical example mapping the statfs() function:

:- module(libc_files,
          [ statfs/2
          ]).
:- use_module(library(cinvoke)).

:- c_import("#include <sys/vfs.h>",
            [ libc ],
            [ statfs(+string, -struct(statfs), [-int])
            ]).

statfs(File, FsStat) :-
    statfs(File, FsStat, Status),
    posix_status(Status, statfs, file, File).

*/

   68cpp_const('ENOENT').
   69cpp_const('EPERM').
   70cpp_const('EACCES').
   71cpp_const('ENOMEM').
   72
   73:- c_import("#include <string.h>
   74             #include <errno.h>",
   75            [ libc ],
   76            [ strerror(+int, [string]) ]).
 posix_status(+Code) is det
 posix_status(+Code, +Action, +Type, +Argument) is det
These predicates may be used to map POSIX int error return status into a suitable Prolog response. If Code is non-negative the predicate simply succeeds. For other cases it retrieves the error code using c_errno/1 and translates the error into a suitable Prolog exception.
   87posix_status(Status) :-
   88    (   Status >= 0
   89    ->  true
   90    ;   posix_raise_error
   91    ).
   92
   93posix_status(Status, Op, Type, Arg) :-
   94    (   Status >= 0
   95    ->  true
   96    ;   posix_raise_error(Op, Type, Arg)
   97    ).
 posix_ptr_status(+Code) is det
 posix_ptr_status(+Code, +Action, +Type, +Argument) is det
Handle the return code from POSIX functions that return a NULL pointer on error.
  105posix_ptr_status(Ptr) :-
  106    c_is_nil(Ptr),
  107    !,
  108    posix_raise_error.
  109posix_ptr_status(_).
  110
  111posix_ptr_status(Ptr, Op, Type, Arg) :-
  112    c_is_nil(Ptr),
  113    !,
  114    posix_raise_error(Op, Type, Arg).
  115posix_ptr_status(_, _, _, _).
 posix_raise_error is det
 posix_raise_error(+Action, +Type, +Argument) is det
Raise an error from a POSIX errno code.
Errors
- posix_error(Errno, String)
  125posix_raise_error :-
  126    c_errno(Errno),
  127    strerror(Errno, String),
  128    throw(error(posix_error(Errno, String), _)).
  129
  130posix_raise_error(Op, Type, Arg) :-
  131    c_errno(Errno),
  132    strerror(Errno, String),
  133    posix_exception_context(Op, Type, Arg, String, Context),
  134    (   posix_exception(Errno, Op, Type, Arg, Context)
  135    ->  true
  136    ;   throw(error(posix_error(Errno, String), Context))
  137    ).
  138
  139posix_exception_context(Op, Type, Arg, String, Context) :-
  140    Context = context(_Stack, posix(Op, Type, Arg, String)).
  141
  142posix_exception('ENOENT', _Op, Type, Arg, Context) :- !,
  143    throw(error(existence_error(Type, Arg), Context)).
  144posix_exception('EACCES', Op, Type, Arg, Context) :- !,
  145    throw(error(permission_error(Op, Type, Arg), Context)).
  146posix_exception('EPERM', Op, Type, Arg, Context) :- !,
  147    throw(error(permission_error(Op, Type, Arg), Context)).
  148posix_exception('ENOMEM', _Op, _Type, _Arg, Context) :- !,
  149    throw(error(resource_error(memory), Context)).
  150
  151
  152		 /*******************************
  153		 *            MESSAGES		*
  154		 *******************************/
  155
  156:- multifile
  157    prolog:message_context//1,
  158    prolog:error_message//1.  159
  160prolog:message_context(context(_, posix(Op, Type, Arg, _String))) -->
  161    { nonvar(Op) },
  162    [ ' in ~w on ~p ~p'-
  163      [Op, Type, Arg] ].
  164prolog:error_message(posix_error(Errno, String)) -->
  165    [ '~p (errno=~p)'-[String, Errno] ]