2 * Copyright © 2010 Codethink Limited
3 * Copyright © 2012 Canonical Limited
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Ryan Lortie <desrt@desrt.ca>
23 #include "dconf-shm.h"
31 dconf_shm_get_shmdir (void)
35 if (g_once_init_enter (&shmdir
))
36 g_once_init_leave (&shmdir
, g_build_filename (g_get_user_runtime_dir (), "dconf", NULL
));
42 dconf_shm_close (guint8
*shm
)
49 dconf_shm_open (const gchar
*name
)
56 shmdir
= dconf_shm_get_shmdir ();
57 filename
= g_build_filename (shmdir
, name
, NULL
);
61 if (g_mkdir_with_parents (shmdir
, 0700) != 0)
63 g_critical ("unable to create directory '%s': %s. dconf will not work properly.", shmdir
, g_strerror (errno
));
67 fd
= open (filename
, O_RDWR
| O_CREAT
, 0600);
70 g_critical ("unable to create file '%s': %s. dconf will not work properly.", filename
, g_strerror (errno
));
74 /* fruncate(fd, 1) is not sufficient because it does not actually
75 * ensure that the space is available (which could give a SIGBUS
78 * posix_fallocate() is also problematic because it is implemented in
79 * a racy way in the libc if unavailable for a particular filesystem
80 * (as is the case for tmpfs, which is where we probably are).
82 * By writing to the second byte in the file we ensure we don't
83 * overwrite the first byte (which is the one we care about).
85 if (pwrite (fd
, "", 1, 1) != 1)
87 g_critical ("failed to allocate file '%s': %s. dconf will not work properly.", filename
, g_strerror (errno
));
91 memory
= mmap (NULL
, 1, PROT_READ
, MAP_SHARED
, fd
, 0);
92 g_assert (memory
!= MAP_FAILED
);
93 g_assert (memory
!= NULL
);
103 dconf_shm_flag (const gchar
*name
)
109 shmdir
= dconf_shm_get_shmdir ();
110 filename
= g_build_filename (shmdir
, name
, NULL
);
112 /* We need O_RDWR for PROT_WRITE.
114 * This is probably due to the fact that some architectures can't make
115 * write-only mappings (so they end up being readable as well).
117 fd
= open (filename
, O_RDWR
);
120 /* In theory we could have opened the file after a client created
121 * it but before they called pwrite(). Do the pwrite() ourselves
122 * to make sure (so we don't get SIGBUS in a moment).
124 * If this fails then it will probably fail for the client too.
125 * If it doesn't then there's not really much we can do...
127 if (pwrite (fd
, "", 1, 1) == 1)
131 /* It would ahve been easier for us to do write(fd, "\1", 1);
132 * but this causes problems on kernels (ie: OpenBSD) that
133 * don't sync up their filesystem cache with mmap()ed regions.
135 * Using mmap() works everywhere.
137 * See https://bugzilla.gnome.org/show_bug.cgi?id=687334 about
138 * why we need to have PROT_READ even though we only write.
140 shm
= mmap (NULL
, 1, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
141 g_assert (shm
!= MAP_FAILED
);