1eac7052fSMatt Macy /*
2eac7052fSMatt Macy * CDDL HEADER START
3eac7052fSMatt Macy *
4eac7052fSMatt Macy * The contents of this file are subject to the terms of the
5eac7052fSMatt Macy * Common Development and Distribution License (the "License").
6eac7052fSMatt Macy * You may not use this file except in compliance with the License.
7eac7052fSMatt Macy *
8eac7052fSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eac7052fSMatt Macy * See the License for the specific language governing permissions
11eac7052fSMatt Macy * and limitations under the License.
12eac7052fSMatt Macy *
13eac7052fSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eac7052fSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eac7052fSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eac7052fSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eac7052fSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eac7052fSMatt Macy *
19eac7052fSMatt Macy * CDDL HEADER END
20eac7052fSMatt Macy */
21eac7052fSMatt Macy /*
22eac7052fSMatt Macy * Copyright 2006 Ricardo Correia. All rights reserved.
23eac7052fSMatt Macy * Use is subject to license terms.
24eac7052fSMatt Macy */
25eac7052fSMatt Macy
261f1e2261SMartin Matuska #include <unistd.h>
271f1e2261SMartin Matuska #include <stdio.h>
281f1e2261SMartin Matuska #include <errno.h>
291f1e2261SMartin Matuska #include <stdlib.h>
301f1e2261SMartin Matuska #include <limits.h>
311f1e2261SMartin Matuska #include <string.h>
321f1e2261SMartin Matuska
33eac7052fSMatt Macy #include <zone.h>
34eac7052fSMatt Macy
35eac7052fSMatt Macy zoneid_t
getzoneid(void)36c03c5b1cSMartin Matuska getzoneid(void)
37eac7052fSMatt Macy {
381f1e2261SMartin Matuska char path[PATH_MAX];
391f1e2261SMartin Matuska char buf[128] = { '\0' };
401f1e2261SMartin Matuska char *cp;
411f1e2261SMartin Matuska
421f1e2261SMartin Matuska int c = snprintf(path, sizeof (path), "/proc/self/ns/user");
431f1e2261SMartin Matuska /* This API doesn't have any error checking... */
44dbd5678dSMartin Matuska if (c < 0 || c >= sizeof (path))
45*525fe93dSMartin Matuska return (GLOBAL_ZONEID);
461f1e2261SMartin Matuska
471f1e2261SMartin Matuska ssize_t r = readlink(path, buf, sizeof (buf) - 1);
481f1e2261SMartin Matuska if (r < 0)
49*525fe93dSMartin Matuska return (GLOBAL_ZONEID);
501f1e2261SMartin Matuska
511f1e2261SMartin Matuska cp = strchr(buf, '[');
521f1e2261SMartin Matuska if (cp == NULL)
53*525fe93dSMartin Matuska return (GLOBAL_ZONEID);
541f1e2261SMartin Matuska cp++;
551f1e2261SMartin Matuska
561f1e2261SMartin Matuska unsigned long n = strtoul(cp, NULL, 10);
571f1e2261SMartin Matuska if (n == ULONG_MAX && errno == ERANGE)
58*525fe93dSMartin Matuska return (GLOBAL_ZONEID);
591f1e2261SMartin Matuska zoneid_t z = (zoneid_t)n;
601f1e2261SMartin Matuska
611f1e2261SMartin Matuska return (z);
62eac7052fSMatt Macy }
63