Home | History | Annotate | Line # | Download | only in gen
getdevmajor.c revision 1.4
      1  1.4  drochner /*	$NetBSD: getdevmajor.c,v 1.4 2009/01/20 18:20:48 drochner 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.4  drochner __RCSID("$NetBSD: getdevmajor.c,v 1.4 2009/01/20 18:20:48 drochner 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.4  drochner devmajor_t
     55  1.1    atatat getdevmajor(const char *name, mode_t type)
     56  1.1    atatat {
     57  1.3    atatat 	struct kinfo_drivers kd[200], *kdp = &kd[0];
     58  1.2  christos 	int rc, i;
     59  1.2  christos 	size_t sz = sizeof(kd);
     60  1.4  drochner 	devmajor_t n = NODEVMAJOR;
     61  1.1    atatat 
     62  1.1    atatat 	if (type != S_IFCHR && type != S_IFBLK) {
     63  1.1    atatat 		errno = EINVAL;
     64  1.2  christos 		return n;
     65  1.1    atatat 	}
     66  1.1    atatat 
     67  1.1    atatat 	do {
     68  1.1    atatat 		rc = sysctlbyname("kern.drivers", kdp, &sz, NULL, 0);
     69  1.1    atatat 		if (rc == -1) {
     70  1.1    atatat 			if (errno != ENOMEM)
     71  1.1    atatat 				goto out;
     72  1.1    atatat 			if (kdp != &kd[0])
     73  1.1    atatat 				free(kdp);
     74  1.2  christos 			if ((kdp = malloc(sz)) == NULL)
     75  1.2  christos 				return n;
     76  1.1    atatat 		}
     77  1.1    atatat 	} while (rc == -1);
     78  1.1    atatat 
     79  1.1    atatat 	rc = sz / sizeof(*kdp);
     80  1.1    atatat 
     81  1.3    atatat 	for (i = 0; i < rc; i++) {
     82  1.3    atatat 		if (strcmp(name, kdp[i].d_name) == 0) {
     83  1.1    atatat 			if (type == S_IFCHR)
     84  1.3    atatat 				n = kdp[i].d_cmajor;
     85  1.1    atatat 			else
     86  1.3    atatat 				n = kdp[i].d_bmajor;
     87  1.1    atatat 			break;
     88  1.1    atatat 		}
     89  1.1    atatat 	}
     90  1.3    atatat 	if (i >= rc)
     91  1.1    atatat 		errno = ENOENT;
     92  1.1    atatat 
     93  1.1    atatat   out:
     94  1.1    atatat 	if (kdp != &kd[0])
     95  1.1    atatat 		free(kdp);
     96  1.1    atatat 
     97  1.2  christos 	return n;
     98  1.1    atatat }
     99