-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpshm_write.c
46 lines (37 loc) · 1.65 KB
/
pshm_write.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2024. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 or (at your option) any *
* later version. This program is distributed without any warranty. See *
* the file COPYING.gpl-v3 for details. *
\*************************************************************************/
/* Listing 54-2 */
#include <fcntl.h>
#include <sys/mman.h>
#include "tlpi_hdr.h"
int
main(int argc, char *argv[])
{
int fd;
size_t len; /* Size of shared memory object */
char *addr;
if (argc != 3 || strcmp(argv[1], "--help") == 0)
usageErr("%s shm-name string\n", argv[0]);
fd = shm_open(argv[1], O_RDWR, 0); /* Open existing object */
if (fd == -1)
errExit("shm_open");
len = strlen(argv[2]);
if (ftruncate(fd, len) == -1) /* Resize object to hold string */
errExit("ftruncate");
printf("Resized to %ld bytes\n", (long) len);
addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED)
errExit("mmap");
if (close(fd) == -1) /* 'fd' is no longer needed */
errExit("close");
printf("copying %ld bytes\n", (long) len);
memcpy(addr, argv[2], len); /* Copy string to shared memory */
exit(EXIT_SUCCESS);
}