3 * Copyright (C) 2011 Simone Piccardi
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.
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.
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.
19 /*****************************************************************************
21 * File mydmesg.c: An example for klogctl function
23 * Author: S. Piccardi Aug. 2011
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 */
37 /* Help printing routine */
40 int main(int argc, char *argv[])
43 * Variables definition
45 int i, len, cmd=0, level=7, third;
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"
61 * Input section: decode command line parameters
64 opterr = 0; /* don't want writing to stderr */
65 while ( (i = getopt(argc, argv, "c:l:h")) != -1) {
70 case 'h': /* help option */
71 printf("Wrong -h option use\n");
75 case 'c': /* klogctl command */
76 cmd = strtol(optarg, NULL, 10); /* convert input */
78 case 'l': /* log level */
79 level = strtol(optarg, NULL, 10); /* convert input */
81 case '?': /* unrecognized options */
82 printf("Unrecognized options -%c\n",optopt);
84 default: /* should not reached */
88 /* ***********************************************************
90 * Options processing completed
94 * ***********************************************************/
95 if ((argc - optind) != 0) { /* There must be 0 remaing arguments */
96 printf("Wrong number of arguments %d\n", argc - optind);
99 if ((cmd < 1) || (cmd>10)) {
100 printf("You must give a value between 1 and 10 for the -c option\n");
103 i = klogctl(10, NULL, 0);
104 if ((buffer = malloc(i+1)) == NULL) {
105 printf("Cannot allocate buffer for %d bytes\n", i);
108 printf("Allocated %d bytes\n", i);
111 /* setting third argument */
112 if (cmd == 8) third=level;
116 if ((len = klogctl(cmd, buffer, third)) < 0) {
117 perror(err_msg[cmd]);
126 printf("Debug, cmd = %d\n", cmd);
127 printf("Debug, len = %d\n", len);
129 printf("%s", buffer);
135 printf("Operation %d executed", cmd);
138 printf("Kernel log buffer ring has %d bytes\n", len);
141 printf("Kernel log buffer ring is %d bytes\n", len);
149 * routine to print usage info and exit
152 printf("Program mydmesg: klogctl testing \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");