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