int main(int argc, char **argv) { char **av = argv; struct sockaddr_in host; afs_int32 code; struct hostent *hp; char hnamebuf[200]; struct timeval tv; int noAuth = 1; /* Default is authenticated connections */ argc--, av++; if (argc < 1) { printf("usage: pxclient <serverHost>\n"); exit(1); } memset(&host, 0, sizeof(struct sockaddr_in)); host.sin_family = AF_INET; host.sin_addr.s_addr = inet_addr(av[0]); #ifdef STRUCT_SOCKADDR_HAS_SA_LEN host.sin_len = sizeof(struct sockaddr_in); #endif if (host.sin_addr.s_addr != -1) { strcpy(hnamebuf, av[0]); } else { hp = gethostbyname(av[0]); if (hp) { host.sin_family = hp->h_addrtype; memcpy((caddr_t) & host.sin_addr, hp->h_addr, hp->h_length); } else { printf("unknown server host %s\n", av[0]); exit(1); } } if ((code = pxclient_Initialize(noAuth, host.sin_addr.s_addr)) != 0) { printf("Couldn't initialize fs library (code=%d).\n", code); exit(1); } code = ubik_Call(RXAFS_GetTime, cstruct, 0, &tv.tv_sec, &tv.tv_usec); if (!code) printf("AFS_GetTime on %s sec=%ld, usec=%ld\n", av[0], tv.tv_sec, (long int)tv.tv_usec); else printf("return code is %d\n", code); #ifdef notdef while (1) { char line[500]; int nargs; printf("fs> "); if (fgets(line, 499, stdin) != NULL) { char *oper; char **argp = args; GetArgs(line, argp, &nargs); oper = &argp[0][0]; ++argp, --nargs; if (!strcmp(oper, "probe")) { code = ubik_Call(RXAFS_GetTime, cstruct, 0, &tv.tv_sec, &tv.tv_usec); printf("return code is %d\n", code); if (!code) printf("sec=%d\n", tv.tv_sec); } else if (!strcmp(oper, "fsstats")) { struct afsStatistics stats; code = ubik_AFS_GetStatistics(cstruct, 0, &stats); printf("return code is %d\n", code); } else if (!strcmp(oper, "fd")) { code = FetchData(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "fs")) { code = FetchStatus(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "fa")) { code = FetchACL(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "sd")) { code = StoreData(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "ss")) { code = StoreStatus(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "sa")) { code = StoreACL(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "cf")) { code = CreateFile(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rf")) { code = RemoveFile(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rn")) { code = Rename(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "sl")) { code = Symlink(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "hl")) { code = HardLink(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "md")) { code = MakeDir(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rd")) { code = RemoveDir(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rdd")) { code = Readdir(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "mm")) { code = MakeMountPoint(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "rt")) { code = ReleaseTokens(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "bs")) { code = BulkStatus(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "lk")) { code = Lookup(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "gt")) { code = GetToken(argp); printf("return code is %d\n", code); } else if (!strcmp(oper, "ka")) { code = KeepAlive(argp); printf("return code is %d\n", code); } else if ((!strcmp(oper, "q")) || !strcmp(oper, "quit")) exit(0); else { printf("Unknown oper! Available operations: \n\n"); printf("fd <vnode> <unique> <pos> <len>\n"); printf("fs <vnode> <unique>\n"); printf("fa <vnode> <unique>\n"); printf ("sd <vnode> <unique> <pos> <len> <flen> [<mode>|-1] [<owner>|-1] [<length>|-1] <string>\n"); printf ("ss <vnode> <unique> [<mode>|-1] [<owner>|-1] [<length>|-1]\n"); printf("sa <vnode> <unique> <string>\n"); printf("rf <vnode> <unique> <name>\n"); printf ("cf <vnode> <unique> <name> [<mode>|-1] [<owner>|-1] [<length>|-1]\n"); printf ("rn <ovnode> <ounique> <oname> <nvnode> <nunique> <nname>\n"); printf ("sl <vnode> <unique> <name> <contents> [<mode>|-1] [<owner>|-1] [<length>|-1]\n"); printf("hl <dvnode> <dunique> <name> <evnode> <eunique>\n"); printf ("md <vnode> <unique> <name> [<mode>|-1] [<owner>|-1] [<length>|-1]\n"); printf("rd <vnode> <unique> <name>\n"); printf("rdd <vnode> <unique> <pos> <len>\n"); printf("lk <vnode> <unique> <name>\n"); printf("gt <vnode> <unique> <tokenID>\n"); printf("ka <vol.l> <vnode> <unique> <isExec> <kaTime>\n"); } } } #endif return 0; }
static int xioopen_pty(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *xfd, unsigned groups, int dummy1, int dummy2, int dummy3) { /* we expect the form: filename */ int ptyfd = -1, ttyfd = -1; #if defined(HAVE_DEV_PTMX) || defined(HAVE_DEV_PTC) bool useptmx = false; /* use /dev/ptmx or equivalent */ #endif #if HAVE_OPENPTY bool useopenpty = false; /* try only openpty */ #endif /* HAVE_OPENPTY */ char ptyname[MAXPTYNAMELEN]; char *tn = NULL; char *linkname = NULL; bool opt_unlink_close = true; /* remove symlink afterwards */ bool wait_slave = false; /* true would be better for many platforms, but some OSes cannot handle this, and for common default behaviour as well as backward compatibility we choose "no" as default */ struct timespec pollintv = { PTY_INTERVALL }; if (argc != 1) { Error2("%s: wrong number of parameters (%d instead of 0)", argv[0], argc-1); } xfd->stream.howtoend = END_CLOSE; if (applyopts_single(&xfd->stream, opts, PH_INIT) < 0) return -1; applyopts(-1, opts, PH_INIT); retropt_bool(opts, OPT_UNLINK_CLOSE, &opt_unlink_close); /* trying to set user-early, perm-early etc. here might be useless because file system entry is eventually available only past pty creation */ /* name not yet known; umask should not be handled with this function! */ /* umask does not affect resulting mode, on Linux 2.4 */ applyopts_named("", opts, PH_EARLY); /* umask! */ #if defined(HAVE_DEV_PTMX) || defined(HAVE_DEV_PTC) retropt_bool(opts, OPT_PTMX, &useptmx); #endif #if HAVE_OPENPTY retropt_bool(opts, OPT_OPENPTY, &useopenpty); #endif #if (defined(HAVE_DEV_PTMX) || defined(HAVE_DEV_PTC)) # if HAVE_OPENPTY useopenpty = !useptmx; # else /* !HAVE_OPENPTY */ useptmx = true; # endif /* !HAVE_OPENPTY */ #else # if HAVE_OPENPTY useopenpty = true; # endif /* HAVE_OPENPTY */ #endif /* ! (defined(HAVE_DEV_PTMX) || defined(HAVE_DEV_PTC)) */ #if HAVE_POLL retropt_bool(opts, OPT_PTY_WAIT_SLAVE, &wait_slave); retropt_timespec(opts, OPT_PTY_INTERVALL, &pollintv); #endif /* HAVE_POLL */ if (applyopts_single(&xfd->stream, opts, PH_INIT) < 0) return -1; applyopts2(-1, opts, PH_INIT, PH_EARLY); applyopts(-1, opts, PH_PREBIGEN); #if defined(HAVE_DEV_PTMX) # define PTMX "/dev/ptmx" /* Linux */ #elif HAVE_DEV_PTC # define PTMX "/dev/ptc" /* AIX 4.3.3 */ #endif #if HAVE_DEV_PTMX || HAVE_DEV_PTC if (useptmx) { if ((ptyfd = Open(PTMX, O_RDWR|O_NOCTTY, 0620)) < 0) { Warn1("open(\""PTMX"\", O_RDWR|O_NOCTTY, 0620): %s", strerror(errno)); /*!*/ } else { ;/*0 Info1("open(\""PTMX"\", O_RDWR|O_NOCTTY, 0620) -> %d", ptyfd);*/ } if (ptyfd >= 0 && ttyfd < 0) { /* we used PTMX before forking */ /*0 extern char *ptsname(int);*/ #if HAVE_GRANTPT /* AIX, not Linux */ if (Grantpt(ptyfd)/*!*/ < 0) { Warn2("grantpt(%d): %s", ptyfd, strerror(errno)); } #endif /* HAVE_GRANTPT */ #if HAVE_UNLOCKPT if (Unlockpt(ptyfd)/*!*/ < 0) { Warn2("unlockpt(%d): %s", ptyfd, strerror(errno)); } #endif /* HAVE_UNLOCKPT */ #if HAVE_PROTOTYPE_LIB_ptsname /* AIX, not Linux */ if ((tn = Ptsname(ptyfd)) == NULL) { Warn2("ptsname(%d): %s", ptyfd, strerror(errno)); } else { Notice1("PTY is %s", tn); } #endif /* HAVE_PROTOTYPE_LIB_ptsname */ if (tn == NULL) { if ((tn = Ttyname(ptyfd)) == NULL) { Warn2("ttyname(%d): %s", ptyfd, strerror(errno)); } } ptyname[0] = '\0'; strncat(ptyname, tn, MAXPTYNAMELEN-1); } } #endif /* HAVE_DEV_PTMX || HAVE_DEV_PTC */ #if HAVE_OPENPTY if (ptyfd < 0) { int result; if ((result = Openpty(&ptyfd, &ttyfd, ptyname, NULL, NULL)) < 0) { Error4("openpty(%p, %p, %p, NULL, NULL): %s", &ptyfd, &ttyfd, ptyname, strerror(errno)); return -1; } Notice1("PTY is %s", ptyname); } #endif /* HAVE_OPENPTY */ if (!retropt_string(opts, OPT_SYMBOLIC_LINK, &linkname)) { if (Unlink(linkname) < 0 && errno != ENOENT) { Error2("unlink(\"%s\"): %s", linkname, strerror(errno)); } if (Symlink(ptyname, linkname) < 0) { Error3("symlink(\"%s\", \"%s\"): %s", ptyname, linkname, strerror(errno)); } if (opt_unlink_close) { if ((xfd->stream.unlink_close = strdup(linkname)) == NULL) { Error1("strdup(\"%s\"): out of memory", linkname); } xfd->stream.opt_unlink_close = true; } } applyopts_named(ptyname, opts, PH_PASTOPEN); applyopts_named(ptyname, opts, PH_FD); applyopts_cloexec(ptyfd, opts);/*!*/ xfd->stream.dtype = XIODATA_PTY; applyopts(ptyfd, opts, PH_FD); { /* special handling of user-late etc.; with standard behaviour (up to 1.7.1.1) they affected /dev/ptmx instead of /dev/pts/N */ uid_t uid = -1, gid = -1; mode_t perm; bool dont; dont = retropt_uid(opts, OPT_USER_LATE, &uid); dont &= retropt_gid(opts, OPT_GROUP_LATE, &gid); if (!dont) { if (Chown(ptyname, uid, gid) < 0) { Error4("chown(\"%s\", %d, %d): %s", ptyname, uid, gid, strerror(errno)); } } if (retropt_mode(opts, OPT_PERM_LATE, &perm) == 0) { if (Chmod(ptyname, perm) < 0) { Error3("chmod(\"%s\", %03o): %s", ptyname, perm, strerror(errno)); } } } xfd->stream.fd = ptyfd; applyopts(ptyfd, opts, PH_LATE); if (applyopts_single(&xfd->stream, opts, PH_LATE) < 0) return -1; #if HAVE_POLL /* if you can and wish: */ if (wait_slave) { /* try to wait until someone opens the slave side of the pty */ /* we want to get a HUP (hangup) condition on the pty */ #if HAVE_DEV_PTMX || HAVE_DEV_PTC if (useptmx) { ttyfd = Open(tn, O_RDWR|O_NOCTTY, 0620); Close(ttyfd); } #endif #if HAVE_OPENPTY if (useopenpty) { Close(ttyfd); } #endif /* HAVE_OPENPTY */ /* now we poll until the HUP vanishes - this indicates a slave conn. */ while (true) { struct pollfd ufd; ufd.fd = ptyfd; ufd.events = (POLLHUP); if (Poll(&ufd, 1, 0) < 0) { Error3("poll({%d, 0x%04hu,}, 1, 0): %s", ufd.fd, ufd.events, strerror(errno)); /*! close something */ return -1; } if (!(ufd.revents & POLLHUP)) { break; } Nanosleep(&pollintv, NULL); continue; } } #endif /* HAVE_POLL */ return STAT_OK; }