From a15768b0bf0922e64695aeab9473abf36dcda1af Mon Sep 17 00:00:00 2001 From: Ryan Lee Date: Mon, 23 Sep 2024 12:45:08 -0700 Subject: [PATCH] Write an output typemap for errno-based functions In Python, return status is signalled by exceptions (or lack thereof) instead of int. Keep the typemap portable for any other languages we may add in the future. Signed-off-by: Ryan Lee --- libraries/libapparmor/swig/SWIG/libapparmor.i | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/libraries/libapparmor/swig/SWIG/libapparmor.i b/libraries/libapparmor/swig/SWIG/libapparmor.i index d56f5000d..b66b7ea5d 100644 --- a/libraries/libapparmor/swig/SWIG/libapparmor.i +++ b/libraries/libapparmor/swig/SWIG/libapparmor.i @@ -123,15 +123,34 @@ extern char *aa_splitcon(char *con, char **mode); /* apparmor.h */ extern int aa_is_enabled(void); -/* aa_find_mountpoint mnt is an output pointer to a heap-allocated string */ -%cstring_output_allocate(char **mnt, free(*$1)); -extern int aa_find_mountpoint(char **mnt); + +/* These should not receive the VOID_Object typemap */ extern int aa_change_hat(const char *subprofile, unsigned long magic_token); extern int aa_change_profile(const char *profile); extern int aa_change_onexec(const char *profile); extern int aa_change_hatv(const char *subprofiles[], unsigned long token); extern int aa_stack_profile(const char *profile); extern int aa_stack_onexec(const char *profile); + +/* aa_find_mountpoint mnt is an output pointer to a heap-allocated string */ +%cstring_output_allocate(char **mnt, free(*$1)); +/* The other errno-based functions should not always be returning the int value: + * - Python exceptions signal success/failure status instead via the %exception + * handler above. + * - Perl (the other binding) has $! for accessing errno but would check the int + * return status first. + * + * The generated C code for (out) resets the return value to None + * before appending the returned data (argout generated by %cstring stuff) + */ +#ifdef SWIGPYTHON +%typemap(out,noblock=1) int { +#if defined(VOID_Object) + $result = VOID_Object; +#endif +} +#endif +extern int aa_find_mountpoint(char **mnt); extern int aa_getprocattr(pid_t tid, const char *attr, char **label, char **mode); extern int aa_gettaskcon(pid_t target, char **label, char **mode); extern int aa_getcon(char **label, char **mode);