telnetsrv_proccmd.c 15.4 KB
Newer Older
1 2 3 4 5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file common/utils/telnetsrv/telnetsrv_proccmd.c
 * \brief: implementation of telnet commands related to this linux process
 * \author Francois TABURET
 * \date 2017
 * \version 0.1
 * \company NOKIA BellLabs France
 * \email: francois.taburet@nokia-bell-labs.com
 * \note
 * \warning
 */
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <string.h>
#include <stdarg.h>
#include <dirent.h>

#define READCFG_DYNLOAD

#define TELNETSERVERCODE
#include "telnetsrv.h"
#define TELNETSRV_PROCCMD_MAIN
55
#include "common/utils/LOG/log.h"
56
#include "common/config/config_userapi.h"
57
#include "openair1/PHY/phy_extern.h"
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include "telnetsrv_proccmd.h"

void decode_procstat(char *record, int debug, telnet_printfunc_t prnt)
{
char prntline[160];
char *procfile_fiels;
char *strtokptr;
char *lptr;
int fieldcnt;
char toksep[2];

  fieldcnt=0;	  
  procfile_fiels =strtok_r(record," ",&strtokptr);
  lptr= prntline;
/*http://man7.org/linux/man-pages/man5/proc.5.html gives the structure of the stat file */
 
74 75
  while( 	procfile_fiels != NULL && fieldcnt < 42) {
    long int policy;
76 77 78 79
    if (strlen(procfile_fiels) == 0)
       continue;
    fieldcnt++;
    sprintf(toksep," ");
80
    switch(fieldcnt) {
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
       case 1: /* id */
           lptr+=sprintf(lptr,"%9.9s ",procfile_fiels);
           sprintf(toksep,")");
       break;  
       case 2: /* name */
	   lptr+=sprintf(lptr,"%20.20s ",procfile_fiels+1);
       break;              
       case 3:   //thread state
           lptr+=sprintf(lptr,"  %c   ",procfile_fiels[0]);
       break;
       case 14:   //time in user mode
       case 15:   //time in kernel mode
           lptr+=sprintf(lptr,"%9.9s ",procfile_fiels);
       break;
       case 18:   //priority
       case 19:   //nice	       
           lptr+=sprintf(lptr,"%3.3s ",procfile_fiels);
       break;
       case 23:   //vsize	       
           lptr+=sprintf(lptr,"%9.9s ",procfile_fiels);
       break;
       case 39:   //processor	       
           lptr+=sprintf(lptr," %2.2s  ",procfile_fiels);
       break;
       case 41:   //policy	       
           lptr+=sprintf(lptr,"%3.3s ",procfile_fiels);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
           policy=strtol(procfile_fiels,NULL,0);
           switch(policy) {
              case SCHED_FIFO:
                   lptr+=sprintf(lptr,"%s ","rt: fifo");
              break;
              case SCHED_OTHER:
                   lptr+=sprintf(lptr,"%s ","other");
              break;
              case SCHED_IDLE:
                   lptr+=sprintf(lptr,"%s ","idle");
              break;
              case SCHED_BATCH:
                   lptr+=sprintf(lptr,"%s ","batch");
              break;
              case SCHED_RR:
                   lptr+=sprintf(lptr,"%s ","rt: rr");
              break;
              case SCHED_DEADLINE:
                   lptr+=sprintf(lptr,"%s ","rt: deadline");
              break;
              default:
                   lptr+=sprintf(lptr,"%s ","????");
              break;
           }
131 132 133
       break;
       default:
       break;	       	       	       	       	       
134
    }/* switch on fieldcnr */  
135
    procfile_fiels =strtok_r(NULL,toksep,&strtokptr); 
136
  } /* while on proc_fields != NULL */
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  prnt("%s\n",prntline); 
} /*decode_procstat */

void read_statfile(char *fname,int debug, telnet_printfunc_t prnt)
{
FILE *procfile;
char arecord[1024];

    procfile=fopen(fname,"r");
    if (procfile == NULL)
       {
       prnt("Error: Couldn't open %s %i %s\n",fname,errno,strerror(errno));
       return;
       }    
    if ( fgets(arecord,sizeof(arecord),procfile) == NULL)
       {
       prnt("Error: Nothing read from %s %i %s\n",fname,errno,strerror(errno));
       fclose(procfile);
       return;
       }    
    fclose(procfile);
    decode_procstat(arecord, debug, prnt);
}

