summaryrefslogtreecommitdiff
path: root/lib/selfpipe.cc
blob: 83b390c04242bd9bfa4c21e827f9367cd36c9fad (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
// nullmailer -- a simple relay-only MTA
// Copyright (C) 2017  Bruce Guenter <bruce@untroubled.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact me at <bruce@untroubled.org>.  There is also a mailing list
// available to discuss this package.  To subscribe, send an email to
// <nullmailer-subscribe@lists.untroubled.org>.

#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include "selfpipe.h"

static int fds[2] = { -1, -1 };

static int fcntl_fl_on(int fd, int flag)
{
  int flags;
  int newflags;
  if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
    return 0;
  if ((newflags = flags | flag) != flags)
    if (fcntl(fd, F_SETFL, newflags))
      return 0;
  return 1;
}

selfpipe::selfpipe()
{
  if (fds[0] < 0) {
    if (pipe(fds) != -1) {
      if (fcntl_fl_on(fds[0], O_NONBLOCK)
	  && fcntl_fl_on(fds[1], O_NONBLOCK)
	  && fcntl_fl_on(fds[1], FD_CLOEXEC)
	  && fcntl_fl_on(fds[1], FD_CLOEXEC))
	return;

      close(fds[0]);
      close(fds[1]);
    }
    fds[0] = fds[1] = -1;
  }
}

selfpipe::operator bool() const
{
  return fds[0] >= 0;
}

static void catcher(int sig)
{
  signal(sig, catcher);
  write(fds[1], &sig, sizeof sig);
}

void selfpipe::catchsig(int sig)
{
  signal(sig, catcher);
}

int selfpipe::caught()
{
  int buf;
  if (read(fds[0], &buf, sizeof buf) == sizeof buf)
    return buf;
  return 0;
}

int selfpipe::waitsig(int timeout)
{
  if (timeout > 0) {
    fd_set fdset;
    FD_ZERO(&fdset);
    FD_SET(fds[0], &fdset);
    struct timeval tv;
    tv.tv_sec = timeout;
    tv.tv_usec = 0;
    int s;
    while ((s = select(fds[0] + 1, &fdset, 0, 0,
		     (timeout <= 0) ? 0 : &tv)) == -1) {
      if (errno != EINTR)
        return -1;
    }
    if (s != 1)
      return 0;
  }
  return caught();
}