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>
25 #include "dconf-shm.h"
33 dconf_shm_get_shmdir (void)
37 if (g_once_init_enter (&shmdir
))
38 g_once_init_leave (&shmdir
, g_build_filename (g_get_user_runtime_dir (), "dconf", NULL
));
44 dconf_shm_close (guint8
*shm
)
51 dconf_shm_open (const gchar
*name
)
58 shmdir
= dconf_shm_get_shmdir ();
59 filename
= g_build_filename (shmdir
, name
, NULL
);
63 if (g_mkdir_with_parents (shmdir
, 0700) != 0)
65 g_critical ("unable to create directory '%s': %s. dconf will not work properly.", shmdir
, g_strerror (errno
));
69 fd
= open (filename
, O_RDWR
| O_CREAT
, 0600);
72 g_critical ("unable to create file '%s': %s. dconf will not work properly.", filename
, g_strerror (errno
));
76 /* ftruncate(fd, 1) is not sufficient because it does not actually
77 * ensure that the space is available (which could give a SIGBUS
80 * posix_fallocate() is also problematic because it is implemented in
81 * a racy way in the libc if unavailable for a particular filesystem
82 * (as is the case for tmpfs, which is where we probably are).
84 * By writing to the second byte in the file we ensure we don't
85 * overwrite the first byte (which is the one we care about).
87 if (pwrite (fd
, "", 1, 1) != 1)
89 g_critical ("failed to allocate file '%s': %s. dconf will not work properly.", filename
, g_strerror (errno
));
93 memory
= mmap (NULL
, 1, PROT_READ
, MAP_SHARED
, fd
, 0);
94 g_assert (memory
!= MAP_FAILED
);
95 g_assert (memory
!= NULL
);
105 dconf_shm_flag (const gchar
*name
)
111 shmdir
= dconf_shm_get_shmdir ();
112 filename
= g_build_filename (shmdir
, name
, NULL
);
114 /* We need O_RDWR for PROT_WRITE.
116 * This is probably due to the fact that some architectures can't make
117 * write-only mappings (so they end up being readable as well).
119 fd
= open (filename
, O_RDWR
);
122 /* In theory we could have opened the file after a client created
123 * it but before they called pwrite(). Do the pwrite() ourselves
124 * to make sure (so we don't get SIGBUS in a moment).
126 * If this fails then it will probably fail for the client too.
127 * If it doesn't then there's not really much we can do...
129 if (pwrite (fd
, "", 1, 1) == 1)
133 /* It would have been easier for us to do write(fd, "\1", 1);
134 * but this causes problems on kernels (ie: OpenBSD) that
135 * don't sync up their filesystem cache with mmap()ed regions.
137 * Using mmap() works everywhere.
139 * See https://bugzilla.gnome.org/show_bug.cgi?id=687334 about
140 * why we need to have PROT_READ even though we only write.
142 shm
= mmap (NULL
, 1, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
143 g_assert (shm
!= MAP_FAILED
);