summaryrefslogtreecommitdiffstats
path: root/maildrop/funcs.C
blob: d25571d3d059f75fe416386c2d815b0ef8b529bb (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include	"funcs.h"
#include	"buffer.h"
#include	"xconfig.h"
#include	"varlist.h"
#include	"maildrop.h"
#include	"config.h"
#include	<string.h>
#include	<signal.h>
#include	<grp.h>
#if	HAVE_MEMORY_H
#include	<memory.h>
#endif
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif

#ifdef _AIX
#undef	GETPGRP_VOID
#define	GETPGRP_VOID
#endif

#if	HAS_GETHOSTNAME
#else
extern "C" int gethostname(const char *, size_t);
#endif


void	memorycopy(void *dst, void *src, int cnt)
{
	if (cnt <= 0)	return;
	memmove(dst, src, cnt);
}

void	outofmem()
{
	throw "Out of memory.";
}

void	seekerr()
{
	throw "Seek error.";
}

const char *TempName(const char *dir, unsigned l)
{
static Buffer buf;
static unsigned counter=0;
char	hostname[256];

	hostname[0]=0;
	gethostname(hostname, 256);
	hostname[sizeof(hostname)-1]=0;

	buf=dir;
	if (l > 0)	buf.Length(l);
	buf.append( (unsigned long)getpid() );
	buf += '.';
	buf.append( (unsigned long)counter++ );
	buf += '.';
	buf += hostname;
	buf += '\0';

	return (buf);
}

int backslash_char(int c)
{
	switch (c)	{
	case 'r':
		return '\r';
	case 'n':
		return '\n';
	case 't':
		return '\t';
	case 'f':
		return '\f';
	case 'v':
		return '\v';
	}
	return (c);
}

///////////////////////////////////////////////////////////////////////////
//
// V0.55a - implement process group kill.  Upon startup, set our process
// group.  Upon exit, kill all processes in our process group.
//
// Functions: setprocgroup() - sets our process group.
//            killprocgroup() - kills our process group.
//
///////////////////////////////////////////////////////////////////////////

static int procgroup_set=0;

void setprocgroup()
{
#if	HAS_SETPGRP

	(void)setpgrp();
	procgroup_set=1;
#else
#if	HAS_SETPGID
	(void)setpgid(0,0);
	procgroup_set=1;
#endif
#endif
}

static GETGROUPS_T getprocgroup()
{
#if	HAS_GETPGRP

#ifdef	GETPGRP_VOID

	return ( getpgrp() );
#else
	return ( getpgrp( getpid()) );
#endif
#else
#if	HAS_GETPGID
	return ( getpgid( 0 ) );
#else
	return ( getpid());		// Shouldn't happen
#endif
#endif
}

void killprocgroup()
{
	if (!procgroup_set)	return;

	signal(SIGHUP, SIG_IGN);	// Don't kill this process

#if	HAS_SETPGRP

	(void)kill( -getprocgroup(), SIGHUP );

#else
#if	HAS_SETPGID
	(void)kill( -getprocgroup(), SIGHUP );
#endif
#endif
}