#ifndef SERVICE_PROCESS_H
#define SERVICE_PROCESS_H

struct service_process {
	struct service_process *prev, *next;
	struct service *service;
	int refcount;

	pid_t pid;
        /* uid is used to check for old/invalid status messages */
	unsigned int uid;

	/* number of new connections process is currently accepting */
	unsigned int available_count;
	/* Approximate number of connections process has ever accepted.
	   This isn't exact, because its calculation is based on
	   available_count updates, which aren't done on every single
	   connection/disconnection. With a busy process it might be a lot
	   smaller than the correct value. */
	unsigned int total_count;

	/* Process index number. This is set only for services with
	   inet_listener_reuse_port=yes listeners. See
	   service_listener.reuse_port_process_index for how this is used. */
	unsigned int index;

	/* Timestamp when the process was created */
	time_t create_time;
	/* Time when process started idling, or 0 if we're not idling. This is
	   updated when the process sends a notification via its status pipe
	   about the number of clients it is processing.

	   This field also determines whether the process is in the service's
	   "busy" or "idle" processes linked list. */
	time_t idle_start;
	/* Timeout for processing idle-kill SIGINT. Either the process will die
	   or it sends a notification about not being idling anymore */
	struct timeout *to_idle_kill;

	/* time when we last received a status update */
	time_t last_status_update;
	/* time when we last sent SIGINT to process */
	time_t last_kill_sent;

	/* kill the process if it doesn't send initial status notification */
	struct timeout *to_status;

	/* restart_request_count was reached for a process whose service has
	   client_limit > 1. The process is just handling its
	   last connections until it dies. Retired processes are not counted
	   towards the service's process_limit.

	   The intention here is to allow configuring a (login) service with
	   process_limit = process_min_avail and restart_request_count set to
	   something else than unlimited. If the process_limit was strictly
	   enforced, long lived connections would prevent new processes from
	   being launched, eventually causing all processes to keep handling
	   existing connections without allowing new ones. */
	bool retired:1;
	bool destroyed:1;
};

#define SERVICE_PROCESS_IS_INITIALIZED(process) \
	((process)->to_status == NULL)

struct service_process *
service_process_create(struct service *service, int accepted_fd,
		       const struct service_listener *accepted_listener);
void service_process_destroy(struct service_process *process);

void service_process_ref(struct service_process *process);
void service_process_unref(struct service_process *process);

void service_process_log_status_error(struct service_process *process,
				      int status);

#endif