void print_threads(char *buf, int debug, telnet_printfunc_t prnt)
{
char aname[256];

DIR *proc_dir;
struct dirent *entry;

168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

    prnt("  id          name            state   USRmod    KRNmod  prio nice   vsize   proc pol \n\n");
    snprintf(aname, sizeof(aname), "/proc/%d/stat", getpid());
    read_statfile(aname,debug,prnt);
    prnt("\n");
    snprintf(aname, sizeof(aname), "/proc/%d/task", getpid());
    proc_dir = opendir(aname);
    if (proc_dir == NULL)
       {
       prnt("Error: Couldn't open %s %i %s\n",aname,errno,strerror(errno));
       return;
       }
    
    while ((entry = readdir(proc_dir)) != NULL)
        {
        if(entry->d_name[0] == '.')
            continue;
	snprintf(aname, sizeof(aname), "/proc/%d/task/%s/stat", getpid(),entry->d_name);    
        read_statfile(aname,debug,prnt);      
        } /* while entry != NULL */
	closedir(proc_dir);
} /* print_threads */


int proccmd_show(char *buf, int debug, telnet_printfunc_t prnt)
{
   
   if (debug > 0)
       prnt(" proccmd_show received %s\n",buf);
198
   if (strcasestr(buf,"thread") != NULL) {
199
       print_threads(buf,debug,prnt);
200
   }
201
   if (strcasestr(buf,"loglvl") != NULL) {
202
       prnt("\n               component level  enabled   output\n");
203
       for (int i=MIN_LOG_COMPONENTS; i < MAX_LOG_COMPONENTS; i++) {
204
            if (g_log->log_component[i].name != NULL) {
205 206 207 208
               prnt("%02i %17.17s:%10.10s    %s      %s\n",i ,g_log->log_component[i].name, 
	             map_int_to_str(log_level_names,(g_log->log_component[i].level>=0)?g_log->log_component[i].level:g_log->log_component[i].savedlevel),
                     ((g_log->log_component[i].level>=0)?"Y":"N"),
                     ((g_log->log_component[i].filelog>0)?g_log->log_component[i].filelog_name:"stdout"));
209
           }
210 211
       }
   }
212
   if (strcasestr(buf,"logopt") != NULL) {
213
       prnt("\n               option      enabled\n");
214 215 216 217 218 219
       for (int i=0; log_options[i].name != NULL; i++) {
               prnt("%02i %17.17s %10.10s \n",i ,log_options[i].name, 
                     ((g_log->flag & log_options[i].value)?"Y":"N") );
       }
   }
   if (strcasestr(buf,"dbgopt") != NULL) {
220
       prnt("\n               module  debug dumpfile\n");
221
       for (int i=0; log_maskmap[i].name != NULL ; i++) {
222
               prnt("%02i %17.17s %5.5s   %5.5s\n",i ,log_maskmap[i].name, 
223
	             ((g_log->debug_mask &  log_maskmap[i].value)?"Y":"N"),
224
                     ((g_log->dump_mask & log_maskmap[i].value)?"Y":"N") );
225 226
       }
   }
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
   if (strcasestr(buf,"config") != NULL) {
       prnt("Command line arguments:\n");
       for (int i=0; i < config_get_if()->argc; i++) {
            prnt("    %02i %s\n",i ,config_get_if()->argv[i]);
       }
       prnt("Config module flags ( -O <cfg source>:<xxx>:dbgl<flags>): 0x%08x\n", config_get_if()->rtflags); 

       prnt("    Print config debug msg, params values (flag %u): %s\n",CONFIG_PRINTPARAMS,
            ((config_get_if()->rtflags & CONFIG_PRINTPARAMS) ? "Y" : "N") ); 
       prnt("    Print config debug msg, memory management(flag %u): %s\n",CONFIG_DEBUGPTR,
            ((config_get_if()->rtflags & CONFIG_DEBUGPTR) ? "Y" : "N") ); 
       prnt("    Print config debug msg, command line processing (flag %u): %s\n",CONFIG_DEBUGCMDLINE,
            ((config_get_if()->rtflags & CONFIG_DEBUGCMDLINE) ? "Y" : "N") );        
       prnt("    Don't exit if param check fails (flag %u): %s\n",CONFIG_NOABORTONCHKF,
            ((config_get_if()->rtflags & CONFIG_NOABORTONCHKF) ? "Y" : "N") );      
       prnt("Config source: %s,  parameters:\n",CONFIG_GETSOURCE );
       for (int i=0; i < config_get_if()->num_cfgP; i++) {
            prnt("    %02i %s\n",i ,config_get_if()->cfgP[i]);
       }
       prnt("Softmodem components:\n");
       prnt("   %02i Ru(s)\n", RC.nb_RU);
       prnt("   %02i lte RRc(s),     %02i NbIoT RRC(s)\n",    RC.nb_inst, RC.nb_nb_iot_rrc_inst);
       prnt("   %02i lte MACRLC(s),  %02i NbIoT MACRLC(s)\n", RC.nb_macrlc_inst, RC.nb_nb_iot_macrlc_inst);
       prnt("   %02i lte L1,	    %02i NbIoT L1\n",	     RC.nb_L1_inst, RC.nb_nb_iot_L1_inst);

       for(int i=0; i<RC.nb_inst; i++) {
           prnt("    lte RRC %i:     %02i CC(s) \n",i,((RC.nb_CC == NULL)?0:RC.nb_CC[i]));
       }
       for(int i=0; i<RC.nb_L1_inst; i++) {
           prnt("    lte L1 %i:      %02i CC(s)\n",i,((RC.nb_L1_CC == NULL)?0:RC.nb_L1_CC[i]));
       }
       for(int i=0; i<RC.nb_macrlc_inst; i++) {
           prnt("    lte macrlc %i:  %02i CC(s)\n",i,((RC.nb_mac_CC == NULL)?0:RC.nb_mac_CC[i]));
260 261 262 263 264 265 266 267 268 269
       }
   }
   return 0;
} 

