diff --git a/include/libwebsockets.h b/include/libwebsockets.h index 80ce71911..9ca1c952e 100644 --- a/include/libwebsockets.h +++ b/include/libwebsockets.h @@ -1,7 +1,7 @@ /* * libwebsockets - small server side websockets and web server implementation * - * Copyright (C) 2010-2018 Andy Green + * Copyright (C) 2010-2019 Andy Green * * 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 #include #include #include diff --git a/include/libwebsockets/lws-context-vhost.h b/include/libwebsockets/lws-context-vhost.h index b9205ae40..de1248c13 100644 --- a/include/libwebsockets/lws-context-vhost.h +++ b/include/libwebsockets/lws-context-vhost.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 diff --git a/include/libwebsockets/lws-system.h b/include/libwebsockets/lws-system.h new file mode 100644 index 000000000..6ad2f7ac5 --- /dev/null +++ b/include/libwebsockets/lws-system.h @@ -0,0 +1,84 @@ +/* + * libwebsockets - small server side websockets and web server implementation + * + * Copyright (C) 2010-2019 Andy Green + * + * 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); diff --git a/lib/core/context.c b/lib/core/context.c index e5a4553f9..f4887b9a9 100644 --- a/lib/core/context.c +++ b/lib/core/context.c @@ -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)) diff --git a/lib/core/libwebsockets.c b/lib/core/libwebsockets.c index 718f17c5f..c425cdc86 100644 --- a/lib/core/libwebsockets.c +++ b/lib/core/libwebsockets.c @@ -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(); +} diff --git a/lib/core/private.h b/lib/core/private.h index 741b5c20d..196b43a08 100644 --- a/lib/core/private.h +++ b/lib/core/private.h @@ -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;