Aggiornamento note copyright
[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     if ((len = klogctl(cmd, buffer, third)) < 0) {
116             perror(err_msg[cmd]);
117             exit(0);                  
118     }
119
120     switch (cmd) 
121     {
122     case 2:
123     case 3:
124     case 4:
125         printf("Debug, cmd = %d\n", cmd);
126         printf("Debug, len = %d\n", len);
127         buffer[len]=0;
128         printf("%s", buffer);
129         exit(0);
130     case 5:
131     case 6:
132     case 7:
133     case 8:
134         printf("Operation %d executed\n", cmd);
135         exit(0);
136     case 9:
137         printf("Kernel log buffer ring has %d bytes\n", len);
138         exit(0);
139     case 10:
140         printf("Kernel log buffer ring is %d bytes\n", len);
141         exit(0);
142     }
143
144
145     exit(0);
146 }
147 /*
148  * routine to print usage info and exit
149  */
150 void usage(void) {
151     printf("Program mydmesg: klogctl testing \n");
152     printf("Usage:\n");
153     printf(" mydmesg [-h] [-l N] -c command \n");
154     printf("  -h   print this help\n");
155     printf("  -c   klogctl command (mandatory)\n");
156     printf("  -l   level\n");
157     exit(1);
158 }