mirror of
https://github.com/haproxy/haproxy.git
synced 2026-02-12 23:33:14 -05:00
Instead of using existing fields and having to put keyword-specific
contexts in the applet definition, let's have the appctx provide a
generic storage area that's currently large enough for existing CLI
commands and small applets, and a function to allocate that storage.
The function will be responsible for verifying that the requested size
fits in the area so that the caller doesn't need to add specific checks;
it is validated during development as this size is static and will
not change at runtime. In addition the caller doesn't even need to
free() the area since it's part of an existing context. For the
caller's convenience, a context pointer "svcctx" for the command is
also provided so that the allocated area can be placed there (or
possibly any other one in case a larger area is needed).
The struct's layout has been temporarily complicated by adding one
level of anonymous union on top of the "ctx" one. This will allow us
to preserve "ctx" during 2.6 for compatibility with possible external
code and get rid of it in 2.7. This explains why the diff extends to
the whole "ctx" union, but a "git show -b" shows that only one extra
layer was added. In order to make both the svcctx pointer and its
storage accessible without further enlarging the appctx structure,
both svcctx and the storage share the same storage as the ctx part.
This is done by having them placed in the union with a protected
overlapping area for svcctx, for which a shadow member is also
present in the storage area:
union {
void* svcctx; // variable accessed by services
struct {
void *shadow; // shadow of svcctx;
char storage[]; // where most services store their data
};
union { // older commands store here and ignore svcctx
...
} ctx;
};
I.e. new applications will use appctx->svcctx while older ones will be
able to continue to use appctx->ctx.*
The whole area (including the pointer's context) is zeroed before any
applet is initialized, and before CLI keyword processor's first invocation,
as it is an important part of the existing keyword processors, which makes
CLI keywords effectively behave like applets.
85 lines
2.5 KiB
C
85 lines
2.5 KiB
C
/*
|
|
* include/haproxy/applet.h
|
|
* This file contains applet function prototypes
|
|
*
|
|
* Copyright (C) 2000-2015 Willy Tarreau - w@1wt.eu
|
|
*
|
|
* 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
|
|
* exclusively.
|
|
*
|
|
* 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
|
|
*/
|
|
|
|
#ifndef _HAPROXY_APPLET_H
|
|
#define _HAPROXY_APPLET_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <haproxy/api.h>
|
|
#include <haproxy/applet-t.h>
|
|
#include <haproxy/conn_stream.h>
|
|
#include <haproxy/list.h>
|
|
#include <haproxy/pool.h>
|
|
#include <haproxy/task.h>
|
|
|
|
extern unsigned int nb_applets;
|
|
extern struct pool_head *pool_head_appctx;
|
|
|
|
struct task *task_run_applet(struct task *t, void *context, unsigned int state);
|
|
int appctx_buf_available(void *arg);
|
|
void *applet_reserve_svcctx(struct appctx *appctx, size_t size);
|
|
|
|
struct appctx *appctx_new(struct applet *applet, struct cs_endpoint *endp);
|
|
|
|
/* Releases an appctx previously allocated by appctx_new(). */
|
|
static inline void __appctx_free(struct appctx *appctx)
|
|
{
|
|
task_destroy(appctx->t);
|
|
if (LIST_INLIST(&appctx->buffer_wait.list))
|
|
LIST_DEL_INIT(&appctx->buffer_wait.list);
|
|
|
|
BUG_ON(appctx->endp && !(appctx->endp->flags & CS_EP_ORPHAN));
|
|
cs_endpoint_free(appctx->endp);
|
|
pool_free(pool_head_appctx, appctx);
|
|
_HA_ATOMIC_DEC(&nb_applets);
|
|
}
|
|
|
|
static inline void appctx_free(struct appctx *appctx)
|
|
{
|
|
/* The task is supposed to be run on this thread, so we can just
|
|
* check if it's running already (or about to run) or not
|
|
*/
|
|
if (!(appctx->t->state & (TASK_QUEUED | TASK_RUNNING)))
|
|
__appctx_free(appctx);
|
|
else {
|
|
/* if it's running, or about to run, defer the freeing
|
|
* until the callback is called.
|
|
*/
|
|
appctx->state |= APPLET_WANT_DIE;
|
|
task_wakeup(appctx->t, TASK_WOKEN_OTHER);
|
|
}
|
|
}
|
|
|
|
/* wakes up an applet when conditions have changed */
|
|
static inline void appctx_wakeup(struct appctx *appctx)
|
|
{
|
|
task_wakeup(appctx->t, TASK_WOKEN_OTHER);
|
|
}
|
|
|
|
#endif /* _HAPROXY_APPLET_H */
|
|
|
|
/*
|
|
* Local variables:
|
|
* c-indent-level: 8
|
|
* c-basic-offset: 8
|
|
* End:
|
|
*/
|