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