Ignore:
Timestamp:
May 3, 2021, 10:56:05 AM (3 years ago)
Author:
Sanahuja Guillaume
Message:

use utmp for autdetecting ip address

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FlairCore/src/FrameworkManager_impl.cpp

    r414 r424  
    3535#include <signal.h>
    3636#include <fcntl.h>
     37#include <utmp.h>
    3738#ifdef __XENO__
    3839#include <native/task.h>
     
    222223  }
    223224  if(address=="autodetect") {
    224     Printf("autodetecting gcs ip address based on ssh connection\n");
     225    Printf("Autodetecting gcs ip address based on ssh connection\n");
    225226    address=AutoDetectGCS();
    226227  }
     
    253254
    254255string FrameworkManager_impl::AutoDetectGCS(void) {
    255     FILE *fp;
    256     char result[15];
    257 
    258     //get only first result and remove line feed
    259     fp = popen("who | awk '{ print $7; }' | head -n 1 | tr -d '\n'", "r");
    260     if (fp == NULL) {
    261         Err("Failed to run autodetect command\n" );
    262     }
    263 
    264     fgets(result, sizeof(result), fp);
    265     pclose(fp);
    266    
    267     return result;
     256  int logsize = 10;//should be sufficient
     257  int file;
     258  struct utmp log[logsize];
     259
     260  file = open("/var/run/utmp", O_RDONLY);
     261
     262//return only first matching occurence
     263//TODO: check also log->ut_addr_v6[0]
     264  if (file) {
     265    int read_size=read(file, &log, sizeof(log));
     266    int nb=read_size/sizeof(struct utmp);
     267 
     268    for(int i = 0; i < nb; i++) {
     269      //printf("\n ut_user: %s host: %s %x\n ut_type: %ld line %s\n", log[i].ut_user,log[i].ut_host,log[i].ut_addr_v6[0], log[i].ut_type,log[i].ut_line);
     270      if(log[i].ut_type==USER_PROCESS) {
     271        close(file);
     272        return log[i].ut_host;
     273      }
     274   
     275    }
     276
     277    close(file);
     278  }
     279 
     280  Err("Failed to run autodetect command\n" );
     281  return "";
    268282}
    269283
Note: See TracChangeset for help on using the changeset viewer.