int proccmd_thread(char *buf, int debug, telnet_printfunc_t prnt)
{
int bv1,bv2;   
int res;
char sv1[64]; 
270
 
271 272 273 274 275
   bv1=0;
   bv2=0;
   sv1[0]=0;
   if (debug > 0)
       prnt("proccmd_thread received %s\n",buf);
276 277 278 279
   if (strcasestr(buf,"help") != NULL) {
          prnt(PROCCMD_THREAD_HELP_STRING);
          return 0;
   } 
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
   res=sscanf(buf,"%i %9s %i",&bv1,sv1,&bv2);
   if (debug > 0)
       prnt(" proccmd_thread: %i params = %i,%s,%i\n",res,bv1,sv1,bv2);   
   if(res != 3)
     {
     prnt("softmodem thread needs 3 params, %i received\n",res);
     return 0;
     }

  
   if (strcasestr(sv1,"prio") != NULL)
       {
       set_sched(0,bv1, bv2);
       }
   else if (strcasestr(sv1,"aff") != NULL)
       {
       set_affinity(0,bv1, bv2);
       }
   else
       {
       prnt("%s is not a valid thread command\n",sv1);
       }
   return 0;
} 
int proccmd_exit(char *buf, int debug, telnet_printfunc_t prnt)
{
   if (debug > 0)
       prnt("process module received %s\n",buf);

   exit_fun("telnet server received exit command\n");
   return 0;
}
 
313

