1 2 3 4 5 6 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <signal.h>
#include <netax25/procutils.h>
#include <netax25/axlib.h>
#include "node.h"
#include "sysinfo.h"
struct user User = {0};
static long CallerPos = -1L;
void login_user(void)
{
FILE *f;
struct user u;
long pos = 0L;
long free = -1L;
struct stat statbuf;
if (stat(DATA_NODE_LOGIN_FILE, &statbuf) == -1) {
node_perror(DATA_NODE_LOGIN_FILE, errno);
return;
}
if (statbuf.st_size % sizeof(struct user) != 0) {
node_msg("%s: Incorrect size", DATA_NODE_LOGIN_FILE);
node_log(LOGLVL_ERROR, "%s: Incorrect size", DATA_NODE_LOGIN_FILE);
return;
}
time(&User.logintime);
User.cmdtime = User.logintime;
User.pid = getpid();
if ((f = fopen(DATA_NODE_LOGIN_FILE, "r+")) == NULL) {
node_perror(DATA_NODE_LOGIN_FILE, errno);
return;
}
if (flock(fileno(f), LOCK_EX) == -1) {
node_perror("login_user: flock", errno);
fclose(f);
return;
}
while (fread(&u, sizeof(u), 1, f) == 1) {
if (u.pid == -1 || (kill(u.pid, 0) == -1 && errno == ESRCH)) {
free = pos;
break;
}
pos += sizeof(u);
}
if (free != -1L && fseek(f, free, 0L) == -1) {
node_perror("login_user: fseek", errno);
fclose(f);
return;
}
fflush(f);
CallerPos = ftell(f);
fwrite(&User, sizeof(User), 1, f);
fflush(f);
flock(fileno(f), LOCK_UN);
fclose(f);
}
void logout_user(void)
{
FILE *f;
if (CallerPos == -1L)
return;
if ((f = fopen(DATA_NODE_LOGIN_FILE, "r+")) == NULL) {
node_perror(DATA_NODE_LOGIN_FILE, errno);
return;
}
if (fseek(f, CallerPos, 0) == -1) {
node_perror("logout_user: fseek", errno);
fclose(f);
return;
}
User.pid = -1;
fwrite(&User, sizeof(User), 1, f);
fclose(f);
}
void update_user(void)
{
FILE *f;
if (CallerPos == -1L)
return;
if ((f = fopen(DATA_NODE_LOGIN_FILE, "r+")) == NULL) {
node_perror(DATA_NODE_LOGIN_FILE, errno);
return;
}
if (fseek(f, CallerPos, 0) == -1) {
node_perror("update_user: fseek", errno);
fclose(f);
return;
}
fwrite(&User, sizeof(User), 1, f);
fclose(f);
}
int user_count(void)
{
FILE *f;
struct user u;
int cnt = 0;
if ((f = fopen(DATA_NODE_LOGIN_FILE, "r")) == NULL) {
node_perror(DATA_NODE_LOGIN_FILE, errno);
return 0;
}
while (fread(&u, sizeof(u), 1, f) == 1)
if (u.pid != -1 && (kill(u.pid, 0) != -1 || errno != ESRCH))
cnt++;
fclose(f);
return cnt;
}
int user_list(int argc, char **argv)
{
FILE *f;
struct user u;
struct tm *tp;
struct proc_nr_nodes *np;
char buf[80];
time_t l;
axio_puts("",NodeIo);
if ((f = fopen(DATA_NODE_LOGIN_FILE, "r")) == NULL) {
node_perror(DATA_NODE_LOGIN_FILE, errno);
return 0;
}
if (User.ul_type == AF_NETROM) {
axio_printf(NodeIo, "%s} %s", NodeId, VERSION);
}
if (User.ul_type == AF_INET) {
axio_printf(NodeIo, "\e[01;35m");
} else {
axio_printf(NodeIo, "Current users:");
}
if (user_count() == 0) {
axio_printf(NodeIo, " No users online.\n");
}
if (User.ul_type == AF_INET) {
axio_printf(NodeIo,"\e[0;m");
}
if (user_count() != 0) /* axio_printf(NodeIo,"") */ ;
while (fread(&u, sizeof(u), 1, f) == 1) {
if (u.pid == -1 || (kill(u.pid, 0) == -1 && errno == ESRCH))
continue;
switch (u.ul_type) {
case AF_FLEXNET:
sprintf(buf, "\nFlexNet (%.9s)",
u.call);
break;
case AF_AX25:
sprintf(buf, "\nUplink (%.9s on interface %.10s)",
u.call, u.ul_name);
break;
case AF_NETROM:
if ((np = find_node(u.ul_name, NULL)) != NULL) {
sprintf(buf, "\nCircuit (%.9s %.18s)",
u.call,
print_node(np->alias, np->call));
} else {
sprintf(buf, "\nCircuit (%.9s %.18s)",
u.call, u.ul_name);
}
break;
#ifdef HAVE_ROSE
case AF_ROSE:
sprintf(buf, "\nROSE (%.9s %.18s)",
u.call, u.ul_name);
break;
#endif
case AF_INET:
sprintf(buf, "\nTelnet (%.9s @ %.16s)",
u.call, u.ul_name);
break;
case AF_INET6:
sprintf(buf, "\nTelnet6 (%.9s via IPv6)", u.call);
break;
case AF_UNSPEC:
sprintf(buf, "\nHost (%.9s on local)",
u.call);
break;
default:
sprintf(buf, "\n?????? (%.9s %.18s)",
u.call, u.ul_name);
break;
}
axio_printf(NodeIo,"%-37.37s ", buf);
switch (u.state) {
case STATE_QUIT:
logout_user();
break;
case STATE_LOGIN:
axio_puts(" -> Logging in",NodeIo);
break;
case STATE_IDLE:
time(&l);
l -= u.cmdtime;
tp = gmtime(&l);
axio_printf(NodeIo," -> Idle (%d:%02d:%02d:%02d)",
tp->tm_yday, tp->tm_hour,
tp->tm_min, tp->tm_sec);
break;
case STATE_TRYING:
switch (u.dl_type) {
case AF_FLEXNET:
axio_printf(NodeIo," -> Trying (%s)",
u.dl_name);
break;
case AF_AX25:
axio_printf(NodeIo," -> Trying (%s on interface %s)",
u.dl_name, u.dl_port);
break;
case AF_NETROM:
axio_printf(NodeIo," -> Trying (%s)",
u.dl_name);
break;
#ifdef HAVE_ROSE
case AF_ROSE:
axio_printf(NodeIo," -> Trying (%s)",
u.dl_name);
break;
#endif
case AF_INET:
axio_printf(NodeIo," -> Trying (%s:%s)",
u.dl_name, u.dl_port);
break;
default:
axio_puts(" -> ???",NodeIo);
break;
}
break;
case STATE_CONNECTED:
switch (u.dl_type) {
case AF_FLEXNET:
axio_printf(NodeIo,"<--> FlexNet (%s)",
u.dl_name);
break;
case AF_AX25:
axio_printf(NodeIo,"<--> Downlink (%s on interface %s)",
u.dl_name, u.dl_port);
break;
case AF_NETROM:
axio_printf(NodeIo,"<--> Circuit (%s)",
u.dl_name);
break;
#ifdef HAVE_ROSE
case AF_ROSE:
axio_printf(NodeIo,"<--> ROSE (%s)",
u.dl_name);
break;
#endif
case AF_INET:
axio_printf(NodeIo,"<--> Telnet (%s:%s)",
u.dl_name, u.dl_port);
break;
default:
axio_printf(NodeIo,"<--> ???");
break;
}
break;
case STATE_PINGING:
axio_printf(NodeIo,"<--> Pinging (%s)", u.dl_name);
break;
case STATE_EXTCMD:
axio_printf(NodeIo,"<--> Extcmd (%s)", u.dl_name);
break;
default:
axio_puts(" -> ??????",NodeIo);
break;
}
axio_puts("",NodeIo);
}
if (User.ul_type == AF_NETROM) {
node_msg("");
}
fclose(f);
return 0;
}
|