summaryrefslogtreecommitdiff
path: root/shm/dconf-shm.c
blob: 6faf391e01c65452babc96e46f6290a98c791c2d (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
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright © 2010 Codethink Limited
 * Copyright © 2012 Canonical Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

#include "config.h"

#include "dconf-shm.h"

#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

static gchar *
dconf_shm_get_shmdir (void)
{
  static gchar *shmdir;

  if (g_once_init_enter (&shmdir))
    g_once_init_leave (&shmdir, g_build_filename (g_get_user_runtime_dir (), "dconf", NULL));

  return shmdir;
}

void
dconf_shm_close (guint8 *shm)
{
  if (shm)
    munmap (shm, 1);
}

guint8 *
dconf_shm_open (const gchar *name)
{
  const gchar *shmdir;
  gchar *filename;
  void *memory;
  gint fd;

  shmdir = dconf_shm_get_shmdir ();
  filename = g_build_filename (shmdir, name, NULL);
  memory = NULL;
  fd = -1;

  if (g_mkdir_with_parents (shmdir, 0700) != 0)
    {
      g_critical ("unable to create directory '%s': %s.  dconf will not work properly.", shmdir, g_strerror (errno));
      goto out;
    }

  fd = open (filename, O_RDWR | O_CREAT, 0600);
  if (fd == -1)
    {
      g_critical ("unable to create file '%s': %s.  dconf will not work properly.", filename, g_strerror (errno));
      goto out;
    }

  /* ftruncate(fd, 1) is not sufficient because it does not actually
   * ensure that the space is available (which could give a SIGBUS
   * later).
   *
   * posix_fallocate() is also problematic because it is implemented in
   * a racy way in the libc if unavailable for a particular filesystem
   * (as is the case for tmpfs, which is where we probably are).
   *
   * By writing to the second byte in the file we ensure we don't
   * overwrite the first byte (which is the one we care about).
   */
  if (pwrite (fd, "", 1, 1) != 1)
    {
      g_critical ("failed to allocate file '%s': %s.  dconf will not work properly.", filename, g_strerror (errno));
      goto out;
    }

  memory = mmap (NULL, 1, PROT_READ, MAP_SHARED, fd, 0);
  g_assert (memory != MAP_FAILED);
  g_assert (memory != NULL);

 out:
  g_free (filename);
  close (fd);

  return memory;
}

void
dconf_shm_flag (const gchar *name)
{
  const gchar *shmdir;
  gchar *filename;
  gint fd;

  shmdir = dconf_shm_get_shmdir ();
  filename = g_build_filename (shmdir, name, NULL);

  /* We need O_RDWR for PROT_WRITE.
   *
   * This is probably due to the fact that some architectures can't make
   * write-only mappings (so they end up being readable as well).
   */
  fd = open (filename, O_RDWR);
  if (fd >= 0)
    {
      /* In theory we could have opened the file after a client created
       * it but before they called pwrite().  Do the pwrite() ourselves
       * to make sure (so we don't get SIGBUS in a moment).
       *
       * If this fails then it will probably fail for the client too.
       * If it doesn't then there's not really much we can do...
       */
      if (pwrite (fd, "", 1, 1) == 1)
        {
          guint8 *shm;

          /* It would have been easier for us to do write(fd, "\1", 1);
           * but this causes problems on kernels (ie: OpenBSD) that
           * don't sync up their filesystem cache with mmap()ed regions.
           *
           * Using mmap() works everywhere.
           *
           * See https://bugzilla.gnome.org/show_bug.cgi?id=687334 about
           * why we need to have PROT_READ even though we only write.
           */
          shm = mmap (NULL, 1, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
          g_assert (shm != MAP_FAILED);

          *shm = 1;

          munmap (shm, 1);
        }

      close (fd);

      unlink (filename);
    }

  g_free (filename);
}