0

The output of the ksta command is as follows:

PU423E3X16003871# ksta
Radio 1 client count 1
    MAC:4c:66:41:11:5d:a9 ip:60.60.70.6 ip_proto:ip ip_age:130 host:android-8710a239e8a8a97c vci:dhcpcd-5.5.6
        vlanid:0 Auth:Yes channel:136 rate:86Mbps rssi:35dB idle:3600s
        Rx bytes:12715 Tx bytes:4034 Rx rate:86Mbps Tx rate:86Mbps Rx last:130s Tx last:131s
        AssocID:1 Mode:normal Flags:0 PauseCnt:0 intf wl1.1 AssocTime: 2316s

Below is a parser that runs the ksta command and gets the values of 8 specified fields from the output. For each value, it runs a CFG_set function to register the variable.

The problem right now is that nothing is read from the fd. The first time fgets() is called, buf seems to be empty, and the function exits without actually reading anything.

Sometimes when I run this code, absolutely nothing is printed to the debug file.

Does anyone see what the issues are with my code?

static int set_sta_info()
{
    FILE * fd;
    FILE * fd_test;
    FILE * fd3;
    char buf[CMD_BUF_LENGTH];
    char* key_val;
    char* start;
    char* value;
    char* key;
    int i;
    char* keys[8] = {"MAC", "ip", "host", "vci", "vlanid", "Auth", "rssi", "AssocTime"};
    //fd = popen("ksta > /tmp/ksta_output.txt", "r");
    fd = popen("cw_diag ksta", "r");
    fd_test = fopen("/tmp/sta_info_debug.txt", "a");
    fprintf(fd_test, "entering set_sta_info()\n");
    if (!fd) return -1;
    // Station Mac, Ip, Hostname, vlanid, Auth, Rate, rssi, interface (wl1.1), Assoc Time.
    while (1)
    {   fprintf(fd_test, "inside while loop");
        buf[0] = 0;
        for (i = 0; i < CMD_BUF_LENGTH; i++){
            buf[i] = 0;
        }
        fgets(buf, sizeof(buf), fd);
        fprintf(fd_test, " line is %s\n", buf);
        if (buf[0] == 0) break;
        fprintf(fd_test, "buf is not empty, didn't break\n");
        key_val = strtok(buf, ' ');
        fprintf(fd_test, "line 2808.\n");
        while (key_val) {
            fprintf(fd_test, "current element examining: %s\n", key_val);
            for (i = 0; i < 8; i++){
                fprintf(fd_test, "looking at key %s\n", keys[i]);
                if (strstr(key_val, keys[i])){
                    fprintf(fd_test, "%s is found in %s\n", keys[i], key_val);
                    start = strstr(buf, ':');
                    if (!start){
                        fprintf(fd_test, "but, there is no :, so skipped.\n");
                        continue;
                    }
                    start ++;
                    while (*start && (*start == ' ' || *start == '\t')) start++;
                    fprintf(fd_test, "the word to be copied is %s\n", start);
                    strncpy(value, start, MAX_NAME_LEN);
                    CFG_set_by_name(keys[i], value);
                    fprintf(fd_test, "key is %s, value is %s\n", keys[i], value);
                }
            }
            key_val = strtok(NULL, ' ');
        }
    }
    fclose(fd);
         fclose(fd_test);
    return 0;
}
ajfbiw.s
  • 401
  • 1
  • 8
  • 22
  • 1
    Add `fflush(fd_test);` after every `fprintf(fd_test, ...` Its important with logging code to flush immediately otherwise the output may not appear as expected. – john Aug 29 '18 at 09:03
  • 1
    First make sure you flush the output with `fflush(fd_test)` every time you print some debugging, otherwise the debugging output could be buffered and not shown. You are also running `cw_diag ksta` , but in your other example you are only running `ksta` (perhaps you ran `cw_diag` as a standalone command first, and if you do this in 2 steps, it might be very different). If you manually run `cw_diag ksta` as one command, do you get the expected output ? It could even be that the `cw_diag` command is only designed to be run from an actual terminal. – nos Aug 29 '18 at 09:03
  • Well, a few problems: If the `popen()` returns `NULL`, you never close your `fd_test` logfile (Nor do you log anything about the failure). The command you're trying to run via popen is different than in your first snippet? You're using `fclose()` instead of `pclose()` to close `fd`. You're probably getting warnings about const pointers. You're using `strncpy()`. You seem to be copying into an uninitialized pointer (`value`), which is not going to end well.... Always compile with a healthy set of warnings (`-Wall -Wextra`) and pay attention to them. It helps. – Shawn Aug 29 '18 at 09:06
  • Did the `popen` call succeed ? Why the `cw_diag` ? Does `ksta` depend on any environment hat might not be available ? Does your process have the appropriate permissions ? Does `ksta` write to stdout ? A lot of possible causes ... – Sander De Dycker Aug 29 '18 at 09:06
  • ksta is an alias for cw_diag ksta. I have tried cw_diag ksta > file.txt inside popen and it works. – ajfbiw.s Aug 29 '18 at 09:07
  • Possibly the `cw_diag ksta` command is failing for some reason, printing to stderr, and exiting without ever printing to stdout. Try changing the command to `cw_diag ksta 2>&1` as described in [this question](https://stackoverflow.com/questions/6900577/c-popen-wont-catch-stderr) – Jim Janney Aug 31 '18 at 00:08

0 Answers0