Overview
Comment: | udpate xpriv to use sysv shmem by default; give iaia an option to generate its own types, and allow selecting between 7-bit and 8-bit encodings for ascii (defaulting to 8-bit); update mkpw to work with new iaia; update ord to add flag controlling byte length (7 or 8) for iaia's ascii mode |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
6a14de18116c33e773d7050066650a2c |
User & Date: | lexi on 2019-07-20 23:37:03 |
Other Links: | manifest | tags |
Context
2019-07-21
| ||
06:15 | add clipserv check-in: 5e680bdd13 user: lexi tags: trunk | |
2019-07-20
| ||
23:37 | udpate xpriv to use sysv shmem by default; give iaia an option to generate its own types, and allow selecting between 7-bit and 8-bit encodings for ascii (defaulting to 8-bit); update mkpw to work with new iaia; update ord to add flag controlling byte length (7 or 8) for iaia's ascii mode check-in: 6a14de1811 user: lexi tags: trunk | |
18:12 | fixes check-in: 00358989c7 user: lexi tags: trunk | |
Changes
Modified clib/iaia.c from [f7f2775d1b] to [1d0d9f385e].
1 2 3 4 5 6 7 8 9 10 11 .. 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 ... 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
/* [ʞ] iaia.c * ~ lexi hale <lexi@hale.su> * # include "iaia.c" * # define _IAIA_FN_{ATOI,ITOA,ASCTOI,ITOASC} * : typedef iaia_word_type, * iaia_error_type * : enum iaia_e_domain, * iaia_e_base, * iaia_e_overflow * ? arbitrary-base integer conversion functions */ ................................................................................ #endif #ifndef _IAIA_FN_ASCTOI # define _IAIA_FN_ASCTOI asctoi #endif #ifndef _IAIA_FN_ITOASC # define _IAIA_FN_ITOASC itoasc #endif enum /* constants */ { /* ascii address space */ numspace = (0x39 - 0x30) + 1, /* 10 */ alphaspace = (0x5a - 0x41) + 1, /* 26 */ smallalphaspace = (0x7a - 0x61) + 1, /* 26 */ /* base representations */ imaxbase = numspace + alphaspace, /* 36 */ maxbase = imaxbase + smallalphaspace /* 62 */ }; /* -- string to integer converters -- */ iaia_error_type _IAIA_FN_ASCTOI(const char* s, iaia_word_type* ret) { iaia_word_type val = 0; enum { base = 128 }; for (;*s!=null;++s) { uint8_t v = *s; if (v > base) return iaia_e_domain; val *= base; val += v; } *ret = val; return ok; } ................................................................................ iaia_error_type _IAIA_FN_ITOASC(iaia_word_type val, const char* buf_start, char* buf_end, char** newbuf) { char* ptr = buf_end; *ptr-- = 0; while(val > 0) { if (ptr < buf_start) return iaia_e_overflow; iaia_word_type rem = val % 128; val /= 128; *ptr-- = (char)rem; } if (newbuf != null) *newbuf = ptr + 1; return ok; } |
> > > > > > > > > > > > > > > > > > > < | > | > > > | > |
1 2 3 4 5 6 7 8 9 10 11 12 .. 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 ... 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
/* [ʞ] iaia.c * ~ lexi hale <lexi@hale.su> * # include "iaia.c" * # define _IAIA_FN_{ATOI,ITOA,ASCTOI,ITOASC} * _IAIA_EXP_ASCFORM * : typedef iaia_word_type, * iaia_error_type * : enum iaia_e_domain, * iaia_e_base, * iaia_e_overflow * ? arbitrary-base integer conversion functions */ ................................................................................ #endif #ifndef _IAIA_FN_ASCTOI # define _IAIA_FN_ASCTOI asctoi #endif #ifndef _IAIA_FN_ITOASC # define _IAIA_FN_ITOASC itoasc #endif #ifndef _IAIA_EXP_ASCFORM /* this is an expression that will be evaluated to * determine whether to compress ascii to a 7-bit * representation or to store it in 8-bit space. * 1 = 7-bit (compressed) * 0 = 8-bit (uncompressed) */ # define _IAIA_EXP_ASCFORM (0) #endif enum /* constants */ { base7bit = 128, /* ascii address space */ numspace = (0x39 - 0x30) + 1, /* 10 */ alphaspace = (0x5a - 0x41) + 1, /* 26 */ smallalphaspace = (0x7a - 0x61) + 1, /* 26 */ /* base representations */ imaxbase = numspace + alphaspace, /* 36 */ maxbase = imaxbase + smallalphaspace /* 62 */ }; #ifndef _IAIA_EXTERNAL_TYPES typedef enum iaia_error_type { iaia_e_ok, iaia_e_domain, iaia_e_base, iaia_e_overflow, } iaia_error_type; typedef unsigned long long iaia_word_type; #endif /* -- string to integer converters -- */ iaia_error_type _IAIA_FN_ASCTOI(const char* s, iaia_word_type* ret) { iaia_word_type val = 0; for (;*s!=null;++s) { uint8_t v = *s; if (v > base7bit) return iaia_e_domain; if (_IAIA_EXP_ASCFORM) val *= base7bit; else val <<= 8; val += v; } *ret = val; return ok; } ................................................................................ iaia_error_type _IAIA_FN_ITOASC(iaia_word_type val, const char* buf_start, char* buf_end, char** newbuf) { char* ptr = buf_end; *ptr-- = 0; while(val > 0) { if (ptr < buf_start) return iaia_e_overflow; iaia_word_type rem = val % 128; if (_IAIA_EXP_ASCFORM) val /= base7bit; else val >>= 8; *ptr-- = (char)rem; } if (newbuf != null) *newbuf = ptr + 1; return ok; } |
Modified mkpw.c from [2857320b30] to [4060441d9a].
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
enum /* iaia errors */ { iaia_e_ok = ok, iaia_e_base = fail, iaia_e_domain = fail, iaia_e_overflow = fail, }; #define _IAIA_FN_ATOI katoi #include "clib/iaia.c" typedef enum { tbl_ok = ok, tbl_error = fail } tbl_error_type; typedef unsigned char tbl_word_type; #include "clib/tbl.c" |
> |
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
enum /* iaia errors */ {
iaia_e_ok = ok,
iaia_e_base = fail,
iaia_e_domain = fail,
iaia_e_overflow = fail,
};
#define _IAIA_FN_ATOI katoi
#define _IAIA_EXTERNAL_TYPES
#include "clib/iaia.c"
typedef enum {
tbl_ok = ok, tbl_error = fail
} tbl_error_type;
typedef unsigned char tbl_word_type;
#include "clib/tbl.c"
|
Modified ord.c from [8e7c9355c5] to [9504e2c85c].
81 82 83 84 85 86 87 88 89 90 91 92 93 94 ... 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 ... 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 ... 197 198 199 200 201 202 203 204 205 206 207 208 209 210 ... 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
arg_asc, arg_bin, arg_trn, arg_oct, arg_dec, arg_duo, arg_hex, arg_b32, arg_b64, switch_prefix, param_prefix, switch_lowercase, arg_ebcdic, }; word bases[] = { [arg_asc] = 0, [arg_bin] = 2, ................................................................................ {arg_duo, "duo"}, {arg_duo, "duodecimal"}, {arg_hex, "hex"}, {arg_hex, "hexadecimal"}, {arg_hex, "b32"}, {arg_hex, "base32"}, /* not padded! */ {switch_prefix, "-p"}, {switch_prefix, "--prefix"}, {switch_lowercase, "-l"}, {switch_lowercase, "--lowercase"}, {param_prefix, "-m"}, {param_prefix, "--manual-prefix"}, {arg_ebcdic, "ebcdic"}, }; /* import the conversion utilities */ typedef bad iaia_error_type; ................................................................................ typedef word iaia_word_type; enum /* iaia synonyms */ { iaia_e_ok = ok, iaia_e_domain = bad_domain, iaia_e_base = bad_base, iaia_e_overflow = bad_overflow, }; #include "clib/iaia.c" bad run(const int argc, const char** argv) { # ifndef _POSIX_IO /* fuck your buffering, it only ever makes * things worse */ setvbuf(stdout,null,_IONBF); ................................................................................ bool raw = false; bool prefix = false; bool lowercase = false; for (const char** arg = argv + 1; *arg != null; ++arg) { uint8_t tblval; if (*arg[0] == '%') { ++ *arg; goto number; } else if (!raw && (tblget(sz(argtbl),argtbl, *arg, &tblval) == ok)) { enum argument symbol = (enum argument) tblval; switch (symbol) { case arg_to: { if (curset == set_out) return bad_syntax; else curset = set_out; } break; ................................................................................ forposix(pfxstrlen = strlen(pfxstr)); ++arg; } break; /* specify an automatic output prefix */ case switch_prefix: { prefix = true; pfxstr = null; } break; case switch_lowercase: { lowercase = true; } break; default: { /* assume base shorthand */ base[curset] = bases[symbol]; } } } else /* bad_find */ number: { ................................................................................ lit("base") " " box("0-9") plus OR "asc")), }, ints[] = { p("default base: \x1b[94m.+\x1b[m"), p("binary literal: "lit("0b") box("01") plus), p("ternary literal: "lit("0t") box("012") plus), p("hex literal: "lit("0x") box("0-9A-Fa-f") plus), p("ascii literal: "lit("@") "\x1b[94m.+\x1b[m"), p("interpret any string (e.g. a keyword) as integer: " lit("%") box("0-9A-Za-z") plus), }, opts[] = { p("-p --prefix : print known prefix codes on output strings"), p("-m --manual-prefix: specify a manual prefix to print before each number"), p("-l --lowercase : prefer lowercase for case-insensitive bases"), }; # undef p # undef OR # undef plus # define hl_on "\x1b[;1m" # define hl_off "\x1b[m" |
> > > > > | > | > > > > > |
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 ... 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 ... 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 ... 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 ... 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
arg_asc, arg_bin, arg_trn, arg_oct, arg_dec, arg_duo, arg_hex, arg_b32, arg_b64, switch_prefix, param_prefix, switch_lowercase, switch_7bit, arg_ebcdic, }; word bases[] = { [arg_asc] = 0, [arg_bin] = 2, ................................................................................ {arg_duo, "duo"}, {arg_duo, "duodecimal"}, {arg_hex, "hex"}, {arg_hex, "hexadecimal"}, {arg_hex, "b32"}, {arg_hex, "base32"}, /* not padded! */ {switch_prefix, "-p"}, {switch_prefix, "--prefix"}, {switch_lowercase, "-l"}, {switch_lowercase, "--lowercase"}, {switch_7bit, "-7"}, {switch_7bit, "--7bit"}, {param_prefix, "-m"}, {param_prefix, "--manual-prefix"}, {arg_ebcdic, "ebcdic"}, }; /* import the conversion utilities */ typedef bad iaia_error_type; ................................................................................ typedef word iaia_word_type; enum /* iaia synonyms */ { iaia_e_ok = ok, iaia_e_domain = bad_domain, iaia_e_base = bad_base, iaia_e_overflow = bad_overflow, }; bool ascii_7bit = false; #define _IAIA_EXP_ASCFORM ascii_7bit #define _IAIA_EXTERNAL_TYPES #include "clib/iaia.c" bad run(const int argc, const char** argv) { # ifndef _POSIX_IO /* fuck your buffering, it only ever makes * things worse */ setvbuf(stdout,null,_IONBF); ................................................................................ bool raw = false; bool prefix = false; bool lowercase = false; for (const char** arg = argv + 1; *arg != null; ++arg) { uint8_t tblval; if (*arg[0] == '`') { ++ *arg; goto number; } else if (!raw && (tblget(sz(argtbl),argtbl, *arg, &tblval) == ok)) { enum argument symbol = (enum argument) tblval; switch (symbol) { case arg_to: { if (curset == set_out) return bad_syntax; else curset = set_out; } break; ................................................................................ forposix(pfxstrlen = strlen(pfxstr)); ++arg; } break; /* specify an automatic output prefix */ case switch_prefix: { prefix = true; pfxstr = null; } break; case switch_lowercase: { lowercase = true; } break; case switch_7bit: { ascii_7bit = true; } break; default: { /* assume base shorthand */ base[curset] = bases[symbol]; } } } else /* bad_find */ number: { ................................................................................ lit("base") " " box("0-9") plus OR "asc")), }, ints[] = { p("default base: \x1b[94m.+\x1b[m"), p("binary literal: "lit("0b") box("01") plus), p("ternary literal: "lit("0t") box("012") plus), p("hex literal: "lit("0x") box("0-9A-Fa-f") plus), p("ascii literal: "lit("@") "\x1b[94m.+\x1b[m"), p("interpret any string (e.g. a keyword) as integer: " lit("`") box("0-9A-Za-z") plus), }, opts[] = { p("-p --prefix : print known prefix codes on output strings"), p("-m --manual-prefix: specify a manual prefix to print before each number"), p("-l --lowercase : prefer lowercase for case-insensitive bases"), p("-7 --7bit : encode ascii in 7-bit space instead of keeping the"), p(" eighth empty bit. if this option is used, ascii"), p(" strings will be maximally compact, but will not match"), p(" the way they are stored in computer memory!"), /* p("-u --utf : allow non-ascii input"), */ }; # undef p # undef OR # undef plus # define hl_on "\x1b[;1m" # define hl_off "\x1b[m" |
Modified xpriv.c from [6369b6f0d6] to [48a3d17a7c].
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 ... 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 ... 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 ... 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 ... 299 300 301 302 303 304 305 306 307 308 309 310 311 312 ... 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 |
TODO add a randomizer call that works on BSD TODO document flags TODO implement/remove lock flag TODO add flag to bring window to current desktop */ #include <pwd.h> #include <pty.h> #include <fcntl.h> #include <unistd.h> #include <stdint.h> ................................................................................ #include <stdlib.h> #include <string.h> #include <signal.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/wait.h> #include <sys/types.h> #include <X11/Xlib.h> #include <errno.h> #include <sys/random.h> //TODO bsd compat #define shmem_prefix "/k.xpriv:" typedef enum { false, true } bool; enum mode { mode_usage, mode_go, mode_register, mode_kill, mode_lock }; enum res { ok, fail_parse, fail_arg, fail_opt, fail_shm, fail_mem, fail_pty, fail_nop, fail_win, fail_wid, fail_x11}; enum res bad(enum res code) { ................................................................................ void sigusr(int a) { if (global -> op == mode_kill) run = false; } void sigterm(int a) { run = false; } void spawn(pid_t ssha, const char* const sockn) { if (ssha = fork()) { char pid_s_buf[16]; char* pid_s = itoa(ssha, pid_s_buf, sizeof(pid_s_buf)); while (access(sockn, F_OK)); // avoid nasty race condition setenv("SSH_AGENT_PID", pid_s, true); setenv("SSH_AUTH_SOCK", sockn, true); global -> agent = ssha; } else { close(1); close(0); execlp("ssh-agent","ssh-agent","-D", "-a",sockn,0); } } enum res register_window(const char* const id, bool weak, const char* const name) { // the id field denotes the path to the shared memory // in use, and allows the user to have multiple // contexts by creating aliases to the binary int fd = shm_open(id, O_CREAT | O_EXCL | O_RDWR, 0600); ftruncate(fd, sizeof(struct signal)); if (fd == -1) return fail_shm; struct signal* s = mmap(0, sizeof(struct signal), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(s == MAP_FAILED) return fail_mem; else global = s; Display* xdpy = XOpenDisplay(NULL); Atom xvital; /* x11 */ { if (xdpy == NULL) return fail_win; Window win; ................................................................................ } if(s -> op == mode_kill) { kill(s -> pid, SIGTERM); } kill(s -> agent, SIGTERM); sigprocmask(SIG_BLOCK, &mask, NULL); shm_unlink(id); if (!weak) { XDeleteProperty(xdpy,s -> wid,xvital); XSync(xdpy,false); } XCloseDisplay(xdpy); } else { // now we start ssh-agent and set the proper environment // variables pid_t ssha; if (name == NULL) { /* messy part */ const char* tmp; //tmpdir defined? if (!(tmp = getenv("TMPDIR"))) tmp = "/tmp"; size_t tmpsz = strlen(tmp); char sockn[tmpsz + 1 + sizeof "ssh." + 11]; strcpy(sockn, tmp); sockn[tmpsz] = '/'; strcpy(sockn+tmpsz+1,"ssh."); ................................................................................ // assuming ascii… for(uint8_t*i=rndid;i<rndid+11;++i) { *i = '0' + (*i % (25 * 2 + 10)); if (*i > '9') *i += 7; if (*i > 'Z') *i += ('a' - 'Z'); } spawn(ssha, sockn); } else spawn(ssha, name); pid_t sad; int p; if (sad = fork()) { int added; waitpid(sad, &added, 0); if (added == 0) { if (weak == false) { XChangeProperty(xdpy,s -> wid,xvital,xvital,8,PropModeReplace,"\01", 1); XSync(xdpy,false); } write(1,"\033c",3); execlp("fish","fish",NULL); } else { return ok; } } else { execlp("ssh-add","ssh-add",NULL); } } return ok; // this is kind of pointless but w/e } enum res kill_window(const char* id) { int fd = shm_open(id, O_RDWR, 0600); if (fd == -1) return fail_nop; struct signal* s = mmap(0, sizeof(struct signal), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(s == MAP_FAILED) return fail_mem; s -> op = mode_kill; kill(s -> pid, SIGUSR1); return ok; } enum res activate_window(Window w) { ................................................................................ if(!XSendEvent(dpy, DefaultRootWindow(dpy), False, mask, &ev)) return fail_x11; XSync(dpy,false); return ok; } int main(int sz, char** argv) { enum mode op = mode_go; bool init_weak = false; const char* init_named = NULL; for(int i = 1; i<sz; ++i) { char* v = argv[i]; if (*v != '-') return bad(fail_arg); ................................................................................ seen_string_arg = true; ++ i; break; default: return bad(fail_opt); } if(opt[1] != 0) { ++opt; goto read_opt; } } size_t nsz; const char* basename = argv[0], *p; for (p = argv[0]; *p!=0; ++p) { if(*p == '/') basename = p + 1; } nsz = p - basename; char shid[nsz + sizeof shmem_prefix]; strncpy(shid,shmem_prefix,sizeof shmem_prefix); strncpy(shid + sizeof shmem_prefix - 1, basename, nsz); shid[nsz + sizeof shmem_prefix - 1] = 0; if (op == mode_go) { int fd; if ((fd = shm_open(shid, O_RDWR, 0600)) == -1) { const char* args[] = { "urxvtc", "-bg", "[90]#4b0024", "-e", argv[0], (init_weak?"-aw":"-a"), 0, 0, 0}; const uint8_t argsz = sizeof args/sizeof(const char*); if (init_named != NULL) { args[argsz - 3] = "-n"; // im sorry args[argsz - 2] = init_named; } execvp("urxvtc", (char* const*)args); } else { struct signal*s = mmap(0, sizeof(struct signal), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); return bad(activate_window(s->wid)); } } else if (op == mode_register) return bad(register_window(shid,init_weak,init_named)); else if (op == mode_kill) return bad(kill_window(shid)); else if (op == mode_usage) { write(1,"\e[1musage:\e[0m ",15); write(1, argv[0], strlen(argv[0])); write(1, " [-aklw [arg]]\n",16); return fail_parse; } } |
| > > > | > > > > > > > > > > > > > > | > > > > > > < > > > > > > < | | | > > | | | | | | | | > > > > > > > > > > | > > > > > > | > | > | | | | > > | | | | | > > > > > > > > > > > > > > > | | | | | | | | | | > > > > | | > | > > > | | > > > > > > > > > > > > > > > > > > > | | |
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 ... 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 ... 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 ... 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 ... 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 ... 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 |
TODO add a randomizer call that works on BSD TODO document flags TODO implement/remove lock flag TODO add flag to bring window to current desktop TODO rewrite using sysv shmem */ #include <pwd.h> #include <pty.h> #include <fcntl.h> #include <unistd.h> #include <stdint.h> ................................................................................ #include <stdlib.h> #include <string.h> #include <signal.h> #include <sys/mman.h> #include <sys/stat.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/shm.h> #include <X11/Xlib.h> #include <errno.h> #include <sys/random.h> #ifdef _RAND_SYSCALL # include <sys/syscall.h> # define getrandom(a,b,c) syscall(SYS_getrandom,a,b,c) /* this is necessary on certain platforms due to * certainly ungodly libc issues, i think. */ #endif #ifdef _SHM_LINUX /* xpriv was originally written with linux shared * memory in mind, because i'm an idiot. i've since * redesigned it to use the superior old sysv shm * api that works on every other unix, not just * linux, but if for some ungodly reason you want * to use the linux one, then just pass -D_SHM_LINUX */ # define shmem_prefix "/k.xpriv:" # define shmlinux(...) __VA_ARGS__ # define shmsysv(...) #else # define shmlinux(...) # define shmsysv(...) __VA_ARGS__ #endif enum /* constants */ { false = 0, true = 1, shmsysv(shmem_key = 0x53373EC3,) /* ye olde magique numbre */ }; typedef _Bool bool; enum mode { mode_usage, mode_go, mode_register, mode_kill, mode_lock }; enum res { ok, fail_parse, fail_arg, fail_opt, fail_shm, fail_mem, fail_pty, fail_nop, fail_win, fail_wid, fail_x11}; enum res bad(enum res code) { ................................................................................ void sigusr(int a) { if (global -> op == mode_kill) run = false; } void sigterm(int a) { run = false; } void spawn(pid_t ssha, const char* const sockn) { if (ssha = fork()) { char pid_s_buf[16]; char* pid_s = itoa(ssha, pid_s_buf, sizeof(pid_s_buf)); while (access(sockn, F_OK)); // avoid nasty race condition setenv("SSH_AGENT_PID", pid_s, true); setenv("SSH_AUTH_SOCK", sockn, true); global -> agent = ssha; } else { close(1); close(0); execlp("ssh-agent","ssh-agent","-D","-a",sockn,0); } } enum res register_window(shmlinux(const char* const id,) bool weak, const char* name) { // the id field denotes the path to the shared memory // in use, and allows the user to have multiple // contexts by creating aliases to the binary struct signal* s; shmlinux ({ int fd = shm_open(id, O_CREAT | O_EXCL | O_RDWR, 0600); ftruncate(fd, sizeof(struct signal)); if (fd == -1) return fail_shm; s = mmap(0, sizeof(struct signal), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(s == MAP_FAILED) return fail_mem; else global = s; }) shmsysv (int shmem; shmem = shmget(shmem_key, sizeof (struct signal), IPC_CREAT | 0777); if (shmem == -1) return fail_shm; s = shmat(shmem, 0,0); if (s == (void*)-1) return fail_mem; else global = s; ) Display* xdpy = XOpenDisplay(NULL); Atom xvital; /* x11 */ { if (xdpy == NULL) return fail_win; Window win; ................................................................................ } if(s -> op == mode_kill) { kill(s -> pid, SIGTERM); } kill(s -> agent, SIGTERM); sigprocmask(SIG_BLOCK, &mask, NULL); shmlinux(shm_unlink(id)); shmsysv({ struct shmid_ds bleh; /* ?? */ shmctl(shmem, IPC_RMID, &bleh); }) if (!weak) { XDeleteProperty(xdpy,s -> wid,xvital); XSync(xdpy,false); } XCloseDisplay(xdpy); } else { // now we start ssh-agent and set the proper environment // variables pid_t ssha; if (name == NULL) { /* messy part */ const char* tmp; //tmpdir defined? if (!(tmp = getenv("XDG_RUNTIME_DIR"))) if (!(tmp = getenv("TMPDIR"))) tmp = "/tmp"; size_t tmpsz = strlen(tmp); char sockn[tmpsz + 1 + sizeof "ssh." + 11]; strcpy(sockn, tmp); sockn[tmpsz] = '/'; strcpy(sockn+tmpsz+1,"ssh."); ................................................................................ // assuming ascii… for(uint8_t*i=rndid;i<rndid+11;++i) { *i = '0' + (*i % (25 * 2 + 10)); if (*i > '9') *i += 7; if (*i > 'Z') *i += ('a' - 'Z'); } name = sockn; } spawn(ssha, name); pid_t sad; int p; if (sad = fork()) { int added; waitpid(sad, &added, 0); if (added == 0) { if (weak == false) { XChangeProperty(xdpy,s -> wid,xvital,xvital,8,PropModeReplace,"\01", 1); XSync(xdpy,false); } write(1,"\033c",3); execlp("fish","fish",NULL); } } else { execlp("ssh-add","ssh-add",NULL); } } return ok; } enum res kill_window(shmlinux(const char* id)) { struct signal* s; shmlinux({ int fd = shm_open(id, O_RDWR, 0600); if (fd == -1) return fail_nop; s = mmap(0, sizeof(struct signal), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(s == MAP_FAILED) return fail_mem; }) shmsysv (int shmem; { shmem = shmget(shmem_key, sizeof(struct signal), 0); if (shmem == -1) return fail_nop; s = shmat(shmem,0,0); if (s == (void*)-1) return fail_mem; }) shmsysv({ struct shmid_ds bleh; /* ?? */ shmctl(shmem, IPC_RMID, &bleh); }) s -> op = mode_kill; kill(s -> pid, SIGUSR1); return ok; } enum res activate_window(Window w) { ................................................................................ if(!XSendEvent(dpy, DefaultRootWindow(dpy), False, mask, &ev)) return fail_x11; XSync(dpy,false); return ok; } int main(int sz, char** argv) { char* binname = "xpriv"; if(sz > 0) binname = argv[0]; enum mode op = mode_go; bool init_weak = false; const char* init_named = NULL; for(int i = 1; i<sz; ++i) { char* v = argv[i]; if (*v != '-') return bad(fail_arg); ................................................................................ seen_string_arg = true; ++ i; break; default: return bad(fail_opt); } if(opt[1] != 0) { ++opt; goto read_opt; } } shmlinux( size_t nsz; const char* basename = argv[0], *p; for (p = binname; *p!=0; ++p) { if(*p == '/') basename = p + 1; } nsz = p - basename; char shid[nsz + sizeof shmem_prefix]; strncpy(shid,shmem_prefix,sizeof shmem_prefix); strncpy(shid + sizeof shmem_prefix - 1, basename, nsz); shid[nsz + sizeof shmem_prefix - 1] = 0; ) shmsysv ({ /* TODO: implement ability to have multiple * keys based on the name of the binary */ }) tryagain: if (op == mode_go) { int shm; if ((shm = shmlinux( shm_open(shid, O_RDWR, 0600) ) shmsysv ( shmget(shmem_key,sizeof (struct signal),0) )) == -1) { const char* args[] = { "urxvtc", "-bg", "[90]#4b0024", "-e", argv[0], (init_weak?"-aw":"-a"), 0, 0, 0}; const uint8_t argsz = sizeof args/sizeof(const char*); if (init_named != NULL) { args[argsz - 3] = "-n"; // im sorry args[argsz - 2] = init_named; } execvp("urxvtc", (char* const*)args); } else { struct signal*s = shmlinux( mmap(0, sizeof(struct signal), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0) ) shmsysv ( shmat(shm, 0, 0) ); struct shmid_ds stat; shmctl(shm, IPC_STAT, &stat); if (stat.shm_nattch == 0) { /* if the shm segment is not attached to any * process, it's a relic that needs to be * cleaned up before we do anything else. on * a sane OS, there would be built-in non- * persistence mechanisms for shared mem, but * alas, as you already know, POSIX */ shmctl(shm, IPC_RMID, &stat); goto tryagain; } if (s == shmlinux(MAP_FAILED) shmsysv((void*)-1)) return bad(fail_mem); return bad(activate_window(s->wid)); } } else if (op == mode_register) return bad(register_window(shmlinux(shid,) init_weak,init_named)); else if (op == mode_kill) return bad(kill_window(shmlinux(shid))); else if (op == mode_usage) { write(1,"\e[1musage:\e[0m ",15); write(1, argv[0], strlen(argv[0])); write(1, " [-aklw [arg]]\n",16); return fail_parse; } } |