+void open_logbuffer(void) {
+ /* Reserve 1% of the RAM for the logfile, but at max 25 MiB.
+ * For 512 MiB of RAM this will lead to a 5 MiB log buffer.
+ * At the moment (2011-12-10), no testcase leads to an i3 log
+ * of more than ~ 600 KiB. */
+ logbuffer_size = min(physical_mem_bytes * 0.01, shmlog_size);
+#if defined(__FreeBSD__)
+ sasprintf(&shmlogname, "/tmp/i3-log-%d", getpid());
+#else
+ sasprintf(&shmlogname, "/i3-log-%d", getpid());
+#endif
+ logbuffer_shm = shm_open(shmlogname, O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
+ if (logbuffer_shm == -1) {
+ fprintf(stderr, "Could not shm_open SHM segment for the i3 log: %s\n", strerror(errno));
+ return;
+ }
+
+#if defined(__OpenBSD__) || defined(__APPLE__)
+ if (ftruncate(logbuffer_shm, logbuffer_size) == -1) {
+ fprintf(stderr, "Could not ftruncate SHM segment for the i3 log: %s\n", strerror(errno));
+#else
+ int ret;
+ if ((ret = posix_fallocate(logbuffer_shm, 0, logbuffer_size)) != 0) {
+ fprintf(stderr, "Could not ftruncate SHM segment for the i3 log: %s\n", strerror(ret));
+#endif
+ close(logbuffer_shm);
+ shm_unlink(shmlogname);
+ return;
+ }
+
+ logbuffer = mmap(NULL, logbuffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, logbuffer_shm, 0);
+ if (logbuffer == MAP_FAILED) {
+ close_logbuffer();
+ fprintf(stderr, "Could not mmap SHM segment for the i3 log: %s\n", strerror(errno));