Programma di test della syscall syslog, e revisione generale
[gapil.git] / sources / mydmesg.c
1 /* mydmesg.c
2  * 
3  * Copyright (C) 2011 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*****************************************************************************
20  *
21  * File mydmesg.c: An example for klogctl function
22  *
23  * Author: S. Piccardi Aug. 2011
24  *
25  *****************************************************************************/
26 #include <sys/types.h>   /* primitive system data types */
27 #include <sys/stat.h>    /* file characteristics constants and functions */
28 #include <stdlib.h>      /* C standard library */
29 #include <stdio.h>       /* standard I/O library */
30 #include <unistd.h>      /* unix standard library */
31 #include <sys/klog.h>
32
33
34 /*
35  * Program mydmesg
36  */
37 /* Help printing routine */
38 void usage(void);
39
40 int main(int argc, char *argv[]) 
41 {
42 /* 
43  * Variables definition
44  */
45     int i, len, cmd=0, level=7, third;
46     char * buffer;
47     char * err_msg[]= {
48         "This is a NOP so it should'nt happen",
49         "This is a NOP so it should'nt happen",
50         "Error on reading kernel log buffer last messages",
51         "Error on reading kernel log buffer",
52         "Error on reading kernel and clearing log buffer",
53         "Error on clearing log buffer",
54         "Error on setting console log level to minimum",
55         "Error on setting console log level to default",
56         "Error on setting console log level",
57         "Error on reading unread message size",
58         "Error on reading log buffer size"
59     };
60     /*
61      * Input section: decode command line parameters 
62      * Use getopt function
63      */
64     opterr = 0;  /* don't want writing to stderr */
65     while ( (i = getopt(argc, argv, "c:l:h")) != -1) {
66         switch (i) {
67         /* 
68          * Handling options 
69          */ 
70         case 'h':                                  /* help option */
71             printf("Wrong -h option use\n");
72             usage();
73             return -1;
74             break;
75         case 'c':                                  /* klogctl command */
76             cmd = strtol(optarg, NULL, 10);        /* convert input */
77             break;
78         case 'l':                                  /* log level */
79             level = strtol(optarg, NULL, 10);      /* convert input */
80             break;
81         case '?':                                  /* unrecognized options */
82             printf("Unrecognized options -%c\n",optopt);
83             usage();
84         default:                                   /* should not reached */
85             usage();
86         }
87     }
88     /* ***********************************************************
89      * 
90      *           Options processing completed
91      *
92      *                Main code beginning
93      * 
94      * ***********************************************************/
95     if ((argc - optind) != 0) {   /* There must be 0 remaing arguments */
96         printf("Wrong number of arguments %d\n", argc - optind);
97         usage();
98     }
99     if ((cmd < 1) || (cmd>10)) {
100         printf("You must give a value between 1 and 10 for the -c option\n");
101         usage();
102     }
103     i = klogctl(10, NULL, 0);
104     if ((buffer = malloc(i+1)) == NULL) {
105         printf("Cannot allocate buffer for %d bytes\n", i);
106         exit(1);
107     } else {
108         printf("Allocated %d bytes\n", i);
109     }
110
111     /* setting third argument */
112     if (cmd == 8) third=level;
113     else third=i;
114
115
116     if ((len = klogctl(cmd, buffer, third)) < 0) {
117             perror(err_msg[cmd]);
118             exit(0);                  
119     }
120
121     switch (cmd) 
122     {
123     case 2:
124     case 3:
125     case 4:
126         printf("Debug, cmd = %d\n", cmd);
127         printf("Debug, len = %d\n", len);
128         buffer[len]=0;
129         printf("%s", buffer);
130         exit(0);
131     case 5:
132     case 6:
133     case 7:
134     case 8:
135         printf("Operation %d executed", cmd);
136         exit(0);
137     case 9:
138         printf("Kernel log buffer ring has %d bytes\n", len);
139         exit(0);
140     case 10:
141         printf("Kernel log buffer ring is %d bytes\n", len);
142         exit(0);
143     }
144
145
146     exit(0);
147 }
148 /*
149  * routine to print usage info and exit
150  */
151 void usage(void) {
152     printf("Program mydmesg: klogctl testing \n");
153     printf("Usage:\n");
154     printf(" mydmesg [-h] [-l N] -c command \n");
155     printf("  -h   print this help\n");
156     printf("  -c   klogctl command (mandatory)\n");
157     printf("  -l   level\n");
158     exit(1);
159 }