Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lassik committed Apr 16, 2019
0 parents commit 17c6cd5
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
BasedOnStyle: WebKit
TabWidth: 8
IndentWidth: 8
UseTab: ForIndentation
AlwaysBreakAfterReturnType: None
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
tab_width = 8

[*.c]
indent_size = 8
indent_style = tab
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
/test_shared
13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2019 Lassi Kortela

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6 changes: 6 additions & 0 deletions build-macos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
set -eux
${CC:-clang} -Wall -Wextra -pedantic -std=c99 -c shm_open_anon.c
${CC:-clang} -Wall -Wextra -pedantic -std=c99 -c test_shared.c
${CC:-clang} -Wall -Wextra -pedantic -std=c99 \
-o test_shared shm_open_anon.c test_shared.c
138 changes: 138 additions & 0 deletions shm_open_anon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2019 Lassi Kortela
// SPDX-License-Identifier: ISC

#include <sys/types.h>

#include <sys/mman.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

//

#undef IMPL_CLASSIC
#undef IMPL_DEV_SHM
#undef IMPL_SHM_ANON
#undef IMPL_SHM_MKSTEMP

#ifdef __APPLE__
#ifdef __MACH__
#define IMPL_CLASSIC
#endif
#endif

#ifdef __NetBSD__
#define IMPL_CLASSIC
#endif

#ifdef __DragonFly__
#define IMPL_CLASSIC
#endif

#ifdef __linux__
#define IMPL_DEV_SHM
#endif

#ifdef __FreeBSD__
#define IMPL_SHM_ANON
#endif

#ifdef __OpenBSD__
#define IMPL_SHM_MKSTEMP
#endif

//

static int save_errno_and_close(int fd)
{
int save;

save = errno;
close(fd);
errno = save;
return -1;
}

static int fd_without_close_on_exec(int fd)
{
int flags;

if ((flags = fcntl(fd, F_GETFD)) == -1)
return save_errno_and_close(fd);
flags &= ~FD_CLOEXEC;
if (fcntl(fd, F_SETFD, flags) == -1)
return save_errno_and_close(fd);
return fd;
}

//

#ifdef IMPL_CLASSIC
int shm_open_anon_private(void)
{
char name[16];
int fd;

snprintf(name, sizeof(name), "/tmp/XXXXXXXX");
if (mktemp(name) == NULL)
return -1;
if ((fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600)) == -1)
return -1;
if (shm_unlink(name) == -1)
return save_errno_and_close(fd);
return fd;
}
#endif

//

#ifdef IMPL_DEV_SHM
int shm_open_anon_private(void)
{
char name[16];
int fd;

snprintf(name, sizeof(name), "/dev/shm/XXXXXXXX");
if ((fd = mkostemp(name, O_CLOEXEC)) == -1)
return -1;
if (shm_unlink(name) == -1)
return save_errno_and_close(fd);
return fd;
}
#endif

//

#ifdef IMPL_SHM_MKSTEMP
int shm_open_anon_private(void)
{
char name[16];
int fd;

snprintf(name, sizeof(name), "/tmp/XXXXXXXX");
if ((fd = shm_mkstemp(name)) == -1)
return -1;
if (shm_unlink(name) == -1)
return save_errno_and_close(fd);
return fd;
}
#endif

//

#ifdef IMPL_SHM_ANON
int shm_open_anon_private(void)
{
return shm_open(SHM_ANON, 0, 0);
}
#endif

//

int shm_open_anon_shared(void)
{
return fd_without_close_on_exec(shm_open_anon_private());
}
2 changes: 2 additions & 0 deletions shm_open_anon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int shm_open_anon_private(void);
int shm_open_anon_shared(void);
97 changes: 97 additions & 0 deletions test_shared.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <sys/types.h>

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "shm_open_anon.h"

#define PROGNAME "test_shared"

static void
diesys(const char *msg)
{
fprintf(stderr, "%s: %s\n", msg, strerror(errno));
exit(1);
}

static void *
map_shared_memory_from_fd(int fd, size_t *out_size)
{
struct stat st;
void *buf;
size_t size;

if (fstat(fd, &st) == -1) {
diesys("fstat");
}
*out_size = size = (size_t)st.st_size;
buf = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
diesys("mmap");
}
return buf;
}

static void
parent(void)
{
char *child_args[3] = {"./" PROGNAME, "child", 0};
char *buf;
size_t bufsize;
pid_t child;
int fd, status;

bufsize = 10 * 1024 * 1024;
if ((fd = shm_open_anon_shared()) == -1)
diesys("shm_open_anon_shared");
if (ftruncate(fd, (off_t)bufsize) == -1)
diesys("ftruncate");
buf = map_shared_memory_from_fd(fd, &bufsize);
memset(buf, 0, bufsize);
snprintf(buf, bufsize, "hello from parent");
if ((child = fork()) == -1)
diesys("fork");
if (!child) {
if (dup2(fd, 3) == -1)
diesys("dup2");
execv(child_args[0], child_args);
diesys("execv");
}
if (waitpid(child, &status, 0) == -1)
diesys("cannot wait for child");
printf("Parent: %s\n", buf);
}

static void
child(void)
{
char *buf;
size_t bufsize;

buf = map_shared_memory_from_fd(3, &bufsize);
printf("Child: %s\n", buf);
snprintf(buf, bufsize, "hello from child");
}

int
main(int argc, char **argv)
{
(void)argv;
if (argc == 1) {
parent();
} else if ((argc == 2) && !strcmp(argv[1], "child")) {
child();
} else {
fprintf(stderr, "usage\n");
exit(1);
}
return 0;
}

0 comments on commit 17c6cd5

Please sign in to comment.