void create_uuid(char *uuid)
{
// Read 128 random bits
- int fd = xopen("/dev/urandom", O_RDONLY);
+ int fd = xopenro("/dev/urandom");
xreadall(fd, uuid, 16);
close(fd);
void xunlink(char *path);
int xcreate(char *path, int flags, int mode);
int xopen(char *path, int flags);
+int xcreate_stdio(char *path, int flags, int mode);
+int xopen_stdio(char *path, int flags);
+int xopenro(char *path);
void xpipe(int *pp);
void xclose(int fd);
int xdup(int fd);
if (al[i].id) s += sprintf(s, "$%c$", '0'+al[i].id);
// Read appropriate number of random bytes for salt
- i = xopen("/dev/urandom", O_RDONLY);
+ i = xopenro("/dev/urandom");
xreadall(i, libbuf, ((len*6)+7)/8);
close(i);
}
// Die unless we can open/create a file, returning file descriptor.
-int xcreate(char *path, int flags, int mode)
+int xcreate_stdio(char *path, int flags, int mode)
{
int fd = open(path, flags^O_CLOEXEC, mode);
+
if (fd == -1) perror_exit_raw(path);
return fd;
}
// Die unless we can open a file, returning file descriptor.
-int xopen(char *path, int flags)
+int xopen_stdio(char *path, int flags)
{
- return xcreate(path, flags, 0);
+ return xcreate_stdio(path, flags, 0);
}
void xpipe(int *pp)
return fd;
}
+// Move file descriptor above stdin/stdout/stderr, using /dev/null to consume
+// old one. (We should never be called with stdin/stdout/stderr closed, but...)
+int notstdio(int fd)
+{
+ while (fd<3) {
+ int fd2 = xdup(fd);
+
+ close(fd);
+ xopen_stdio("/dev/null", O_RDWR);
+ fd = fd2;
+ }
+
+ return fd;
+}
+
+// Create a file but don't return stdin/stdout/stderr
+int xcreate(char *path, int flags, int mode)
+{
+ return notstdio(xcreate_stdio(path, flags, mode));
+}
+
+// Open a file descriptor NOT in stdin/stdout/stderr
+int xopen(char *path, int flags)
+{
+ return notstdio(xopen_stdio(path, flags));
+}
+
+// Open read only, treating "-" as a synonym for stdin.
+int xopenro(char *path)
+{
+ if (!strcmp(path, "-")) return 0;
+
+ return xopen(path, O_RDONLY);
+}
+
FILE *xfdopen(int fd, char *mode)
{
FILE *f = fdopen(fd, mode);
void load_policy_main(void)
{
- char *path = *toys.optargs;
- int fd = xopen(path, O_RDONLY);
+ int fd = xopenro(*toys.optargs);
off_t policy_len = fdlength(fd);
char *policy_data = mmap(0, policy_len, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (!policy_data || security_load_policy(policy_data, policy_len) < 0)
- perror_exit("Couldn't %s %s", policy_data ? "load" : "read", path);
+ perror_exit("Couldn't %s %s", policy_data ? "load" : "read", *toys.optargs);
munmap(policy_data, policy_len);
}
continue;
sprintf(toybuf, "/sys/class/rfkill/rfkill%u/uevent", rfevent.idx);
- tvar = xopen(toybuf, O_RDONLY);
+ tvar = xopenro(toybuf);
while ((line = get_line(tvar))) {
char *s = line;
if (!toys.optflags) help_exit("need --option");
for (ss = toys.optargs; *ss; ss++) {
- int fd = xopen(*ss, O_RDONLY), i;
+ int fd = xopenro(*ss), i;
// Command line order discarded so perform multiple operations in flag order
for (i = 0; i < 32; i++) {
void fsfreeze_main(void)
{
- int fd = xopen(*toys.optargs, O_RDONLY);
+ int fd = xopenro(*toys.optargs);
long p = 1;
xioctl(fd, (toys.optflags & FLAG_f) ? FIFREEZE : FITHAW, &p);
void insmod_main(void)
{
- int fd = !strcmp(*toys.optargs, "-") ? 0 : xopen(*toys.optargs, O_RDONLY);
+ int fd = xopenro(*toys.optargs);
int i, rc;
i = 1;
// Open file and chdir, verbosely
xprintf("rootdir = %s\n", *toys.optargs);
if (toys.optflags & FLAG_d && strcmp(TT.fname, "-")) {
- fd = xopen(TT.fname, O_RDONLY);
+ fd = xopenro(TT.fname);
xprintf("table = %s\n", TT.fname);
} else xprintf("table = <stdin>\n");
xchdir(*toys.optargs);
filename = toybuf;
}
- if (setns(fd = xopen(filename, O_RDONLY), flags[i]))
- perror_exit("setns");
+ if (setns(fd = xopenro(filename), flags[i])) perror_exit("setns");
close(fd);
}
nsnames += strlen(nsnames)+1;
for (i = 0; i<ARRAY_LEN(pipes); i++) xsignal(pipes[i], oneit_signaled);
if (toys.optflags & FLAG_3) {
- // Ensure next available filehandle is #3
- while (open("/", 0) < 3);
+ // Ensure next available filehandles are #3 and #4
+ while (xopen_stdio("/", 0) < 3);
close(3);
close(4);
- if (pipe(pipes)) perror_exit("pipe");
+ xpipe(pipes);
fcntl(4, F_SETFD, FD_CLOEXEC);
}
char **try;
if (!(toys.optflags & FLAG_n)) TT.iterations++;
- TT.ufd = xopen("/dev/urandom", O_RDONLY);
+ TT.ufd = xopenro("/dev/urandom");
// We don't use loopfiles() here because "-" isn't stdin, and want to
// respond to files we can't open via chmod.
static int write_key(char *path, char *key, char *value)
{
- int fd = open(path, O_WRONLY);;
+ int fd = open(path, O_WRONLY);
if (fd < 0) {
key_error(key);
if ((toys.optflags & FLAG_d) && !delete_entry()) return;
//show arp chache
- fd = xopen("/proc/net/arp", O_RDONLY);
+ fd = xopenro("/proc/net/arp");
buf = get_line(fd);
free(buf); //skip first line
if (!TT.flagd && TT.logfile) {
int fd = open(TT.logfile, O_WRONLY | O_CREAT | O_APPEND, 0666);
- if (fd >=0 && fd != 2) {
+ if (fd==-1) perror_msg("'%s", TT.logfile);
+ else {
dup2(fd, 2);
close(fd);
- } else if (fd < 0) perror_msg("'%s", TT.logfile);
+ }
}
used = vsnprintf(NULL, 0, msg, d);
smsg = xzalloc(++used);
static int parse_crontab(char *fname)
{
char *line;
- int lno, fd = xopen(fname, O_RDONLY);
+ int lno, fd = xopenro(fname);
long plen = 0;
for (lno = 1; (line = get_rawline(fd, &plen, '\n')); lno++,free(line)) {
int fdin;
snprintf(toybuf, sizeof(toybuf), "%s%s", TT.cdir, name);
- if ((fdin = open(toybuf, O_RDONLY)) == -1)
- error_exit("No crontab for '%s'", name);
+ fdin = xopenro(toybuf);
xsendfile(fdin, 1);
xclose(fdin);
}
snprintf(toybuf, sizeof(toybuf), "%s%s", TT.cdir, dest);
fdout = xcreate(toybuf, O_WRONLY|O_CREAT|O_TRUNC, 0600);
- fdin = xopen(src, O_RDONLY);
+ fdin = xopenro(src);
xsendfile(fdin, fdout);
xclose(fdin);
if (!stat(toybuf, &sb)) { // file exists and have some content.
if (sb.st_size) {
- srcfd = xopen(toybuf, O_RDONLY);
+ srcfd = xopenro(toybuf);
xsendfile(srcfd, destfd);
xclose(srcfd);
}
}
}
-static int xmove_fd(int fd)
-{
- int newfd;
-
- if (fd > STDERR_FILENO) return fd;
- if ((newfd = fcntl(fd, F_DUPFD, 3) < 0)) perror_exit("dupfd IO");
- close(fd);
- return newfd;
-}
-
static void setup_inout()
{
/* for C_BS, in/out is done as it is. so only in.sz is enough.
if (!TT.in.name) {
TT.in.name = "stdin";
TT.in.fd = STDIN_FILENO;
- } else {
- TT.in.fd = xopen(TT.in.name, O_RDONLY);
- TT.in.fd = xmove_fd(TT.in.fd);
- }
+ } else TT.in.fd = xopenro(TT.in.name);
+
//setup outout
if (!TT.out.name) {
TT.out.name = "stdout";
int flags = O_WRONLY|O_CREAT;
if (!(toys.optflags&C_NOTRUNC)) flags |= O_TRUNC;
TT.out.fd = xcreate(TT.out.name, flags, 0666);
- TT.out.fd = xmove_fd(TT.out.fd);
}
if (TT.in.offset) {
static uint32_t getxid(void)
{
uint32_t randnum;
- int fd = xopen("/dev/urandom", O_RDONLY);
+ int fd = xopenro("/dev/urandom");
+
+// TODO xreadfile
xreadall(fd, &randnum, sizeof(randnum));
xclose(fd);
return randnum;
int i, fd;
if(!(toys.optflags & FLAG_f)) TT.file = "/var/lib/misc/dhcpd.leases"; //DEF_LEASE_FILE
- fd = xopen(TT.file, O_RDONLY);
+ fd = xopenro(TT.file);
xprintf("Mac Address IP Address Host Name Expires %s\n", (toys.optflags & FLAG_a) ? "at" : "in");
xread(fd, &written_time, sizeof(written_time));
current_time = time(NULL);
if ((setsid() < 0) && (getpid() != getsid(0)))
perror_exit("setsid");
xclose(0);
- xopen(TT.tty_name, O_RDWR|O_NDELAY|O_CLOEXEC);
+ xopen_stdio(TT.tty_name, O_RDWR|O_NDELAY|O_CLOEXEC);
fcntl(0, F_SETFL, fcntl(0, F_GETFL) & ~O_NONBLOCK); // Block read
dup2(0, 1);
dup2(0, 2);
syslog(LOG_NOTICE, "KLOGD: started with Kernel ring buffer as log source\n");
klogctl(1, NULL, 0);
} else {
- TT.fd = xopen("/proc/kmsg", O_RDONLY); //_PATH_KLOG in paths.h
+ TT.fd = xopenro("/proc/kmsg"); //_PATH_KLOG in paths.h
syslog(LOG_NOTICE, "KLOGD: started with /proc/kmsg as log source\n");
}
openlog("Kernel", 0, LOG_KERN); //open connection to system logger..
pwidth = (toys.optflags & FLAG_W) ? 46 : 16;
*tm = time(tm+1);
- fd = xopen(file, O_RDONLY);
+ fd = xopenro(file);
loc = xlseek(fd, 0, SEEK_END);
// Loop through file structures in reverse order.
gid_t gid = 0;
if (path) {
- // Try to read major/minor string
+ // Try to read major/minor string, returning if we can't
temp = strrchr(path, '/');
fd = open(path, O_RDONLY);
*temp = 0;
{
char *buf = NULL;
int len, res;
- int fd = xopen(modules, O_RDONLY);
+ int fd = xopenro(modules);
+
+ // TODO xreadfile()
len = fdlength(fd);
buf = xmalloc(len);
xioctl(fd, VT_GETSTATE, &vstate);
close(0); //new vt becomes stdin
- vt_fd = xopen(toybuf, O_RDWR);
+ vt_fd = xopen_stdio(toybuf, O_RDWR);
if (toys.optflags & FLAG_s) {
ioctl(vt_fd, VT_ACTIVATE, TT.vt_num);
ioctl(vt_fd, VT_WAITACTIVE, TT.vt_num);
if (toys.optargs[0]) {
int fd;
- dup2((fd = xopen(toys.optargs[0], O_RDWR)), 0);
+ dup2((fd = xopen_stdin(toys.optargs[0], O_RDWR)), 0);
if (!isatty(0)) error_exit("%s: it is not a tty", toys.optargs[0]);
dup2( fd, 1);
dup2( fd, 2);
sd = init_tftp(&server);
packet = (uint8_t*)xzalloc(TFTP_IOBUFSIZE);
- fd = xopen(TT.local_file, O_RDONLY);
+ fd = xopenro(TT.local_file);
for (;;) { //first loop for request send and confirmation from server.
packetlen = mkpkt_request(packet, TFTP_OP_WRQ, TT.remote_file, 1);
if (toys.optflags == 7) return;
for (i = 0; i < 2; i++) {
- file[i] = strcmp("-", toys.optargs[i])
- ? xopen(toys.optargs[i], O_RDONLY) : 0;
+ file[i] = xopenro(toys.optargs[i]);
line[i] = get_line(file[i]);
}
if (!ctoken) break;
if (!*ctoken) continue;
- //Get start position.
+ // Get start position.
if (*(dtoken = strsep(&ctoken, "-"))) {
start = atolx_range(dtoken, 0, INT_MAX);
start = (start?(start-1):start);
}
- //Get end position.
+ // Get end position.
if (!ctoken) end = -1; //case e.g. 1,2,3
else if (*ctoken) {//case e.g. N-M
end = atolx_range(ctoken, 0, INT_MAX);
add_to_list(start, end);
TT.nelem++;
}
- //if list is missing in command line.
+ // if list is missing in command line.
if (!TT.nelem) error_exit("missing positions list");
}
if(strcmp(*argv, "-") == 0) TT.do_cut(0); //for stdin
else {
int fd = open(*argv, O_RDONLY, 0);
- if(fd < 0) {//if file not present then continue with other files.
+ if (fd < 0) {//if file not present then continue with other files.
perror_msg_raw(*argv);
continue;
}
ss += len;
aa->arglen = len;
aa->dir = !!strchr(s, 'd');
- if (TT.topdir == -1) TT.topdir = xopen(".", 0);
+ if (TT.topdir == -1) TT.topdir = xopenro(".");
// collect names and execute commands
} else {
toys.exitval = 1;
if (toys.optflags & FLAG_s) {
close(2);
- xopen("/dev/null", O_RDWR);
+ xopen_stdio("/dev/null", O_RDWR);
}
if (toys.optflags & FLAG_r) {
}
if (isatty(0)) {
close(0);
- open("/dev/null", O_RDONLY);
+ xopen_stdio("/dev/null", O_RDONLY);
}
xexec(toys.optargs);
}
strip = 0;
char *oldname = NULL, *newname = NULL;
- if (TT.infile) TT.filepatch = xopen(TT.infile, O_RDONLY);
+ if (TT.infile) TT.filepatch = xopenro(TT.infile);
TT.filein = TT.fileout = -1;
if (TT.dir) xchdir(TT.dir);
TT.filein = xcreate(name, O_CREAT|O_EXCL|O_RDWR, 0666);
} else {
printf("patching %s\n", name);
- TT.filein = xopen(name, O_RDONLY);
+ TT.filein = xopenro(name);
}
if (toys.optflags & FLAG_dry_run)
TT.fileout = xopen("/dev/null", O_RDWR);
// so handle all -e, then all -f. (At least the behavior's consistent.)
for (al = TT.e; al; al = al->next) parse_pattern(&al->arg, strlen(al->arg));
- for (al = TT.f; al; al = al->next)
- do_lines(strcmp(al->arg, "-") ? xopen(al->arg, O_RDONLY) : 0,parse_pattern);
+ for (al = TT.f; al; al = al->next) do_lines(xopenro(al->arg), parse_pattern);
parse_pattern(0, 0);
dlist_terminate(TT.pattern);
if (TT.nextlen) error_exit("no }");
char *line = 0, mode[16],
*class[] = {"begin%*[ ]%15s%*[ ]%n", "begin-base64%*[ ]%15s%*[ ]%n"};
- if (toys.optc) ifd = xopen(*toys.optargs, O_RDONLY);
+ if (toys.optc) ifd = xopenro(*toys.optargs);
while (!idx) {
free(line);
int i, m = toys.optflags & FLAG_m, fd = 0;
- if (toys.optc > 1) fd = xopen(toys.optargs[0], O_RDONLY);
+ if (toys.optc > 1) fd = xopenro(toys.optargs[0]);
base64_init(toybuf);