314 315 316 317
int proccmd_log(char *buf, int debug, telnet_printfunc_t prnt)
{
int idx1=0;
int idx2=NUM_LOG_LEVEL-1;
318 319 320
char *logsubcmd=NULL;

int s = sscanf(buf,"%ms %i-%i\n",&logsubcmd, &idx1,&idx2);   
321 322
   
   if (debug > 0)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
       prnt( "proccmd_log received %s\n   s=%i sub command %s\n",buf,s,((logsubcmd==NULL)?"":logsubcmd));

   if (s == 1 && logsubcmd != NULL) {
      if (strcasestr(logsubcmd,"online") != NULL) {
          if (strcasestr(buf,"noonline") != NULL) {
   	      set_glog_onlinelog(0);
              prnt("online logging disabled\n",buf);
          } else {
   	      set_glog_onlinelog(1);
              prnt("online logging enabled\n",buf);
          }
      }
      else if (strcasestr(logsubcmd,"show") != NULL) {
          prnt("Available log levels: \n   ");
          for (int i=0; log_level_names[i].name != NULL; i++)
             prnt("%s ",log_level_names[i].name);
339
          prnt("\n\n");
340 341 342
          prnt("Available display options: \n   ");
          for (int i=0; log_options[i].name != NULL; i++)
             prnt("%s ",log_options[i].name);
343
          prnt("\n\n");
344
          prnt("Available debug and dump options: \n   ");
345 346
          for (int i=0; log_maskmap[i].name != NULL; i++)
             prnt("%s ",log_maskmap[i].name);
347
          prnt("\n\n");
348
   	  proccmd_show("loglvl",debug,prnt);
349 350
   	  proccmd_show("logopt",debug,prnt);
   	  proccmd_show("dbgopt",debug,prnt);
351 352 353 354 355 356
      }
      else if (strcasestr(logsubcmd,"help") != NULL) {
          prnt(PROCCMD_LOG_HELP_STRING);
      } else {
          prnt("%s: wrong log command...\n",logsubcmd);
      }
357 358 359 360 361 362
   } else if ( s == 2 && logsubcmd != NULL) {
      char *opt=NULL;
      char *logparam=NULL;
      int  l;
      int optbit;

363
      l=sscanf(logsubcmd,"%m[^'_']_%ms",&logparam,&opt);
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
      if (l == 2 && strcmp(logparam,"print") == 0){
         optbit=map_str_to_int(log_options,opt);
         if (optbit < 0) {
            prnt("option %s unknown\n",opt);
         } else {
            if (idx1 > 0)    
                SET_LOG_OPTION(optbit);
            else
                CLEAR_LOG_OPTION(optbit);
            proccmd_show("logopt",debug,prnt);
         }
      }
      else if (l == 2 && strcmp(logparam,"debug") == 0){
         optbit=map_str_to_int(log_maskmap,opt);
         if (optbit < 0) {
379
            prnt("module %s unknown\n",opt);
380 381 382 383 384 385 386 387
         } else {
            if (idx1 > 0)    
                SET_LOG_DEBUG(optbit);
            else
                CLEAR_LOG_DEBUG(optbit);
            proccmd_show("dbgopt",debug,prnt);
         }
      }  
388
       else if (l == 2 && strcmp(logparam,"dump") == 0){
389 390
         optbit=map_str_to_int(log_maskmap,opt);
         if (optbit < 0) {
391
            prnt("module %s unknown\n",opt);
392 393
         } else {
            if (idx1 > 0)    
394
                SET_LOG_DUMP(optbit);
395
            else
396
                CLEAR_LOG_DUMP(optbit);
397 398 399 400 401
            proccmd_show("dbgopt",debug,prnt);
         }
      }       
      if (logparam != NULL) free(logparam);
      if (opt != NULL)      free(opt); 
402
   } else if ( s == 3 && logsubcmd != NULL) {
403
      int level, enable,filelog;
404 405 406 407
      char *tmpstr=NULL;
      char *logparam=NULL;
      int l;

408 409 410
      level = OAILOG_DISABLE - 1;
      filelog = -1;
      enable=-1; 
411 412 413 414 415 416
      l=sscanf(logsubcmd,"%m[^'_']_%m[^'_']",&logparam,&tmpstr);
      if (debug > 0)
          prnt("l=%i, %s %s\n",l,((logparam==NULL)?"\"\"":logparam), ((tmpstr==NULL)?"\"\"":tmpstr));
      if (l ==2 ) {
         if (strcmp(logparam,"level") == 0) {
             level=map_str_to_int(log_level_names,tmpstr);
417 418 419 420
             if (level < 0) {
                 prnt("level %s unknown\n",tmpstr);
                 level=OAILOG_DISABLE - 1;
              }
421 422 423 424 425
         } else {
             prnt("%s%s unknown log sub command \n",logparam, tmpstr);
         }
      } else if (l ==1 ) {
         if (strcmp(logparam,"enable") == 0) {
426
            enable=1;
427
         } else if (strcmp(logparam,"disable") == 0) {
428
             level=OAILOG_DISABLE;
429
         } else if (strcmp(logparam,"file") == 0) {
430
             filelog = 1 ;
431
         } else if (strcmp(logparam,"nofile") == 0) {
432
             filelog = 0 ;
433 434 435 436 437 438 439 440 441
         } else {
             prnt("%s%s unknown log sub command \n",logparam, tmpstr);
         }
      } else {
          prnt("%s unknown log sub command \n",logsubcmd); 
      }
      if (logparam != NULL) free(logparam);
      if (tmpstr != NULL)   free(tmpstr);
      for (int i=idx1; i<=idx2 ; i++) {
442 443 444 445 446
        if (level >= OAILOG_DISABLE)
           set_log(i, level);
        else if ( enable == 1)
           set_log(i,g_log->log_component[i].savedlevel);
        else if ( filelog == 1 ) {
447
           set_component_filelog(i);
448
        } else if ( filelog == 0 ) {
449 450
           close_component_filelog(i);
        } 
451 452 453
          
      }
     proccmd_show("loglvl",debug,prnt);
454 455 456
   } else {
       prnt("%s: wrong log command...\n",buf);
   }
457 458 459 460 461

   return 0;
} 
/*-------------------------------------------------------------------------------------*/

462
void add_softmodem_cmds(void)
463
{
464
   add_telnetcmd("softmodem",proc_vardef,proc_cmdarray);
465
}