Compare commits

..

No commits in common. "3cfb7cc50771ca6ee579217c1194534313f03d8d" and "a3af963958a3d9df2729799d228751cd0a03d3cc" have entirely different histories.

5 changed files with 25 additions and 67 deletions

View File

@ -119,7 +119,7 @@ int write_string_stream_ts(
struct timespec *ts) { struct timespec *ts) {
bool needs_nl; bool needs_nl;
int r, fd; int r;
assert(f); assert(f);
assert(line); assert(line);
@ -127,14 +127,6 @@ int write_string_stream_ts(
if (ferror(f)) if (ferror(f))
return -EIO; return -EIO;
if (ts) {
/* If we shall set the timestamp we need the fd. But fmemopen() streams generally don't have
* an fd. Let's fail early in that case. */
fd = fileno(f);
if (fd < 0)
return -EBADF;
}
needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n"); needs_nl = !(flags & WRITE_STRING_FILE_AVOID_NEWLINE) && !endswith(line, "\n");
if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) { if (needs_nl && (flags & WRITE_STRING_FILE_DISABLE_BUFFER)) {
@ -162,7 +154,7 @@ int write_string_stream_ts(
if (ts) { if (ts) {
struct timespec twice[2] = {*ts, *ts}; struct timespec twice[2] = {*ts, *ts};
if (futimens(fd, twice) < 0) if (futimens(fileno(f), twice) < 0)
return -errno; return -errno;
} }
@ -894,7 +886,7 @@ int fflush_and_check(FILE *f) {
} }
int fflush_sync_and_check(FILE *f) { int fflush_sync_and_check(FILE *f) {
int r, fd; int r;
assert(f); assert(f);
@ -902,16 +894,10 @@ int fflush_sync_and_check(FILE *f) {
if (r < 0) if (r < 0)
return r; return r;
/* Not all file streams have an fd associated (think: fmemopen()), let's handle this gracefully and if (fsync(fileno(f)) < 0)
* assume that in that case we need no explicit syncing */
fd = fileno(f);
if (fd < 0)
return 0;
if (fsync(fd) < 0)
return -errno; return -errno;
r = fsync_directory_of_file(fd); r = fsync_directory_of_file(fileno(f));
if (r < 0) if (r < 0)
return r; return r;
@ -1009,7 +995,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) { int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
size_t n = 0, allocated = 0, count = 0; size_t n = 0, allocated = 0, count = 0;
_cleanup_free_ char *buffer = NULL; _cleanup_free_ char *buffer = NULL;
int r; int r, tty = -1;
assert(f); assert(f);
@ -1084,23 +1070,13 @@ int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
count++; count++;
if (eol != EOL_NONE) { if (eol != EOL_NONE) {
/* If we are on a tty, we can't shouldn't wait for more input, because that /* If we are on a tty, we can't wait for more input. But we expect only
* generally means waiting for the user, interactively. In the case of a TTY * \n as the single EOL marker, so there is no need to wait. We check
* we expect only \n as the single EOL marker, so we are in the lucky * this condition last to avoid isatty() check if not necessary. */
* position that there is no need to wait. We check this condition last, to
* avoid isatty() check if not necessary. */
if ((flags & (READ_LINE_IS_A_TTY|READ_LINE_NOT_A_TTY)) == 0) { if (tty < 0)
int fd; tty = isatty(fileno(f));
if (tty > 0)
fd = fileno(f);
if (fd < 0) /* Maybe an fmemopen() stream? Handle this gracefully,
* and don't call isatty() on an invalid fd */
flags |= READ_LINE_NOT_A_TTY;
else
flags |= isatty(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
}
if (FLAGS_SET(flags, READ_LINE_IS_A_TTY))
break; break;
} }

View File

@ -88,9 +88,7 @@ int read_timestamp_file(const char *fn, usec_t *ret);
int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space); int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
typedef enum ReadLineFlags { typedef enum ReadLineFlags {
READ_LINE_ONLY_NUL = 1 << 0, READ_LINE_ONLY_NUL = 1 << 0,
READ_LINE_IS_A_TTY = 1 << 1,
READ_LINE_NOT_A_TTY = 1 << 2,
} ReadLineFlags; } ReadLineFlags;
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret); int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret);

View File

@ -81,34 +81,31 @@ int chvt(int vt) {
int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) { int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
_cleanup_free_ char *line = NULL; _cleanup_free_ char *line = NULL;
struct termios old_termios; struct termios old_termios;
int r, fd; int r;
assert(f); assert(f);
assert(ret); assert(ret);
/* If this is a terminal, then switch canonical mode off, so that we can read a single /* If this is a terminal, then switch canonical mode off, so that we can read a single character */
* character. (Note that fmemopen() streams do not have an fd associated with them, let's handle that if (tcgetattr(fileno(f), &old_termios) >= 0) {
* nicely.) */
fd = fileno(f);
if (fd >= 0 && tcgetattr(fd, &old_termios) >= 0) {
struct termios new_termios = old_termios; struct termios new_termios = old_termios;
new_termios.c_lflag &= ~ICANON; new_termios.c_lflag &= ~ICANON;
new_termios.c_cc[VMIN] = 1; new_termios.c_cc[VMIN] = 1;
new_termios.c_cc[VTIME] = 0; new_termios.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSADRAIN, &new_termios) >= 0) { if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) {
char c; char c;
if (t != USEC_INFINITY) { if (t != USEC_INFINITY) {
if (fd_wait_for_event(fd, POLLIN, t) <= 0) { if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) {
(void) tcsetattr(fd, TCSADRAIN, &old_termios); (void) tcsetattr(fileno(f), TCSADRAIN, &old_termios);
return -ETIMEDOUT; return -ETIMEDOUT;
} }
} }
r = safe_fgetc(f, &c); r = safe_fgetc(f, &c);
(void) tcsetattr(fd, TCSADRAIN, &old_termios); (void) tcsetattr(fileno(f), TCSADRAIN, &old_termios);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)
@ -122,13 +119,8 @@ int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) {
} }
} }
if (t != USEC_INFINITY && fd > 0) { if (t != USEC_INFINITY) {
/* Let's wait the specified amount of time for input. When we have no fd we skip this, under if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0)
* the assumption that this is an fmemopen() stream or so where waiting doesn't make sense
* anyway, as the data is either already in the stream or cannot possible be placed there
* while we access the stream */
if (fd_wait_for_event(fd, POLLIN, t) <= 0)
return -ETIMEDOUT; return -ETIMEDOUT;
} }
@ -786,9 +778,6 @@ const char *default_term_for_tty(const char *tty) {
int fd_columns(int fd) { int fd_columns(int fd) {
struct winsize ws = {}; struct winsize ws = {};
if (fd < 0)
return -EBADF;
if (ioctl(fd, TIOCGWINSZ, &ws) < 0) if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
return -errno; return -errno;
@ -823,9 +812,6 @@ unsigned columns(void) {
int fd_lines(int fd) { int fd_lines(int fd) {
struct winsize ws = {}; struct winsize ws = {};
if (fd < 0)
return -EBADF;
if (ioctl(fd, TIOCGWINSZ, &ws) < 0) if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
return -errno; return -errno;

View File

@ -488,7 +488,7 @@ static int import_file(struct trie *trie, const char *filename, uint16_t file_pr
size_t len; size_t len;
char *pos; char *pos;
r = read_line_full(f, LONG_LINE_MAX, READ_LINE_NOT_A_TTY, &line); r = read_line(f, LONG_LINE_MAX, &line);
if (r < 0) if (r < 0)
return r; return r;
if (r == 0) if (r == 0)

View File

@ -294,7 +294,7 @@ int config_parse(const char *unit,
_cleanup_fclose_ FILE *ours = NULL; _cleanup_fclose_ FILE *ours = NULL;
unsigned line = 0, section_line = 0; unsigned line = 0, section_line = 0;
bool section_ignored = false, bom_seen = false; bool section_ignored = false, bom_seen = false;
int r, fd; int r;
assert(filename); assert(filename);
assert(lookup); assert(lookup);
@ -311,9 +311,7 @@ int config_parse(const char *unit,
} }
} }
fd = fileno(f); fd_warn_permissions(filename, fileno(f));
if (fd >= 0) /* stream might not have an fd, let's be careful hence */
fd_warn_permissions(filename, fd);
for (;;) { for (;;) {
_cleanup_free_ char *buf = NULL; _cleanup_free_ char *buf = NULL;