RDK Documentation (Open Sourced RDK Components)
rdklogctrl.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <netinet/in.h>
7 #include <netdb.h>
8 #include <sys/socket.h>
9 #include <sys/wait.h>
10 #include <arpa/inet.h>
11 #include <unistd.h>
12 #include "rdk_dynamic_logger.h"
13 
14 #define COMP_SIGNATURE "LOG.RDK."
15 #define COMP_SIGNATURE_LEN 8
16 #define DL_PORT 12035
17 #define DL_SIGNATURE "COMC"
18 #define DL_SIGNATURE_LEN 4
19 
20 static void usage(const char* app_name)
21 {
22  printf("usage: %s <app_name> <module_name> <loglevel> \n",app_name);
23  printf("Parameters:\n");
24  printf("app_name -> Application name, as per listed by 'ps' command\n");
25  printf("module_name -> Module name.\n");
26  printf(" For RDK component, the 'Module name' is expected to start with 'LOG.RDK.' string\n");
27  printf(" For CPC component like 'Reciever', the module name can be either 'LOG.RDK.' or 'XREConnection', 'RmfMediaPlayer', etc.\n");
28  printf("loglevel -> Log Level of the Component to be modified\n");
29  printf(" Possible values - FATAL,ERROR,WARN,NOTICE,INFO,DEBUG,TRACE1-TRACE9\n");
30  printf(" Turn off any loglevel using '~' symbol.\n");
31  printf(" (i.e) '~ERROR' would turn off error logs alone for that component\n");
32 }
33 
34 static char validate_loglevel(const char* level)
35 {
36  char *loglevel = (char *)level;
37  char negate = 0;
38 
39  if(loglevel[0] == '~') {
40  loglevel++;
41  negate = 0x80;
42  }
43 
44  if(0 == strncmp(loglevel,"FATAL",5))
45  return negate|RDK_LOG_FATAL;
46  else if(0 == strncmp(loglevel,"ERROR",5))
47  return negate|RDK_LOG_ERROR;
48  else if(0 == strncmp(loglevel,"WARN",4))
49  return negate|RDK_LOG_WARN;
50  else if(0 == strncmp(loglevel,"NOTICE",6))
51  return negate|RDK_LOG_NOTICE;
52  else if(0 == strncmp(loglevel,"INFO",4))
53  return negate|RDK_LOG_INFO;
54  else if(0 == strncmp(loglevel,"DEBUG",5))
55  return negate|RDK_LOG_DEBUG;
56  else if(0 == strncmp(loglevel,"TRACE1",6))
57  return negate|RDK_LOG_TRACE1;
58  else if(0 == strncmp(loglevel,"TRACE2",6))
59  return negate|RDK_LOG_TRACE2;
60  else if(0 == strncmp(loglevel,"TRACE3",6))
61  return negate|RDK_LOG_TRACE3;
62  else if(0 == strncmp(loglevel,"TRACE4",6))
63  return negate|RDK_LOG_TRACE4;
64  else if(0 == strncmp(loglevel,"TRACE5",6))
65  return negate|RDK_LOG_TRACE5;
66  else if(0 == strncmp(loglevel,"TRACE6",6))
67  return negate|RDK_LOG_TRACE6;
68  else if(0 == strncmp(loglevel,"TRACE7",6))
69  return negate|RDK_LOG_TRACE7;
70  else if(0 == strncmp(loglevel,"TRACE8",6))
71  return negate|RDK_LOG_TRACE8;
72  else if(0 == strncmp(loglevel,"TRACE9",6))
73  return negate|RDK_LOG_TRACE9;
74  else
75  return ENUM_RDK_LOG_COUNT;
76 }
77 
78 static int validate_module_name(const char *name)
79 {
80  FILE *fp = NULL;
81  char ch;
82  char comp_name[128] = {0};
83  int ret = -1;
84 
85  fp = fopen("/etc/debug.ini","r");
86  if(NULL == fp)
87  return ret;
88 
89  do {
90  ch = fscanf(fp, "%s", comp_name);
91  if(strstr(comp_name,name)) {
92  ret = 0;
93  break;
94  }
95  } while(ch == 1);
96 
97  fclose(fp);
98  return ret;
99 }
100 
101 int main(int argc, char *argv[])
102 {
103  struct sockaddr_in dest_addr;
104  int i, sockfd, numbytes, addr_len, app_len, comp_len, optval = 1;
105  char level = -1;
106  unsigned char buf[128] = {0};
107 
108  if (argc != 4) {
109  usage(argv[0]);
110  return -1;
111  }
112 
113  if(0 != strcmp("Receiver",argv[1])) {
114  if( (0 != strncmp(argv[2],COMP_SIGNATURE,COMP_SIGNATURE_LEN)) ||
115  (0 != validate_module_name(argv[2])) ) {
116  printf("Invalid module name\n");
117  usage(argv[0]);
118  return -1;
119  }
120  }
121 
122  level = validate_loglevel(argv[3]);
123  if(ENUM_RDK_LOG_COUNT == level) {
124  printf("Invalid log level\n");
125  usage(argv[0]);
126  return -1;
127  }
128 
129  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
130  printf("socket: %s\n",strerror(errno));
131  return -1;
132  }
133 
134  if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,(char *) &optval,sizeof(optval))){
135  printf("Error setting socket to BROADCAST mode %s\n",strerror(errno));
136  return -1;
137  }
138 
139  memset(&dest_addr,0,sizeof(dest_addr));
140  dest_addr.sin_family = AF_INET;
141  dest_addr.sin_port=htons((unsigned short) DL_PORT);
142  dest_addr.sin_addr.s_addr=inet_addr("127.255.255.255");
143 
144  /* Dynamic log signature 'COMC' */
145  i = DL_SIGNATURE_LEN;
146  memcpy(buf,DL_SIGNATURE,i);
147 
148  /* Log level */
149  buf[++i] = (unsigned char)level;
150 
151  /* App name length */
152  app_len = strlen(argv[1]);
153  buf[++i] = app_len;
154 
155  /* App name */
156  memcpy(buf+(++i),argv[1],app_len);
157 
158  /* Module name length */
159  comp_len = strlen(argv[2]);
160  i += app_len;
161  buf[i] = comp_len;
162 
163  /* Module name */
164  memcpy(buf+(++i),argv[2],comp_len);
165 
166  /* Total packet length excluding Dynamic log signature.
167  * Application name length + the byte to hold that value +
168  * Component name length + the byte to hold that value +
169  * log level byte
170  */
171  buf[4] = app_len + comp_len + 3;
172 
173  /* Total packet length including Dynamic log signature +
174  * the byte to hold that value
175  */
176 
177 #define DL_PACKET_LEN buf[4]+DL_SIGNATURE_LEN+1
178 
179  if ((numbytes=sendto(sockfd, buf, DL_PACKET_LEN, 0, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr))) == -1) {
180  printf("sendto: %s\n",strerror(errno));
181  return -1;
182  }
183 
184  printf( "Sent message to update log level of %s for %s process\n", argv[2], argv[1]);
185  close(sockfd);
186  return 0;
187 }
188