summaryrefslogtreecommitdiffstats
path: root/include/clplumbing/proctrack.h
blob: 975ff1ba664f7e3aee1d99682df8bf43c12b8eda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Process tracking object.
 *
 * Copyright (c) 2002 International Business Machines
 * Author:	Alan Robertson <alanr@unix.sh>
 *
 * 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; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef _PROCTRACK_H
#	define _PROCTRACK_H
#include <sys/types.h>
#include <sys/times.h>
#include <clplumbing/longclock.h>

/*
 * We track processes, mainly so we can do something appropriate
 * when they die, and find processes should we need to kill them...
 */

typedef struct _ProcTrack	ProcTrack;
typedef struct _ProcTrack_ops	ProcTrack_ops;
typedef struct _ProcTrackKillInfo	ProcTrackKillInfo;

/*
 * The levels of logging possible for our process
 */
enum _ProcTrackLogType {
	PT_LOGNONE = 2,		/* Exits never automatically logged */
	PT_LOGNORMAL,		/* Automatically log abnormal exits */
	PT_LOGVERBOSE		/* Automatically log every exit */
};
typedef enum _ProcTrackLogType	ProcTrackLogType;

#define proctrack_pid(p) (p)->pid
#define proctrack_data(p) (p)->privatedata
#define reset_proctrack_data(p) (p)->privatedata = NULL
#define proctrack_timedout(p) ((p)->timeoutseq > 0)

struct _ProcTrack {
	pid_t			pid;
	int			isapgrp;
	ProcTrackLogType	loglevel;
	void *			privatedata;
	ProcTrack_ops*		ops;

	longclock_t		startticks;
	TIME_T			starttime;
	unsigned		timerid;
	int			timeoutseq;
	ProcTrackKillInfo*	killinfo;
};

/*
 * The set of operations to perform on our tracked processes.
 */
struct _ProcTrack_ops {

	/* Called when a process dies */
	void 	(*procdied)		
		(ProcTrack* p, int status, int signo, int exitcode
		,	int waslogged);

	/* Called when a process registers */
	void	(*procregistered)
		(ProcTrack*p);

	/* Returns a "name" for a process (for messages) */
	/* (may have to be copied, because it may be a static value) */
	const char *
		(*proctype)
		(ProcTrack* p);
};

struct _ProcTrackKillInfo {
	long	mstimeout;	/* Timeout in milliseconds */
	int	signalno;	/* Signal number to issue @ timeout */
};

/* A function for calling by the process table iterator */
typedef void (*ProcTrackFun) (ProcTrack* p, void * data);

/* Call this function to activate the procdied member function */
/* Returns TRUE if 'pid' was registered */
int ReportProcHasDied(int pid, int status);

/* Create/Log a new tracked process */
void NewTrackedProc(pid_t pid, int isapgrp, ProcTrackLogType loglevel
,	void * privatedata , ProcTrack_ops* ops);

/* "info" is 0-terminated (terminated by a 0 signal) */
int SetTrackedProcTimeouts(pid_t pid, ProcTrackKillInfo* info);
void RemoveTrackedProcTimeouts(pid_t pid);

/* Return information associated with the given PID (or NULL) */
ProcTrack* GetProcInfo(pid_t pid);

/*
 * Iterate over the set of tracked processes.
 * If proctype is NULL, then walk through them all, otherwise only those
 * of the given type ("f")
 */
void	ForEachProc(ProcTrack_ops* proctype, ProcTrackFun f, void * data);

void	DisableProcLogging(void);	/* Useful for shutdowns */
void	EnableProcLogging(void);
#endif