-
Notifications
You must be signed in to change notification settings - Fork 7
Use fstat() to check if there is a socket or not #31
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #31 +/- ##
==========================================
+ Coverage 64.87% 65.73% +0.86%
==========================================
Files 13 13
Lines 1170 1179 +9
==========================================
+ Hits 759 775 +16
+ Misses 411 404 -7
Continue to review full report at Codecov.
|
bin/main.c
Outdated
|
||
for (int i = 0; i < 2; i++) { | ||
if (!pfds[i].revents) | ||
continue; | ||
|
||
ret = read(pfds[i].fd, buffer, sizeof(buffer)); | ||
if (fstat(pfds[i].fd, &st)) { | ||
fprintf(stderr, "Error in fstat.\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also log what the error is.
bin/non.c
Outdated
struct stat st; | ||
|
||
if (fstat(fd, &st)) { | ||
fprintf(stderr, "Error in fstat.\n"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Here too.)
bin/main.c
Outdated
if (S_ISSOCK(st.st_mode)) { | ||
ret = recv(pfds[i].fd, buffer, sizeof(buffer), 0); | ||
} else { | ||
ret = read(pfds[i].fd, buffer, sizeof(buffer)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong indentation.
bad47ff
to
db4a765
Compare
} | ||
|
||
if (S_ISSOCK(st.st_mode)) { | ||
ret = recv(pfds[i].fd, buffer, sizeof(buffer), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, don't use braces for a single-line if statement.
if (S_ISSOCK(st.st_mode)) { | ||
ret = recv(pfds[i].fd, buffer, sizeof(buffer), 0); | ||
} else { | ||
ret = read(pfds[i].fd, buffer, sizeof(buffer)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, don't use braces for a single-line if statement.
bin/non.c
Outdated
|
||
if (fstat(fd, &st)) { | ||
fprintf(stderr, "Error in fstat. Errno is %d\n", errno); | ||
return -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, don't use braces for a single-line if statement.
bin/non.c
Outdated
} | ||
|
||
if (S_ISSOCK(st.st_mode)) { | ||
ret = send(fd, buf, len, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, don't use braces for a single-line if statement.
bin/non.c
Outdated
if (S_ISSOCK(st.st_mode)) { | ||
ret = send(fd, buf, len, 0); | ||
} else { | ||
ret = write(fd, buf, len); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, don't use braces for a single-line if statement.
db4a765
to
950ddf3
Compare
bin/main.c
Outdated
|
||
for (int i = 0; i < 2; i++) { | ||
if (!pfds[i].revents) | ||
continue; | ||
|
||
ret = read(pfds[i].fd, buffer, sizeof(buffer)); | ||
if (fstat(pfds[i].fd, &st)) { | ||
fprintf(stderr, "Error in fstat. Errno is %d\n", errno); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
%m or strerror, please.
bin/non.c
Outdated
struct stat st; | ||
|
||
if (fstat(fd, &st)) { | ||
fprintf(stderr, "Error in fstat. Errno is %d\n", errno); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here too.
recv/send function can only be applied to a socket. We can use fstat() function to check if the fd is a socket or not. If so, we would like to use recv/send function to replace read/write. Related to:enarx-archive#30 Signed-off-by: Ke Zhao <[email protected]>
950ddf3
to
6588580
Compare
recv/send function can only be applied to a socket.
We can use fstat() function to check if the fd is a socket or not.
If so, we would like to use recv/send function to replace read/write.
Related to:#30
Signed-off-by: Ke Zhao [email protected]