xref: /netbsd-src/lib/libc/compat/net/__cmsg_alignbytes.c (revision ace5b9b5feb0e7608bd2da7a617428d2e1cf8aa3)
1*ace5b9b5Schristos /*	$NetBSD: __cmsg_alignbytes.c,v 1.2 2024/01/20 14:52:46 christos Exp $	*/
2e8bec33bSjoerg 
3e8bec33bSjoerg /*-
4e8bec33bSjoerg  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5e8bec33bSjoerg  * All rights reserved.
6e8bec33bSjoerg  *
7e8bec33bSjoerg  * This code is derived from software contributed to The NetBSD Foundation
8e8bec33bSjoerg  * by Jun-ichiro Hagino.
9e8bec33bSjoerg  *
10e8bec33bSjoerg  * Redistribution and use in source and binary forms, with or without
11e8bec33bSjoerg  * modification, are permitted provided that the following conditions
12e8bec33bSjoerg  * are met:
13e8bec33bSjoerg  * 1. Redistributions of source code must retain the above copyright
14e8bec33bSjoerg  *    notice, this list of conditions and the following disclaimer.
15e8bec33bSjoerg  * 2. Redistributions in binary form must reproduce the above copyright
16e8bec33bSjoerg  *    notice, this list of conditions and the following disclaimer in the
17e8bec33bSjoerg  *    documentation and/or other materials provided with the distribution.
18e8bec33bSjoerg  *
19e8bec33bSjoerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20e8bec33bSjoerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21e8bec33bSjoerg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22e8bec33bSjoerg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23e8bec33bSjoerg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24e8bec33bSjoerg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25e8bec33bSjoerg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26e8bec33bSjoerg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27e8bec33bSjoerg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28e8bec33bSjoerg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29e8bec33bSjoerg  * POSSIBILITY OF SUCH DAMAGE.
30e8bec33bSjoerg  */
31e8bec33bSjoerg 
32e8bec33bSjoerg #include <sys/cdefs.h>
33*ace5b9b5Schristos __RCSID("$NetBSD: __cmsg_alignbytes.c,v 1.2 2024/01/20 14:52:46 christos Exp $");
34e8bec33bSjoerg 
35e8bec33bSjoerg #include "namespace.h"
36e8bec33bSjoerg #include <sys/types.h>
37e8bec33bSjoerg #include <sys/param.h>
38e8bec33bSjoerg #include <sys/sysctl.h>
39e8bec33bSjoerg #include <sys/socket.h>
40e8bec33bSjoerg 
41*ace5b9b5Schristos #include <compat/include/extern.h>
42e8bec33bSjoerg 
43e8bec33bSjoerg int
__cmsg_alignbytes(void)44e8bec33bSjoerg __cmsg_alignbytes(void)
45e8bec33bSjoerg {
46e8bec33bSjoerg 	static int alignbytes = -1;
47e8bec33bSjoerg #ifdef HW_ALIGNBYTES
48e8bec33bSjoerg 	int mib[2];
49e8bec33bSjoerg 	size_t len;
50e8bec33bSjoerg 	int ret;
51e8bec33bSjoerg #endif
52e8bec33bSjoerg 
53e8bec33bSjoerg 	if (alignbytes > 0)
54e8bec33bSjoerg 		return alignbytes;
55e8bec33bSjoerg 
56e8bec33bSjoerg #ifdef HW_ALIGNBYTES
57e8bec33bSjoerg 	mib[0] = CTL_HW;
58e8bec33bSjoerg 	mib[1] = HW_ALIGNBYTES;
59e8bec33bSjoerg 	len = sizeof(alignbytes);
60e8bec33bSjoerg 	ret = sysctl(mib, (u_int) (sizeof(mib) / sizeof(mib[0])),
61e8bec33bSjoerg 	    (void *)&alignbytes, &len, NULL, (size_t)0);
62e8bec33bSjoerg 	if (ret >= 0 && alignbytes >= 0)
63e8bec33bSjoerg 		return alignbytes;
64e8bec33bSjoerg #endif
65e8bec33bSjoerg 	/* last resort */
66e8bec33bSjoerg 	alignbytes = ALIGNBYTES;
67e8bec33bSjoerg 	return alignbytes;
68e8bec33bSjoerg }
69