Imported Upstream version 2.48.0
[platform/upstream/glib.git] / gio / tests / dbus-launch.c
1 /*
2  * Mock version of dbus-launch, for gdbus-unix-addresses test
3  *
4  * Copyright © 2015 Collabora Ltd.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <glib.h>
21
22 #ifndef G_OS_UNIX
23 #error This is a Unix-specific test helper
24 #endif
25
26 #include <errno.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31 #define ME "GDBus mock version of dbus-launch"
32
33 static void
34 write_all (const void *ptr,
35            size_t      len)
36 {
37   const char *p = ptr;
38
39   while (len > 0)
40     {
41       ssize_t done = write (STDOUT_FILENO, p, len);
42
43       if (done == 0)
44         {
45           g_error ("%s: write: EOF", ME);
46         }
47       else if (done < 0)
48         {
49           if (errno == EINTR)
50             continue;
51
52           g_error ("%s: write: %s", ME, g_strerror (errno));
53         }
54       else
55         {
56           if (len < (size_t) done)
57             g_error ("%s: wrote too many bytes?", ME);
58
59           len -= done;
60           p += done;
61         }
62     }
63 }
64
65 int
66 main (int   argc,
67       char *argv[])
68 {
69   pid_t pid = 0x2323;
70   long window_id = 0x42424242;
71   const char *addr = "hello:this=address-is-from-the,mock=dbus-launch";
72
73   write_all (addr, strlen (addr) + 1);
74   write_all (&pid, sizeof (pid));
75   write_all (&window_id, sizeof (window_id));
76   return 0;
77 }