/*-
 * Copyright (c) 2000-2003 Andrey Simonenko
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *   @(#)$Id: ipa_cmd.h,v 1.1.4.1 2006/12/15 16:31:21 simon Exp $
 */

#ifndef IPA_CMD_H
#define IPA_CMD_H

#define CMD_NSIZE	30
#define CMD_NALLOC	20

#define RC_STARTUP	0		/* Any startup {} */
#define RC_SHUTDOWN	1		/* Any shutdown {} */

#ifdef WITH_LIMITS
struct limit;
#endif

#ifdef WITH_THRESHOLDS
struct threshold;
#endif

enum {
#ifdef WITH_LIMITS
	WPID_TYPE_LIMIT,
#endif
#ifdef WITH_SUBLIMITS
	WPID_TYPE_SUBLIMIT,
#endif
#ifdef WITH_THRESHOLDS
	WPID_TYPE_THRESHOLD,
#endif
	WPID_TYPE_NOT_USED
};

/*
 * Wpid ("wait PID") describes one forked process, which runs
 * a list of commands from some section in background.
 */
struct wpid {
	LIST_ENTRY(wpid) hlink;		/* All wpids list in one hash bucket. */
	pid_t		pid;		/* PID of background process. */
	int		type;		/* Selector for next union. */
#ifdef WITH_ANY_LIMITS
	union {
# ifdef WITH_LIMITS
		const struct limit *limit;
# endif
# ifdef WITH_SUBLIMITS
		const struct sublimit *sublimit;
# endif
# ifdef WITH_THRESHOLDS
		const struct threshold *threshold;
# endif
	} u;
#endif /* WITH_ANY_LIMITS */
};

#ifdef WITH_ANY_LIMITS

#ifndef WPID_HASH_BUCKETS
# define  WPID_HASH_BUCKETS 64		/* Must be power of 2. */
#endif

LIST_HEAD(wpid_hash, wpid);

extern struct wpid_hash wpid_hash[];

#define get_wpid_bucket(pid) ((u_int)(pid) & (WPID_HASH_BUCKETS - 1))

extern void	init_wpid_hash(void);
extern int	wpid_hash_is_empty(void);
extern void	rem_wpid_from_hash(struct wpid *);

#endif /* WITH_ANY_LIMITS */

#define CMD_FREE_STR	0x01		/* Free str buffer. */
#define CMD_FREE_USER	0x02		/* Free user buffer. */

/*
 * Structure for "exec" parameter.
 */
struct cmd {
	STAILQ_ENTRY(cmd) link;		/* For list building. */
	char		*str;		/* Command string. */
	char		*user;		/* User name or NULL. */
	u_int		free_mask;	/* CMD_FREE_xxx bits. */
	size_t		str_size;	/* Length of str, including trailing '\0'. */
	u_int		subst_per_cent;	/* Number of "%%" in str. */
	u_int		subst_rule;	/* Number of "%rule%" in str. */
};

/*
 * All commands in one section, which can contain "exec" parameters.
 * By default any startup or shutdown section has sync_exec == 1,
 * any other section has sync_exec == 0.
 */
struct cmd_list {
	int		sync_exec;	/* "sync_exec" parameter. */
	STAILQ_HEAD(, cmd) list;	/* List of all commands. */
};

/*
 * rule|rulepat|autorule { startup|shutdown{}}
 */
struct cmds_rule {
	int	set;			/* 1, if startup|shutdown{} section is specified. */
	struct cmd_list	cmdl;
#ifdef WITH_LIMITS
	struct cmd_list	cmdl_if_all_reached;
	struct cmd_list	cmdl_if_all_not_reached;
	struct cmd_list	cmdl_if_any_reached;
	struct cmd_list	cmdl_if_any_not_reached;
#endif
};

#ifdef WITH_LIMITS
/*
 * limit|sublimit { startup|shutdown{}}}
 */
struct cmds_limit {
	int	set;			/* 1, if startup|shutdown{} section is specified. */
	struct cmd_list	cmdl;
	struct cmd_list	cmdl_if_reached;
	struct cmd_list	cmdl_if_not_reached;
};
#endif

extern int	global_debug_exec;
extern char	*shell_path;
extern char	*shell_path_default;
extern char	*shell_arg1;
extern char	*shell_arg1_default;

extern ipa_mzone *cmd_mzone;

extern ipa_mem_type *m_cmd;

extern struct cmd_list cmds_startup;
extern struct cmd_list cmds_shutdown;

extern const char *const sync_exec_msg[];

extern int	setsuppgids(const char *, gid_t);

struct rule;

extern int	exec_cmd_list(const struct rule *, struct wpid *, const struct cmd_list *, const char *, ...) ATTR_FORMAT(printf, 4, 5);
extern int	exec_cmd_list_cons(const struct cmd_list *, const char *, ...) ATTR_FORMAT(printf, 2, 3);

extern void	init_cmd_list(struct cmd_list *);

extern void	init_cmds_rule(struct cmds_rule *);
extern void	free_cmd_list(struct cmd_list *);
extern void	set_sync_exec_cmds_rule(struct cmds_rule *);

extern int	run_cmds(int);

extern void	free_cmds_rule(struct cmds_rule *);

#ifdef WITH_LIMITS
extern void	init_cmds_limit(struct cmds_limit *);
extern void	free_cmds_limit(struct cmds_limit *);
extern void	set_sync_exec_cmds_limit(struct cmds_limit *);
#endif

struct rule;

extern int	copy_cmd_list(const struct rule *, struct cmd_list *, const struct cmd_list *);
extern int	copy_cmds_rule(const struct rule *, struct cmds_rule *, const struct cmds_rule *);

#ifdef WITH_LIMITS
extern int	copy_cmds_limit(const struct rule *, struct cmds_limit *, const struct cmds_limit *);
#endif

#endif /* !IPA_CMD_H */


syntax highlighted by Code2HTML, v. 0.9.1