profiles: allow profiles in XDG_DATA_DIRS
[dconf.git] / shm / dconf-shm.c
blob6faf391e01c65452babc96e46f6290a98c791c2d
1 /*
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 "config.h"
25 #include "dconf-shm.h"
27 #include <sys/mman.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
32 static gchar *
33 dconf_shm_get_shmdir (void)
35 static gchar *shmdir;
37 if (g_once_init_enter (&shmdir))
38 g_once_init_leave (&shmdir, g_build_filename (g_get_user_runtime_dir (), "dconf", NULL));
40 return shmdir;
43 void
44 dconf_shm_close (guint8 *shm)
46 if (shm)
47 munmap (shm, 1);
50 guint8 *
51 dconf_shm_open (const gchar *name)
53 const gchar *shmdir;
54 gchar *filename;
55 void *memory;
56 gint fd;
58 shmdir = dconf_shm_get_shmdir ();
59 filename = g_build_filename (shmdir, name, NULL);
60 memory = NULL;
61 fd = -1;
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));
66 goto out;
69 fd = open (filename, O_RDWR | O_CREAT, 0600);
70 if (fd == -1)
72 g_critical ("unable to create file '%s': %s. dconf will not work properly.", filename, g_strerror (errno));
73 goto out;
76 /* ftruncate(fd, 1) is not sufficient because it does not actually
77 * ensure that the space is available (which could give a SIGBUS
78 * later).
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));
90 goto out;
93 memory = mmap (NULL, 1, PROT_READ, MAP_SHARED, fd, 0);
94 g_assert (memory != MAP_FAILED);
95 g_assert (memory != NULL);
97 out:
98 g_free (filename);
99 close (fd);
101 return memory;
104 void
105 dconf_shm_flag (const gchar *name)
107 const gchar *shmdir;
108 gchar *filename;
109 gint fd;
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);
120 if (fd >= 0)
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)
131 guint8 *shm;
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);
145 *shm = 1;
147 munmap (shm, 1);
150 close (fd);
152 unlink (filename);
155 g_free (filename);