Loading [MathJax]/jax/output/HTML-CSS/config.js
CS350 COS
COS
All Data Structures Files Functions Variables Typedefs Macros
sysctl.c File Reference
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <syscall.h>
#include <sys/sysctl.h>
Include dependency graph for sysctl.c:

Go to the source code of this file.

Data Structures

struct  SysCtlEntry
 

Macros

#define SYSCTL_STR(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT)    { #_PATH, SYSCTL_TYPE_STR, _FLAGS, _DESCRIPTION },
 
#define SYSCTL_INT(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT)    { #_PATH, SYSCTL_TYPE_INT, _FLAGS, _DESCRIPTION },
 
#define SYSCTL_BOOL(_PATH, _FLAGS, _DESCRIPTION, _DEFAULT)    { #_PATH, SYSCTL_TYPE_BOOL, _FLAGS, _DESCRIPTION },
 

Typedefs

typedef struct SysCtlEntry SysCtlEntry
 

Functions

void PrintVal (int idx)
 
void UpdateVal (int idx, const char *val)
 
int main (int argc, const char *argv[])
 

Variables

SysCtlEntry SYSCTLTable []
 

Data Structure Documentation

◆ SysCtlEntry

struct SysCtlEntry

Definition at line 10 of file sysctl.c.

Collaboration diagram for SysCtlEntry:
[legend]
Data Fields
char description[128]
int flags
void * node
char path[64]
int type

Macro Definition Documentation

◆ SYSCTL_BOOL

#define SYSCTL_BOOL (   _PATH,
  _FLAGS,
  _DESCRIPTION,
  _DEFAULT 
)     { #_PATH, SYSCTL_TYPE_BOOL, _FLAGS, _DESCRIPTION },

Definition at line 21 of file sysctl.c.

◆ SYSCTL_INT

#define SYSCTL_INT (   _PATH,
  _FLAGS,
  _DESCRIPTION,
  _DEFAULT 
)     { #_PATH, SYSCTL_TYPE_INT, _FLAGS, _DESCRIPTION },

Definition at line 19 of file sysctl.c.

◆ SYSCTL_STR

#define SYSCTL_STR (   _PATH,
  _FLAGS,
  _DESCRIPTION,
  _DEFAULT 
)     { #_PATH, SYSCTL_TYPE_STR, _FLAGS, _DESCRIPTION },

Definition at line 17 of file sysctl.c.

Typedef Documentation

◆ SysCtlEntry

typedef struct SysCtlEntry SysCtlEntry

Function Documentation

◆ main()

int main ( int  argc,
const char *  argv[] 
)

Definition at line 103 of file sysctl.c.

104{
105 if (argc == 2 && strcmp(argv[1],"-h") == 0) {
106 printf("Usage: sysctl [NODE] [VALUE]\n");
107 return 1;
108 }
109
110 if (argc == 2 && strcmp(argv[1],"-d") == 0) {
111 printf("%-20s %s\n", "Name", "Description");
112 for (int i = 0; SYSCTLTable[i].type != 0; i++) {
113 printf("%-20s %s\n",
114 SYSCTLTable[i].path,
115 SYSCTLTable[i].description);
116 }
117 return 0;
118 }
119
120 if (argc == 2 || argc == 3) {
121 for (int i = 0; SYSCTLTable[i].type != 0; i++) {
122 if (strcmp(SYSCTLTable[i].path, argv[1]) == 0) {
123 if (argc == 2)
124 PrintVal(i);
125 else
126 UpdateVal(i, argv[2]);
127 }
128 }
129
130 return 0;
131 }
132
133 for (int i = 0; SYSCTLTable[i].type != 0; i++) {
134 PrintVal(i);
135 }
136
137 return 0;
138}
void PrintVal(int idx)
Definition: sysctl.c:32
void UpdateVal(int idx, const char *val)
Definition: sysctl.c:64
SysCtlEntry SYSCTLTable[]
Definition: sysctl.c:23
int printf(const char *fmt,...)
Definition: printf.c:212
int strcmp(const char *s1, const char *s2)
Definition: string.c:81
int type
Definition: sysctl.c:12
Here is the call graph for this function:

◆ PrintVal()

void PrintVal ( int  idx)

Definition at line 32 of file sysctl.c.

33{
34 switch (SYSCTLTable[idx].type) {
35 case SYSCTL_TYPE_STR: {
36 SysCtlString scStr;
37
38 OSSysCtl(SYSCTLTable[idx].path, &scStr, NULL);
39 printf("%s: %s\n", SYSCTLTable[idx].path, scStr.value);
40 break;
41 }
42 case SYSCTL_TYPE_INT: {
43 SysCtlInt scInt;
44
45 OSSysCtl(SYSCTLTable[idx].path, &scInt, NULL);
46 printf("%s: %ld\n", SYSCTLTable[idx].path, scInt.value);
47 break;
48 }
49 case SYSCTL_TYPE_BOOL: {
50 SysCtlBool scBool;
51
52 OSSysCtl(SYSCTLTable[idx].path, &scBool, NULL);
53 printf("%s: %s\n", SYSCTLTable[idx].path,
54 scBool.value ? "true" : "false");
55 break;
56 }
57 default:
58 printf("%s: Unsupported type\n", SYSCTLTable[idx].path);
59 break;
60 }
61}
int OSSysCtl(const char *node, void *oldval, void *newval)
Definition: syscall.c:155
uint32_t type
Definition: multiboot.h:8
#define NULL
Definition: stddef.h:6
int64_t value
Definition: sysctl.h:42
char value[SYSCTL_STR_MAXLENGTH]
Definition: sysctl.h:37
#define SYSCTL_TYPE_INT
Definition: sysctl.h:17
#define SYSCTL_TYPE_BOOL
Definition: sysctl.h:18
#define SYSCTL_TYPE_STR
Definition: sysctl.h:16
bool value
Definition: sysctl.h:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateVal()

void UpdateVal ( int  idx,
const char *  val 
)

Definition at line 64 of file sysctl.c.

65{
66 switch (SYSCTLTable[idx].type) {
67 case SYSCTL_TYPE_STR: {
68 SysCtlString scStr;
69
70 strncpy(scStr.value, val, sizeof(scStr.value) - 1);
71 OSSysCtl(SYSCTLTable[idx].path, NULL, &scStr);
72 break;
73 }
74 case SYSCTL_TYPE_INT: {
75 SysCtlInt scInt;
76
77 scInt.value = atoi(val);
78 printf("%ld\n", scInt.value);
79 OSSysCtl(SYSCTLTable[idx].path, NULL, &scInt);
80 break;
81 }
82 case SYSCTL_TYPE_BOOL: {
83 SysCtlBool scBool;
84
85 if (strcmp(val,"true") == 0)
86 scBool.value = true;
87 else if (strcmp(val,"false") == 0)
88 scBool.value = false;
89 else {
90 printf("Value must be true or false\n");
91 exit(1);
92 }
93 OSSysCtl(SYSCTLTable[idx].path, NULL, &scBool);
94 break;
95 }
96 default:
97 printf("%s: Unsupported type\n", SYSCTLTable[idx].path);
98 break;
99 }
100}
void exit(int status)
Definition: exit.c:36
int atoi(const char *nptr)
Definition: stdlib.c:3
char * strncpy(char *to, const char *from, size_t len)
Definition: string.c:34
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ SYSCTLTable

SysCtlEntry SYSCTLTable[]
Initial value:
= {
{ "", 0, 0, "" },
}
#define SYSCTL_LIST
Definition: sysctl.h:23

Definition at line 23 of file sysctl.c.