Skip to content

Commit

Permalink
feat:add /var/run/rtty file to get the state of rtty (#105)
Browse files Browse the repository at this point in the history
Other programs, such as Luci, can display the connection status of the
program by reading /var/run/rtty

Signed-off-by: Jiajian Zhou <zhou_0611@163.com>
  • Loading branch information
richard611 authored May 7, 2022
1 parent 66c3a02 commit 3afa2ed
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <glob.h>

#include "log/log.h"
#include "utils.h"
#include "rtty.h"

enum {
Expand Down Expand Up @@ -241,6 +242,8 @@ int main(int argc, char **argv)
load_default_ca_cert(rtty.ssl_ctx);
#endif

rtty_run_state(RTTY_STATE_DISCONNECTED);

if (rtty_start(&rtty) < 0)
return -1;

Expand Down
3 changes: 3 additions & 0 deletions src/rtty.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ void rtty_exit(struct rtty *rtty)

http_conns_free(&rtty->http_conns);

rtty_run_state(RTTY_STATE_DISCONNECTED);

if (!rtty->reconnect)
ev_break(rtty->loop, EVBREAK_ALL);
}
Expand Down Expand Up @@ -399,6 +401,7 @@ static int parse_msg(struct rtty *rtty)
}
buffer_pull(rb, NULL, msglen - 1);
log_info("register success\n");
rtty_run_state(RTTY_STATE_CONNECTED);
break;

case MSG_TYPE_LOGIN:
Expand Down
37 changes: 37 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>

#include "utils.h"
#include "log/log.h"

int find_login(char *buf, int len)
{
Expand Down Expand Up @@ -222,3 +224,38 @@ bool getgid_by_pid(pid_t pid, gid_t *gid)

return true;
}

void rtty_run_state(int state)
{
static int _st = -1;
const char *rtty_statefile = "/var/run/rtty";
FILE *sf;
const char *str_state = "Disconnected\n";

if (_st == state) {
return;
}

_st = state;

sf = fopen(rtty_statefile, "w+");
if (!sf) {
log_err("Cannot create state %s: %s", rtty_statefile, strerror(errno));
return;
}

switch(_st){
case RTTY_STATE_CONNECTED:
str_state = "Connected\n";
break;
default:
break;
}

fwrite(str_state, strlen(str_state), 1, sf);

fsync(fileno(sf));
fclose(sf);

return;
}
7 changes: 7 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ bool getuid_by_pid(pid_t pid, uid_t *uid);

bool getgid_by_pid(pid_t pid, gid_t *gid);

enum {
RTTY_STATE_CONNECTED = 1,
RTTY_STATE_DISCONNECTED
};

void rtty_run_state(int state);

#endif

0 comments on commit 3afa2ed

Please sign in to comment.