xref: /netbsd-src/lib/libc/gen/getdevmajor.c (revision ace5b9b5feb0e7608bd2da7a617428d2e1cf8aa3)
1*ace5b9b5Schristos /*	$NetBSD: getdevmajor.c,v 1.7 2024/01/20 14:52:47 christos Exp $ */
22803cf76Satatat 
32803cf76Satatat /*-
42803cf76Satatat  * Copyright (c) 2004 The NetBSD Foundation, Inc.
52803cf76Satatat  * All rights reserved.
62803cf76Satatat  *
72803cf76Satatat  * This code is derived from software contributed to The NetBSD Foundation
82803cf76Satatat  * by Andrew Brown.
92803cf76Satatat  *
102803cf76Satatat  * Redistribution and use in source and binary forms, with or without
112803cf76Satatat  * modification, are permitted provided that the following conditions
122803cf76Satatat  * are met:
132803cf76Satatat  * 1. Redistributions of source code must retain the above copyright
142803cf76Satatat  *    notice, this list of conditions and the following disclaimer.
152803cf76Satatat  * 2. Redistributions in binary form must reproduce the above copyright
162803cf76Satatat  *    notice, this list of conditions and the following disclaimer in the
172803cf76Satatat  *    documentation and/or other materials provided with the distribution.
182803cf76Satatat  * 3. Neither the name of the University nor the names of its contributors
192803cf76Satatat  *    may be used to endorse or promote products derived from this software
202803cf76Satatat  *    without specific prior written permission.
212803cf76Satatat  *
222803cf76Satatat  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
232803cf76Satatat  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
242803cf76Satatat  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
252803cf76Satatat  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
262803cf76Satatat  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
272803cf76Satatat  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
282803cf76Satatat  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
292803cf76Satatat  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
302803cf76Satatat  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
312803cf76Satatat  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
322803cf76Satatat  * POSSIBILITY OF SUCH DAMAGE.
332803cf76Satatat  */
342803cf76Satatat 
352803cf76Satatat #include <sys/cdefs.h>
362803cf76Satatat #if defined(LIBC_SCCS) && !defined(lint)
37*ace5b9b5Schristos __RCSID("$NetBSD: getdevmajor.c,v 1.7 2024/01/20 14:52:47 christos Exp $");
382803cf76Satatat #endif /* LIBC_SCCS and not lint */
392803cf76Satatat 
402803cf76Satatat #include "namespace.h"
412803cf76Satatat #include <sys/types.h>
422803cf76Satatat #include <sys/stat.h>
432803cf76Satatat #include <sys/param.h>
442803cf76Satatat #include <sys/sysctl.h>
452803cf76Satatat 
462803cf76Satatat #include <errno.h>
472803cf76Satatat #include <string.h>
482803cf76Satatat #include <stdlib.h>
49*ace5b9b5Schristos #include "getdevmajor_private.h"
502803cf76Satatat 
512803cf76Satatat #ifdef __weak_alias
__weak_alias(getdevmajor,_getdevmajor)522803cf76Satatat __weak_alias(getdevmajor,_getdevmajor)
532803cf76Satatat #endif
542803cf76Satatat 
55ad965be0Sdrochner /*
56ad965be0Sdrochner  * XXX temporary alias because getdevmajor() was renamed
57ad965be0Sdrochner  * in -current for some time
58ad965be0Sdrochner  */
59ad965be0Sdrochner dev_t
60ad965be0Sdrochner __getdevmajor50(const char *name, mode_t type)
61ad965be0Sdrochner {
62ad965be0Sdrochner 
63ad965be0Sdrochner 	return (dev_t)getdevmajor(name, type);
64ad965be0Sdrochner }
65ad965be0Sdrochner 
66d767912bSdrochner devmajor_t
getdevmajor(const char * name,mode_t type)672803cf76Satatat getdevmajor(const char *name, mode_t type)
682803cf76Satatat {
69817b0022Satatat 	struct kinfo_drivers kd[200], *kdp = &kd[0];
70c5e820caSchristos 	size_t i, sz = sizeof(kd);
71c5e820caSchristos 	int rc;
72d767912bSdrochner 	devmajor_t n = NODEVMAJOR;
732803cf76Satatat 
742803cf76Satatat 	if (type != S_IFCHR && type != S_IFBLK) {
752803cf76Satatat 		errno = EINVAL;
76d15ecb98Schristos 		return n;
772803cf76Satatat 	}
782803cf76Satatat 
792803cf76Satatat 	do {
802803cf76Satatat 		rc = sysctlbyname("kern.drivers", kdp, &sz, NULL, 0);
812803cf76Satatat 		if (rc == -1) {
822803cf76Satatat 			if (errno != ENOMEM)
832803cf76Satatat 				goto out;
842803cf76Satatat 			if (kdp != &kd[0])
852803cf76Satatat 				free(kdp);
86d15ecb98Schristos 			if ((kdp = malloc(sz)) == NULL)
87d15ecb98Schristos 				return n;
882803cf76Satatat 		}
892803cf76Satatat 	} while (rc == -1);
902803cf76Satatat 
91c5e820caSchristos 	sz /= sizeof(*kdp);
922803cf76Satatat 
93c5e820caSchristos 	for (i = 0; i < sz; i++) {
94817b0022Satatat 		if (strcmp(name, kdp[i].d_name) == 0) {
952803cf76Satatat 			if (type == S_IFCHR)
96817b0022Satatat 				n = kdp[i].d_cmajor;
972803cf76Satatat 			else
98817b0022Satatat 				n = kdp[i].d_bmajor;
992803cf76Satatat 			break;
1002803cf76Satatat 		}
1012803cf76Satatat 	}
102c5e820caSchristos 	if (i >= sz)
1032803cf76Satatat 		errno = ENOENT;
1042803cf76Satatat 
1052803cf76Satatat   out:
1062803cf76Satatat 	if (kdp != &kd[0])
1072803cf76Satatat 		free(kdp);
1082803cf76Satatat 
109d15ecb98Schristos 	return n;
1102803cf76Satatat }
111