1/*	$NetBSD: getdevmajor.c,v 1.7 2024/01/20 14:52:47 christos Exp $ */
2
3/*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36#if defined(LIBC_SCCS) && !defined(lint)
37__RCSID("$NetBSD: getdevmajor.c,v 1.7 2024/01/20 14:52:47 christos Exp $");
38#endif /* LIBC_SCCS and not lint */
39
40#include "namespace.h"
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <sys/param.h>
44#include <sys/sysctl.h>
45
46#include <errno.h>
47#include <string.h>
48#include <stdlib.h>
49#include "getdevmajor_private.h"
50
51#ifdef __weak_alias
52__weak_alias(getdevmajor,_getdevmajor)
53#endif
54
55/*
56 * XXX temporary alias because getdevmajor() was renamed
57 * in -current for some time
58 */
59dev_t
60__getdevmajor50(const char *name, mode_t type)
61{
62
63	return (dev_t)getdevmajor(name, type);
64}
65
66devmajor_t
67getdevmajor(const char *name, mode_t type)
68{
69	struct kinfo_drivers kd[200], *kdp = &kd[0];
70	size_t i, sz = sizeof(kd);
71	int rc;
72	devmajor_t n = NODEVMAJOR;
73
74	if (type != S_IFCHR && type != S_IFBLK) {
75		errno = EINVAL;
76		return n;
77	}
78
79	do {
80		rc = sysctlbyname("kern.drivers", kdp, &sz, NULL, 0);
81		if (rc == -1) {
82			if (errno != ENOMEM)
83				goto out;
84			if (kdp != &kd[0])
85				free(kdp);
86			if ((kdp = malloc(sz)) == NULL)
87				return n;
88		}
89	} while (rc == -1);
90
91	sz /= sizeof(*kdp);
92
93	for (i = 0; i < sz; i++) {
94		if (strcmp(name, kdp[i].d_name) == 0) {
95			if (type == S_IFCHR)
96				n = kdp[i].d_cmajor;
97			else
98				n = kdp[i].d_bmajor;
99			break;
100		}
101	}
102	if (i >= sz)
103		errno = ENOENT;
104
105  out:
106	if (kdp != &kd[0])
107		free(kdp);
108
109	return n;
110}
111