Created
January 14, 2022 21:57
-
-
Save adsr/3913e90860aa49858ce98440c7274aa6 to your computer and use it in GitHub Desktop.
Program to dump/cat shm segment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc -Wall -pedantic -g -std=c99 shmcat.c -o shmcat */ | |
#define _XOPEN_SOURCE | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/shm.h> | |
int main(int argc, char **argv) { | |
if (argc < 2) { | |
fprintf(stderr, "Usage: shmcat <shmid>\n"); | |
return 1; | |
} | |
int shmid = atoi(argv[1]); | |
struct shmid_ds buf = {{0}}; | |
if (shmctl(shmid, IPC_STAT, &buf) < 0) { | |
perror("shmctl"); | |
return 1; | |
} | |
int nbytes = buf.shm_segsz; | |
if (nbytes > 0) { | |
void *shm = shmat(shmid, NULL, SHM_RDONLY); | |
if (shm == (void *)-1) { | |
perror("shmat"); | |
return 1; | |
} | |
ssize_t rv = write(STDOUT_FILENO, shm, nbytes); | |
if (rv < 0) { | |
perror("write"); | |
return 1; | |
} else if (rv != nbytes) { | |
fprintf(stderr, "shmcat: Partial write of %zd/%d byte(s) to stdout\n", rv, nbytes); | |
return 1; | |
} | |
shmdt(shm); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment