1 // SPDX-License-Identifier: CDDL-1.0 2 /* 3 * CDDL HEADER START 4 * 5 * The contents of this file are subject to the terms of the 6 * Common Development and Distribution License (the "License"). 7 * You may not use this file except in compliance with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or https://opensource.org/licenses/CDDL-1.0. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2006 Ricardo Correia. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #include <unistd.h> 28 #include <stdio.h> 29 #include <errno.h> 30 #include <stdlib.h> 31 #include <limits.h> 32 #include <string.h> 33 34 #include <zone.h> 35 36 zoneid_t 37 getzoneid(void) 38 { 39 char path[PATH_MAX]; 40 char buf[128] = { '\0' }; 41 char *cp; 42 43 int c = snprintf(path, sizeof (path), "/proc/self/ns/user"); 44 /* This API doesn't have any error checking... */ 45 if (c < 0 || c >= sizeof (path)) 46 return (GLOBAL_ZONEID); 47 48 ssize_t r = readlink(path, buf, sizeof (buf) - 1); 49 if (r < 0) 50 return (GLOBAL_ZONEID); 51 52 cp = strchr(buf, '['); 53 if (cp == NULL) 54 return (GLOBAL_ZONEID); 55 cp++; 56 57 unsigned long n = strtoul(cp, NULL, 10); 58 if (n == ULONG_MAX && errno == ERANGE) 59 return (GLOBAL_ZONEID); 60 zoneid_t z = (zoneid_t)n; 61 62 return (z); 63 } 64