Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add /var/run/rtty file to get the state of rtty #105

Merged
merged 1 commit into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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