}
+/* This retrieves variable <name> from variables <vars>, and if found,
+ * duplicates the result into sample <smp>. smp_dup() is used in order to
+ * release the variables lock ASAP (so a pre-allocated chunk is obtained
+ * via get_trash_shunk()). The variables' lock is used for reads.
+ *
+ * The function returns 0 if the variable was not found, otherwise 1
+ * with the sample filled.
+ */
+static int var_to_smp(struct vars *vars, const char *name, struct sample *smp)
+{
+ struct var *var;
+
+ /* Get the variable entry. */
+ HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock);
+ var = var_get(vars, name);
+ if (!var) {
+ HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
+ return 0;
+ }
+
+ /* Copy sample. */
+ smp->data = var->data;
+ smp_dup(smp);
+
+ HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
+ return 1;
+}
+
/* This function fills a sample with the variable content.
*
* Keep in mind that a sample content is duplicated by using smp_dup()
int vars_get_by_name(const char *name, size_t len, struct sample *smp)
{
struct vars *vars;
- struct var *var;
enum vars_scope scope;
/* Resolve name and scope. */
if (!vars || vars->scope != scope)
return 0;
- /* Get the variable entry. */
- HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock);
- var = var_get(vars, name);
- if (!var) {
- HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
- return 0;
- }
-
- /* Copy sample. */
- smp->data = var->data;
- smp_dup(smp);
-
- HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
- return 1;
+ return var_to_smp(vars, name, smp);
}
/* This function fills a sample with the content of the variable described
int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp)
{
struct vars *vars;
- struct var *var;
/* Select "vars" pool according with the scope. */
vars = get_vars(smp->sess, smp->strm, var_desc->scope);
if (!vars || vars->scope != var_desc->scope)
return 0;
- /* Get the variable entry. */
- HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock);
- var = var_get(vars, var_desc->name);
- if (!var) {
- HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
- return 0;
- }
-
- /* Copy sample. */
- smp->data = var->data;
- smp_dup(smp);
-
- HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock);
- return 1;
+ return var_to_smp(vars, var_desc->name, smp);
}
/* Always returns ACT_RET_CONT even if an error occurs. */