mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
lws_system
This commit is contained in:
parent
f00194c321
commit
b606c883f3
6 changed files with 110 additions and 1 deletions
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010-2018 Andy Green <andy@warmcat.com>
|
||||
* Copyright (C) 2010-2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
|
@ -517,6 +517,7 @@ struct lws_tokens;
|
|||
struct lws_vhost;
|
||||
struct lws;
|
||||
|
||||
#include <libwebsockets/lws-system.h>
|
||||
#include <libwebsockets/lws-ws-close.h>
|
||||
#include <libwebsockets/lws-callbacks.h>
|
||||
#include <libwebsockets/lws-ws-state.h>
|
||||
|
|
|
@ -673,6 +673,9 @@ struct lws_context_creation_info {
|
|||
* on a unix socket, you can give a "username:groupname" string here
|
||||
* to control the owner:group it's created with. It's always created
|
||||
* with 0660 mode. */
|
||||
const lws_system_ops_t *system_ops;
|
||||
/**< CONTEXT: hook up lws_system_ apis to system-specific
|
||||
* implementations */
|
||||
|
||||
/* Add new things just above here ---^
|
||||
* This is part of the ABI, don't needlessly break compatibility
|
||||
|
|
84
include/libwebsockets/lws-system.h
Normal file
84
include/libwebsockets/lws-system.h
Normal file
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010-2019 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation:
|
||||
* version 2.1 of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*
|
||||
* included from libwebsockets.h
|
||||
*
|
||||
* This provides a clean way to interface lws user code to be able to
|
||||
* work unchanged on different systems for fetching common system information,
|
||||
* and performing common system operations like reboot.
|
||||
*
|
||||
* An ops struct with the system-specific implementations is set at
|
||||
* context creation time, and apis are provided that call through to
|
||||
* those where they exist.
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
LWS_SYSI_HRS_DEVICE_MODEL = 1,
|
||||
LWS_SYSI_HRS_DEVICE_SERIAL,
|
||||
LWS_SYSI_HRS_FIRMWARE_VERSION,
|
||||
|
||||
LWS_SYSI_USER_BASE = 100
|
||||
} lws_system_item_t;
|
||||
|
||||
typedef union {
|
||||
const char *hrs; /* human readable string */
|
||||
void *data;
|
||||
time_t t;
|
||||
} lws_system_arg_t;
|
||||
|
||||
typedef struct lws_system_ops {
|
||||
int (*get_info)(lws_system_item_t i, lws_system_arg_t arg, size_t *len);
|
||||
int (*reboot)(void);
|
||||
} lws_system_ops_t;
|
||||
|
||||
/* wrappers handle NULL members or no ops struct set at all cleanly */
|
||||
|
||||
/**
|
||||
* lws_system_get_info() - get standardized system information
|
||||
*
|
||||
* \param context: the lws_context
|
||||
* \param item: which information to fetch
|
||||
* \param arg: where to place the result
|
||||
* \param len: incoming: max length of result, outgoing: used length of result
|
||||
*
|
||||
* This queries a standardized information-fetching ops struct that can be
|
||||
* applied to the context... the advantage is it allows you to get common items
|
||||
* of information like a device serial number writing the code once, even if the
|
||||
* actual serial number muse be fetched in wildly different ways depending on
|
||||
* the exact platform it's running on.
|
||||
*
|
||||
* Set arg and *len on entry to be the result location and the max length that
|
||||
* can be used there, on seccessful exit *len is set to the actual length and
|
||||
* 0 is returned. On error, 1 is returned.
|
||||
*/
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
lws_system_get_info(struct lws_context *context, lws_system_item_t item,
|
||||
lws_system_arg_t arg, size_t *len);
|
||||
|
||||
|
||||
/**
|
||||
* lws_system_reboot() - if provided, use the lws_system ops to reboot
|
||||
*
|
||||
* \param context: the lws_context
|
||||
*
|
||||
* If possible, the system will reboot. Otherwise returns 1.
|
||||
*/
|
||||
LWS_EXTERN LWS_VISIBLE int
|
||||
lws_system_reboot(struct lws_context *context);
|
|
@ -120,6 +120,7 @@ lws_create_context(const struct lws_context_creation_info *info)
|
|||
context->gid = info->gid;
|
||||
context->username = info->username;
|
||||
context->groupname = info->groupname;
|
||||
context->system_ops = info->system_ops;
|
||||
|
||||
/* if he gave us names, set the uid / gid */
|
||||
if (lws_plat_drop_app_privileges(context, 0))
|
||||
|
|
|
@ -934,3 +934,22 @@ lws_humanize(char *p, int len, uint64_t v, const lws_humanize_unit_t *schema)
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_system_get_info(struct lws_context *context, lws_system_item_t item,
|
||||
lws_system_arg_t arg, size_t *len)
|
||||
{
|
||||
if (!context->system_ops || !context->system_ops->get_info)
|
||||
return 1;
|
||||
|
||||
return context->system_ops->get_info(item, arg, len);
|
||||
}
|
||||
|
||||
int
|
||||
lws_system_reboot(struct lws_context *context)
|
||||
{
|
||||
if (!context->system_ops || !context->system_ops->reboot)
|
||||
return 1;
|
||||
|
||||
return context->system_ops->reboot();
|
||||
}
|
||||
|
|
|
@ -308,6 +308,7 @@ struct lws_context {
|
|||
time_t next_cull;
|
||||
#endif
|
||||
|
||||
const lws_system_ops_t *system_ops;
|
||||
void *external_baggage_free_on_destroy;
|
||||
const struct lws_token_limits *token_limits;
|
||||
void *user_space;
|
||||
|
|
Loading…
Add table
Reference in a new issue