dbus-daemon: send signals on connection overflow
[platform/upstream/dbus.git] / tools / tool-common.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* tool-common - common functionality for dbus-test-tool modules
3  *
4  * Copyright © 2003 Philip Blundell <philb@gnu.org>
5  * Copyright © 2011 Nokia Corporation
6  * Copyright © 2014 Collabora Ltd.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25 #include "tool-common.h"
26
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32
33 #ifdef DBUS_WIN
34 #include <io.h>
35 #include <windows.h>
36 #else
37 #include <unistd.h>
38 #endif
39
40 void
41 tool_oom (const char *doing)
42 {
43   fprintf (stderr, "OOM while %s\n", doing);
44   exit (1);
45 }
46
47 #ifdef DBUS_WIN
48 typedef int WriteResult;
49 #define write(fd, buf, len) _write(fd, buf, len)
50 #else
51 typedef ssize_t WriteResult;
52 #endif
53
54 dbus_bool_t
55 tool_write_all (int fd,
56     const void *buf,
57     size_t size)
58 {
59   const char *p = buf;
60   size_t bytes_written = 0;
61
62   while (size > bytes_written)
63     {
64       WriteResult res = write (fd, p, size - bytes_written);
65
66       if (res < 0)
67         {
68           if (errno == EINTR)
69             continue;
70           else
71             return FALSE;
72         }
73       else
74         {
75           size_t this_time = (size_t) res;
76           p += this_time;
77           bytes_written += this_time;
78         }
79     }
80
81   return TRUE;
82 }