xrxs.c (raw)
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
#include <u.h> #include <libc.h> #include <fcall.h> #include <thread.h> #include <9p.h> #include <stdio.h> #include <dirent.h> #include <libString.h> #include <string.h> #include "err.h" #include "command.h" #include "util.h" #include "aux.h" #include "cart.h" #include "universe.h" #include "realm.h" #include "user.h" #define CARTSPATH "./carts" #define REALMSPATH "./realms" int chatty9p = 1; static Tree* tree; static UserInfo users_table[64]; void xrxs_attach(Req* r) { /* As it is, once the user detaches, they will stay in the table * until the server restarts. We have to figure out some way * to detect a detach to remove them... You can't delay respond() * or the attach will never complete. We have logout() for now... */ int i = 0; int vacancy = 0; char* usr; char* username = r->ifcall.uname; for (i = 0; i < 64; i++) { usr = users_table[i].name; if (scmp(usr, username)) { respond(r, EUNAME); return; } if (*usr == 0) { scpy(username, usr, 32); vacancy = 1; break; } } if (vacancy) { respond(r, nil); } else { respond(r, EUFULL); } } void write_ctl(Req* r) { char cmd[16] = {0}; char* c = r->ifcall.data; int i; for (i = 0; i < r->ifcall.count && *c != ' ' && *c != '\n'; i++) { ccat(cmd, *c++); } if (*c == ' ') c++; scsw(c, '\n', 0); fprintf(stderr, "%s(%s)\n", cmd, c); uvlong const cmd_hashv = hash(cmd, 0); switch (cmd_hashv) { case LOGIN: login(users_table, r->fid->uid, c); break; case LOAD: load_cart(users_table, r->fid->uid, c); break; case CHUNK: get_chunk(users_table, r->fid->uid, c); break; case CREATE: create_realm(users_table, r->fid->uid, c); break; case PROTECT: protect_realm(users_table, r->fid->uid, c); break; case TRANSFER: transfer_realm(users_table, r->fid->uid, c); break; case ENTER: enter_realm(users_table, r->fid->uid, c); break; case LEAVE: leave_realm(users_table, r->fid->uid); break; case LOGOUT: logout(users_table, r->fid->uid); break; case SAVE: // save(r->fid->uid); break; case RESET: // reset(r->fid->uid); break; case UNLOAD: unload_cart(users_table, r->fid->uid); break; } r->ofcall.count = r->ifcall.count; r->fid->file->dir.length = r->ifcall.count; respond(r, nil); } void write_universe(Req* r) { char key[16] = {0}; char value[64] = {0}; char* c = r->ifcall.data; UserInfo* u = find_user(users_table, r->fid->uid); Atom* a; int i; for (i = 0; i < 15 && i < r->ifcall.count && *c != ' ' && *c != '\n'; i++) { ccat(key, *c++); } c++; for (i = 0; i < 63 && i < r->ifcall.count && *c != ' ' && *c != '\n'; i++) { ccat(value, *c++); } if (u != nil && u->realm != nil && u->realm->universe != nil) { a = get_atom(u->realm->universe, key); if (a != nil) { scpy(value, a->value, 64); } else { a = malloc(sizeof(Atom)); scpy(key, a->name, 16); scpy(value, a->value, 64); a->next = nil; set_atom(u->realm->universe, a); } } r->ofcall.count = r->ifcall.count; r->fid->file->dir.length = r->ifcall.count; respond(r, nil); } void xrxs_write(Req* r) { Aux* a = r->fid->file->aux; switch (a->type) { case CTL: write_ctl(r); break; case UNIVERSE: write_universe(r); break; case SCOPE: // write_scope(r); default: respond(r, nil); break; } } void read_users(Req* r) { char buf[2113] = {0}; int i; for (i = 0; i < 64; i++) { if (scmp(users_table[i].name, r->fid->uid)) { scat(buf, users_table[i].name); ccat(buf, '\n'); break; } } for (i = 0; i < 64; i++) { if ( scmp(users_table[i].name, "\0") || scmp(users_table[i].name, r->fid->uid)) { continue; } scat(buf, users_table[i].name); ccat(buf, '\n'); } ccat(buf, 0); readstr(r, buf); respond(r, nil); } String** list_dir(char* path) { int size = 128; String** self = malloc(size * sizeof(String*)); DIR* dir; struct dirent* ent; int i = 0; char* c; if ((dir = opendir(path)) != NULL) { while ((ent = readdir(dir)) != NULL) { if (i = size) { size *= 2; self = realloc(self, size * sizeof(String*)); } c = ent->d_name; if (scmp(c, ".") || scmp(c, "..")) { continue; } self[i] = s_new(); while (*c) { s_putc(self[i], *c++); } s_terminate(self[i++]); } closedir(dir); } self[i] = nil; return self; } void s_freemany(String** ss) { int i; String** s = ss; for (i = 0; i < 128; i++) { if (*s != nil) { s_free(*s); } } free(ss); } void read_carts(Req* r) { String** carts = list_dir(CARTSPATH); String** c = carts; String* data = s_new(); while (*c != nil) { s_append(data, (*c)->base); s_putc(data, '\n'); c++; } s_terminate(data); readstr(r, data->base); respond(r, nil); s_free(data); s_freemany(carts); } void read_slot(Req* r) { UserInfo* u = find_user(users_table, r->fid->uid); if (u == nil) goto end; if (u->cart == nil) goto end; readstr(r, u->cart->rom); end: respond(r, nil); } void read_data(Req* r, FileType t) { UserInfo* u = find_user(users_table, r->fid->uid); if (u->cart == nil) goto end; switch (t) { case SPRITE_DATA: readstr(r, u->cart->sprite_data); break; case AUDIO_DATA: readstr(r, u->cart->audio_data); break; case TEXT_DATA: readstr(r, u->cart->txt_data); break; } end: respond(r, nil); } void read_realms(Req* r) { UserInfo* user = find_user(users_table, r->fid->uid); char realm_path[128] = {0}; String** realms; String** rr; Realm* realm; String* data = s_new(); int i, u, m, p; char ubuf[8] = {0}; char mbuf[8] = {0}; char pbuf[2] = {0}; if (user->cart == nil) respond(r, nil); scat(realm_path, "carts/"); scat(realm_path, user->cart->name); scat(realm_path, "/realms"); realms = list_dir(realm_path); rr = realms; while (*rr != nil) { s_append(data, (*rr)->base); s_putc(data, ' '); realm = parse_realm(user->cart->name, (*rr)->base); m = realm->max; p = realm->password ? 1 : 0; for (i = u = 0; i < 64; i++) { if ( users_table[i].realm != nil && scmp(users_table[i].realm->name, realm->name)) u++; } itoa(u, ubuf, 10); s_append(data, ubuf); s_putc(data, ' '); itoa(m, mbuf, 10); s_append(data, mbuf); s_putc(data, ' '); itoa(p, pbuf, 10); s_append(data, pbuf); s_putc(data, '\n'); rr++; } s_terminate(data); readstr(r, data->base); respond(r, nil); s_free(data); s_freemany(realms); } void read_universe(Req* r) { char* uname = r->fid->uid; UserInfo* u = find_user(users_table, uname); String* data = s_new(); Universe* universe; Atom* a; int i; if (u == nil || u->realm == nil) { respond(r, nil); return; } universe = u->realm->universe; for (i = 0; i < 256; i++) { a = universe->atoms[i]; while (a != nil) { s_append(data, a->name); s_append(data, " = "); s_append(data, a->value); s_putc(data, '\n'); a = a->next; } } s_terminate(data); readstr(r, data->base); respond(r, nil); s_free(data); } void xrxs_read(Req* r) { Aux* a = r->fid->file->aux; switch (a->type) { case USERS: read_users(r); break; case CARTS: read_carts(r); break; case SLOT: read_slot(r); break; case SPRITE_DATA: case AUDIO_DATA: case TEXT_DATA: read_data(r, a->type); break; case REALMS: read_realms(r); break; case UNIVERSE: read_universe(r); break; case SCOPE: // read_scope(r); break; case RANDOM: // read_random(r); break; case GRANDOM: // read_grandom(r); break; default: respond(r, nil); break; } } void fs_destroy_file(File* f) { Aux* a = f->aux; if (a && a->data) { free(a->data); free(a); } else if (a) { free(a); } } Srv fs = {.attach = xrxs_attach, .read = xrxs_read, .write = xrxs_write}; int threadmaybackground(void) { return 1; } void threadmain(int argc, char* argv[]) { char* mtpt = nil; char* usocket = nil; int i; /* if -h CMD is given, print the hash value of CMD */ if (argc == 3 && scmp(argv[1], "-h")) { printf("%llu\n", hash(argv[2], 0)); return; } /* if -m PATH is supplied, mount on PATH */ /* if -s NAME is supplied, create a socket for the namespace */ /* otherwise, just use srv() (for wrapping with socat or inetd) */ if (argc >= 3) { for (i = 0; i < argc; i++) { if (scmp(argv[i], "-m")) { mtpt = argv[++i]; printf("serving on %s", mtpt); } else if (scmp(argv[i], "-s")) { usocket = argv[++i]; printf("serving socket namespace %s", usocket); } } } fs.foreground = 1; fs.tree = alloctree(nil, nil, DMDIR | 0777, fs_destroy_file); tree = fs.tree; closefile( createfile(tree->root, "ctl", nil, DMAPPEND | 0300, create_aux(CTL))); closefile(createfile(tree->root, "carts", nil, 0400, create_aux(CARTS))); closefile(createfile(tree->root, "users", nil, 0400, create_aux(USERS))); closefile(createfile(tree->root, "slot", nil, 0400, create_aux(SLOT))); closefile(createfile(tree->root, "data", nil, DMDIR | 0500, nil)); closefile(createfile( walkfile(tree->root, "data"), "sprite", nil, 0400, create_aux(SPRITE_DATA))); closefile(createfile( walkfile(tree->root, "data"), "audio", nil, 0400, create_aux(AUDIO_DATA))); closefile(createfile( walkfile(tree->root, "data"), "text", nil, 0400, create_aux(TEXT_DATA))); closefile(createfile(tree->root, "realms", nil, 0400, create_aux(REALMS))); closefile(createfile( tree->root, "universe", nil, DMAPPEND | 0600, create_aux(UNIVERSE))); closefile( createfile(tree->root, "scope", nil, DMAPPEND | 0600, create_aux(SCOPE))); closefile(createfile(tree->root, "random", nil, 0400, create_aux(RANDOM))); closefile(createfile(tree->root, "grandom", nil, 0400, create_aux(GRANDOM))); if (argc >= 3) { if (mtpt != nil && access(mtpt, AEXIST) < 0 && access(mtpt, AEXIST) < 0) sysfatal("mountpoint %s does not exist", mtpt); threadpostmountsrv(&fs, usocket, mtpt, MREPL | MCREATE); threadexits(0); } else { srv(&fs); } } |