Path: blob/main/sys/contrib/openzfs/lib/libspl/os/linux/zone.c
48546 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2006 Ricardo Correia. All rights reserved.23* Use is subject to license terms.24*/2526#include <unistd.h>27#include <stdio.h>28#include <errno.h>29#include <stdlib.h>30#include <limits.h>31#include <string.h>3233#include <zone.h>3435zoneid_t36getzoneid(void)37{38char path[PATH_MAX];39char buf[128] = { '\0' };40char *cp;4142int c = snprintf(path, sizeof (path), "/proc/self/ns/user");43/* This API doesn't have any error checking... */44if (c < 0 || c >= sizeof (path))45return (GLOBAL_ZONEID);4647ssize_t r = readlink(path, buf, sizeof (buf) - 1);48if (r < 0)49return (GLOBAL_ZONEID);5051cp = strchr(buf, '[');52if (cp == NULL)53return (GLOBAL_ZONEID);54cp++;5556unsigned long n = strtoul(cp, NULL, 10);57if (n == ULONG_MAX && errno == ERANGE)58return (GLOBAL_ZONEID);59zoneid_t z = (zoneid_t)n;6061return (z);62}636465