1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright (c) 2016 by Delphix. All rights reserved. 14 */ 15 16 /* 17 * Steal memory from the kernel, forcing the ARC to decrease in size, and hold 18 * it until the process receives a signal. 19 */ 20 21 #include <stdio.h> 22 #include <sys/types.h> 23 #include <sys/shm.h> 24 #include <strings.h> 25 #include <stdlib.h> 26 #include <unistd.h> 27 #include <errno.h> 28 29 static void 30 usage(char *progname) 31 { 32 (void) fprintf(stderr, "Usage: %s -f <bytes>\n", progname); 33 exit(1); 34 } 35 36 static void 37 fail(char *err, int rval) 38 { 39 perror(err); 40 exit(rval); 41 } 42 43 static void 44 daemonize(void) 45 { 46 pid_t pid; 47 48 if ((pid = fork()) < 0) { 49 fail("fork", 1); 50 } else if (pid != 0) { 51 (void) fprintf(stdout, "%ld\n", pid); 52 exit(0); 53 } 54 55 (void) setsid(); 56 (void) close(0); 57 (void) close(1); 58 (void) close(2); 59 } 60 61 int 62 main(int argc, char *argv[]) 63 { 64 int c; 65 boolean_t fflag = B_FALSE; 66 char *prog = argv[0]; 67 long long size; 68 char *stroll_leftovers; 69 int shm_id; 70 void *shm_attached; 71 72 while ((c = getopt(argc, argv, "f")) != -1) { 73 switch (c) { 74 /* Run in the foreground */ 75 case 'f': 76 fflag = B_TRUE; 77 break; 78 default: 79 usage(prog); 80 } 81 } 82 83 argc -= optind; 84 argv += optind; 85 86 if (argc != 1) 87 usage(prog); 88 size = strtoll(argv[0], &stroll_leftovers, 10); 89 if (size <= 0) 90 fail("invalid size in bytes", 1); 91 92 if ((shm_id = shmget(IPC_PRIVATE, size, IPC_CREAT|IPC_EXCL)) == -1) 93 fail("shmget", 1); 94 if ((shm_attached = shmat(shm_id, NULL, SHM_SHARE_MMU)) == (void *)-1) 95 fail("shmat", 1); 96 97 if (fflag == B_FALSE) 98 daemonize(); 99 (void) pause(); 100 101 /* NOTREACHED */ 102 return (0); 103 } 104