1*f14fb602SLionel Sambuc /* $NetBSD: getdevmajor.c,v 1.6 2012/03/13 21:13:35 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 2004 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Andrew Brown.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras * without specific prior written permission.
212fe8fb19SBen Gras *
222fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
232fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
242fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
252fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
262fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
272fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
282fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
292fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
302fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
312fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
322fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
332fe8fb19SBen Gras */
342fe8fb19SBen Gras
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
37*f14fb602SLionel Sambuc __RCSID("$NetBSD: getdevmajor.c,v 1.6 2012/03/13 21:13:35 christos Exp $");
382fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
392fe8fb19SBen Gras
402fe8fb19SBen Gras #include "namespace.h"
412fe8fb19SBen Gras #include <sys/types.h>
422fe8fb19SBen Gras #include <sys/stat.h>
432fe8fb19SBen Gras #include <sys/param.h>
442fe8fb19SBen Gras #include <sys/sysctl.h>
452fe8fb19SBen Gras
462fe8fb19SBen Gras #include <errno.h>
472fe8fb19SBen Gras #include <string.h>
482fe8fb19SBen Gras #include <stdlib.h>
492fe8fb19SBen Gras
502fe8fb19SBen Gras #ifdef __weak_alias
512fe8fb19SBen Gras __weak_alias(getdevmajor,_getdevmajor)
522fe8fb19SBen Gras #endif
532fe8fb19SBen Gras
542fe8fb19SBen Gras /*
552fe8fb19SBen Gras * XXX temporary alias because getdevmajor() was renamed
562fe8fb19SBen Gras * in -current for some time
572fe8fb19SBen Gras */
582fe8fb19SBen Gras dev_t __getdevmajor50(const char *, mode_t);
592fe8fb19SBen Gras dev_t
__getdevmajor50(const char * name,mode_t type)602fe8fb19SBen Gras __getdevmajor50(const char *name, mode_t type)
612fe8fb19SBen Gras {
622fe8fb19SBen Gras
632fe8fb19SBen Gras return (dev_t)getdevmajor(name, type);
642fe8fb19SBen Gras }
652fe8fb19SBen Gras
662fe8fb19SBen Gras devmajor_t
getdevmajor(const char * name,mode_t type)672fe8fb19SBen Gras getdevmajor(const char *name, mode_t type)
682fe8fb19SBen Gras {
692fe8fb19SBen Gras struct kinfo_drivers kd[200], *kdp = &kd[0];
70*f14fb602SLionel Sambuc size_t i, sz = sizeof(kd);
71*f14fb602SLionel Sambuc int rc;
722fe8fb19SBen Gras devmajor_t n = NODEVMAJOR;
732fe8fb19SBen Gras
742fe8fb19SBen Gras if (type != S_IFCHR && type != S_IFBLK) {
752fe8fb19SBen Gras errno = EINVAL;
762fe8fb19SBen Gras return n;
772fe8fb19SBen Gras }
782fe8fb19SBen Gras
792fe8fb19SBen Gras do {
802fe8fb19SBen Gras rc = sysctlbyname("kern.drivers", kdp, &sz, NULL, 0);
812fe8fb19SBen Gras if (rc == -1) {
822fe8fb19SBen Gras if (errno != ENOMEM)
832fe8fb19SBen Gras goto out;
842fe8fb19SBen Gras if (kdp != &kd[0])
852fe8fb19SBen Gras free(kdp);
862fe8fb19SBen Gras if ((kdp = malloc(sz)) == NULL)
872fe8fb19SBen Gras return n;
882fe8fb19SBen Gras }
892fe8fb19SBen Gras } while (rc == -1);
902fe8fb19SBen Gras
91*f14fb602SLionel Sambuc sz /= sizeof(*kdp);
922fe8fb19SBen Gras
93*f14fb602SLionel Sambuc for (i = 0; i < sz; i++) {
942fe8fb19SBen Gras if (strcmp(name, kdp[i].d_name) == 0) {
952fe8fb19SBen Gras if (type == S_IFCHR)
962fe8fb19SBen Gras n = kdp[i].d_cmajor;
972fe8fb19SBen Gras else
982fe8fb19SBen Gras n = kdp[i].d_bmajor;
992fe8fb19SBen Gras break;
1002fe8fb19SBen Gras }
1012fe8fb19SBen Gras }
102*f14fb602SLionel Sambuc if (i >= sz)
1032fe8fb19SBen Gras errno = ENOENT;
1042fe8fb19SBen Gras
1052fe8fb19SBen Gras out:
1062fe8fb19SBen Gras if (kdp != &kd[0])
1072fe8fb19SBen Gras free(kdp);
1082fe8fb19SBen Gras
1092fe8fb19SBen Gras return n;
1102fe8fb19SBen Gras }
111