xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tcsd/platform.c (revision 9ddb6ab554e70fb9bbd90c3d96b812bc57755a14)
1 
2 /*
3  * Licensed Materials - Property of IBM
4  *
5  * trousers - An open source TCG Software Stack
6  *
7  * (C) Copyright International Business Machines Corp. 2004, 2005
8  *
9  */
10 
11 
12 #if (defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__))
13 #include <sys/param.h>
14 #include <sys/sysctl.h>
15 #include <err.h>
16 #elif (defined (__linux) || defined (linux) || defined(__GLIBC__))
17 #include <utmp.h>
18 #elif (defined (SOLARIS))
19 #include <utmpx.h>
20 #endif
21 
22 #include <sys/time.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "trousers/tss.h"
28 #include "trousers_types.h"
29 #include "tcs_tsp.h"
30 #include "tcs_int_literals.h"
31 #include "capabilities.h"
32 #include "tcsps.h"
33 #include "tcslog.h"
34 
35 
36 #if (defined (__linux) || defined (linux) || defined(__GLIBC__))
37 MUTEX_DECLARE_INIT(utmp_lock);
38 
39 char
40 platform_get_runlevel()
41 {
42 	char runlevel;
43 	struct utmp ut, save, *next = NULL;
44 	struct timeval tv;
45 	int flag = 0, counter = 0;
46 
47 	MUTEX_LOCK(utmp_lock);
48 
49 	memset(&ut, 0, sizeof(struct utmp));
50 	memset(&save, 0, sizeof(struct utmp));
51 	memset(&tv, 0, sizeof(struct timeval));
52 
53 	ut.ut_type = RUN_LVL;
54 
55 	next = getutid(&ut);
56 
57 	while (next != NULL) {
58 		if (next->ut_tv.tv_sec > tv.tv_sec) {
59 			memcpy(&save, next, sizeof(*next));
60 			flag = 1;
61 		} else if (next->ut_tv.tv_sec == tv.tv_sec) {
62 			if (next->ut_tv.tv_usec > tv.tv_usec) {
63 				memcpy(&save, next, sizeof(*next));
64 				flag = 1;
65 			}
66 		}
67 
68 		counter++;
69 		next = getutid(&ut);
70 	}
71 
72 	if (flag) {
73 		//printf("prev_runlevel=%c, runlevel=%c\n", save.ut_pid / 256, save.ut_pid % 256);
74 		runlevel = save.ut_pid % 256;
75 	} else {
76 		//printf("unknown\n");
77 		runlevel = 'u';
78 	}
79 
80 	MUTEX_UNLOCK(utmp_lock);
81 
82 	return runlevel;
83 }
84 #elif (defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__NetBSD__))
85 
86 char
87 platform_get_runlevel()
88 {
89 	int mib[2], rlevel = -1;
90 	size_t len;
91 
92 	mib[0] = CTL_KERN;
93 	mib[1] = KERN_SECURELVL;
94 
95 	len = sizeof(rlevel);
96 	if (sysctl(mib,2,&rlevel,&len, NULL,0) == -1) {
97 		err(1,"Could not get runlevel");
98 		return 'u';
99 	}
100 #if defined (__OpenBSD__)
101 	if (rlevel == 0)
102 #else
103 	if (rlevel == -1)
104 #endif
105 		return 's';
106 
107 	return rlevel + '0';
108 }
109 #elif (defined (SOLARIS))
110 
111 MUTEX_DECLARE_INIT(utmp_lock);
112 char
113 platform_get_runlevel()
114 {
115 	char runlevel;
116 	struct utmpx ut, *utp = NULL;
117 
118 	MUTEX_LOCK(utmp_lock);
119 
120 	memset(&ut, 0, sizeof(ut));
121 	ut.ut_type = RUN_LVL;
122 
123 	setutxent();
124 	utp = getutxid(&ut);
125 	if (utp->ut_type == RUN_LVL &&
126 	    sscanf(utp->ut_line, "run-level %c", &runlevel) != 1)
127 			runlevel = 'u';
128 	endutxent();
129 
130 	MUTEX_UNLOCK(utmp_lock);
131 
132 	return runlevel;
133 }
134 #endif
135