Changeset View
Changeset View
Standalone View
Standalone View
head/lib/libcasper/libcasper/libcasper.h
/*- | /*- | ||||
* Copyright (c) 2012-2013 The FreeBSD Foundation | * Copyright (c) 2012-2013 The FreeBSD Foundation | ||||
* Copyright (c) 2015 Mariusz Zaborski <oshogbo@FreeBSD.org> | * Copyright (c) 2015-2017 Mariusz Zaborski <oshogbo@FreeBSD.org> | ||||
* All rights reserved. | * All rights reserved. | ||||
* | * | ||||
* This software was developed by Pawel Jakub Dawidek under sponsorship from | * This software was developed by Pawel Jakub Dawidek under sponsorship from | ||||
* the FreeBSD Foundation. | * the FreeBSD Foundation. | ||||
* | * | ||||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
* modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||
* are met: | * are met: | ||||
Show All 16 Lines | |||||
* SUCH DAMAGE. | * SUCH DAMAGE. | ||||
* | * | ||||
* $FreeBSD$ | * $FreeBSD$ | ||||
*/ | */ | ||||
#ifndef _LIBCASPER_H_ | #ifndef _LIBCASPER_H_ | ||||
#define _LIBCASPER_H_ | #define _LIBCASPER_H_ | ||||
#ifdef HAVE_CASPER | |||||
#define WITH_CASPER | |||||
#endif | |||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <sys/nv.h> | |||||
#include <stdlib.h> | |||||
#include <unistd.h> | |||||
#ifndef _NVLIST_T_DECLARED | #ifndef _NVLIST_T_DECLARED | ||||
#define _NVLIST_T_DECLARED | #define _NVLIST_T_DECLARED | ||||
struct nvlist; | struct nvlist; | ||||
typedef struct nvlist nvlist_t; | typedef struct nvlist nvlist_t; | ||||
#endif | #endif | ||||
#ifndef _CAP_CHANNEL_T_DECLARED | #ifndef _CAP_CHANNEL_T_DECLARED | ||||
#define _CAP_CHANNEL_T_DECLARED | #define _CAP_CHANNEL_T_DECLARED | ||||
#ifdef WITH_CASPER | |||||
struct cap_channel; | struct cap_channel; | ||||
typedef struct cap_channel cap_channel_t; | typedef struct cap_channel cap_channel_t; | ||||
#endif | #else | ||||
struct cap_channel { | |||||
int cch_fd; | |||||
}; | |||||
typedef struct cap_channel cap_channel_t; | |||||
#endif /* ! WITH_CASPER */ | |||||
#endif /* ! _CAP_CHANNEL_T_DECLARED */ | |||||
/* | /* | ||||
* The functions opens unrestricted communication channel to Casper. | * The functions opens unrestricted communication channel to Casper. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
cap_channel_t *cap_init(void); | cap_channel_t *cap_init(void); | ||||
#else | |||||
static inline cap_channel_t * | |||||
cap_init(void) | |||||
{ | |||||
cap_channel_t *chan; | |||||
chan = malloc(sizeof(*chan)); | |||||
if (chan != NULL) { | |||||
chan->cch_fd = -1; | |||||
} | |||||
return (chan); | |||||
} | |||||
#endif | |||||
/* | /* | ||||
* The functions to communicate with service. | * The functions to communicate with service. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
cap_channel_t *cap_service_open(const cap_channel_t *chan, const char *name); | cap_channel_t *cap_service_open(const cap_channel_t *chan, const char *name); | ||||
int cap_service_limit(const cap_channel_t *chan, | int cap_service_limit(const cap_channel_t *chan, | ||||
const char * const *names, size_t nnames); | const char * const *names, size_t nnames); | ||||
#else | |||||
#define cap_service_open(chan, name) (cap_init()) | |||||
#define cap_service_limit(chan, names, nnames) (0) | |||||
#endif | |||||
/* | /* | ||||
* The function creates cap_channel_t based on the given socket. | * The function creates cap_channel_t based on the given socket. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
cap_channel_t *cap_wrap(int sock); | cap_channel_t *cap_wrap(int sock); | ||||
#else | |||||
static inline cap_channel_t * | |||||
cap_wrap(int sock) | |||||
{ | |||||
cap_channel_t *chan; | |||||
chan = cap_init(); | |||||
if (chan != NULL) { | |||||
chan->cch_fd = sock; | |||||
} | |||||
return (chan); | |||||
} | |||||
#endif | |||||
/* | /* | ||||
* The function returns communication socket and frees cap_channel_t. | * The function returns communication socket and frees cap_channel_t. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
int cap_unwrap(cap_channel_t *chan); | int cap_unwrap(cap_channel_t *chan); | ||||
#else | |||||
#define cap_unwrap(chan) (chan->cch_fd) | |||||
#endif | |||||
/* | /* | ||||
* The function clones the given capability. | * The function clones the given capability. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
cap_channel_t *cap_clone(const cap_channel_t *chan); | cap_channel_t *cap_clone(const cap_channel_t *chan); | ||||
#else | |||||
static inline cap_channel_t * | |||||
cap_clone(const cap_channel_t *chan) | |||||
{ | |||||
cap_channel_t *newchan; | |||||
newchan = cap_init(); | |||||
if (newchan == NULL) { | |||||
return (NULL); | |||||
} | |||||
if (chan->cch_fd == -1) { | |||||
newchan->cch_fd = -1; | |||||
} else { | |||||
newchan->cch_fd = dup(chan->cch_fd); | |||||
if (newchan->cch_fd < 0) { | |||||
free(newchan); | |||||
newchan = NULL; | |||||
} | |||||
} | |||||
return (newchan); | |||||
} | |||||
#endif | |||||
/* | /* | ||||
* The function closes the given capability. | * The function closes the given capability. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
void cap_close(cap_channel_t *chan); | void cap_close(cap_channel_t *chan); | ||||
#else | |||||
static inline void | |||||
cap_close(cap_channel_t *chan) | |||||
{ | |||||
if (chan->cch_fd >= 0) { | |||||
close(chan->cch_fd); | |||||
} | |||||
free(chan); | |||||
} | |||||
#endif | |||||
/* | /* | ||||
* The function returns socket descriptor associated with the given | * The function returns socket descriptor associated with the given | ||||
* cap_channel_t for use with select(2)/kqueue(2)/etc. | * cap_channel_t for use with select(2)/kqueue(2)/etc. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
int cap_sock(const cap_channel_t *chan); | int cap_sock(const cap_channel_t *chan); | ||||
#else | |||||
#define cap_sock(chan) (chan->cch_fd) | |||||
#endif | |||||
/* | /* | ||||
* The function limits the given capability. | * The function limits the given capability. | ||||
* It always destroys 'limits' on return. | * It always destroys 'limits' on return. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
int cap_limit_set(const cap_channel_t *chan, nvlist_t *limits); | int cap_limit_set(const cap_channel_t *chan, nvlist_t *limits); | ||||
#else | |||||
#define cap_limit_set(chan, limits) (0) | |||||
#endif | |||||
/* | /* | ||||
* The function returns current limits of the given capability. | * The function returns current limits of the given capability. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
int cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp); | int cap_limit_get(const cap_channel_t *chan, nvlist_t **limitsp); | ||||
#else | |||||
static inline int | |||||
cap_limit_get(const cap_channel_t *chan __unused, nvlist_t **limitsp) | |||||
{ | |||||
*limitsp = nvlist_create(0); | |||||
return (0); | |||||
} | |||||
#endif | |||||
/* | /* | ||||
* Function sends nvlist over the given capability. | * Function sends nvlist over the given capability. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
int cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl); | int cap_send_nvlist(const cap_channel_t *chan, const nvlist_t *nvl); | ||||
#else | |||||
#define cap_send_nvlist(chan, nvl) (0) | |||||
#endif | |||||
/* | /* | ||||
* Function receives nvlist over the given capability. | * Function receives nvlist over the given capability. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
nvlist_t *cap_recv_nvlist(const cap_channel_t *chan, int flags); | nvlist_t *cap_recv_nvlist(const cap_channel_t *chan, int flags); | ||||
#else | |||||
#define cap_recv_nvlist(chan, flags) (0) | |||||
#endif | |||||
/* | /* | ||||
* Function sends the given nvlist, destroys it and receives new nvlist in | * Function sends the given nvlist, destroys it and receives new nvlist in | ||||
* response over the given capability. | * response over the given capability. | ||||
*/ | */ | ||||
#ifdef WITH_CASPER | |||||
nvlist_t *cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl, int flags); | nvlist_t *cap_xfer_nvlist(const cap_channel_t *chan, nvlist_t *nvl, int flags); | ||||
#else | |||||
static inline nvlist_t * | |||||
cap_xfer_nvlist(const cap_channel_t *chan __unused, nvlist_t *nvl, int flags) | |||||
{ | |||||
nvlist_destroy(nvl); | |||||
return (nvlist_create(flags)); | |||||
} | |||||
#endif | |||||
#endif /* !_LIBCASPER_H_ */ | #endif /* !_LIBCASPER_H_ */ |