Skip to content

Commit 8c9fc4c

Browse files
Listeria monocytogenesFrancesco149
Listeria monocytogenes
authored andcommitted
close(clientfd) in the main thread
we can forget about http_drop() and shutdown() since the thread serving the request closes the socket on exit.
1 parent 16eb1e4 commit 8c9fc4c

File tree

2 files changed

+2
-10
lines changed

2 files changed

+2
-10
lines changed

httpd.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#define IPPROTO_TCP 6
44
#define SO_REUSEADDR 2
55
#define SOL_SOCKET 1
6-
#define SHUT_RDWR 2
76
#define O_RDONLY 0
87

98
typedef unsigned short uint16_t;
@@ -27,7 +26,6 @@ int close(int fd);
2726
int socket(int domain, int type, int protocol);
2827
int accept(int socket, sockaddr_in_t *restrict address,
2928
socklen_t *restrict address_len);
30-
int shutdown(int socket, int how);
3129
int bind(int socket, const sockaddr_in_t *address, socklen_t address_len);
3230
int listen(int socket, int backlog);
3331
int setsockopt(int socket, int level, int option_name, const void *option_value,
@@ -99,11 +97,6 @@ static void http_consume(int clientfd, char *http_buf, size_t buf_len) {
9997
}
10098
}
10199

102-
static void http_drop(int clientfd) {
103-
shutdown(clientfd, SHUT_RDWR);
104-
close(clientfd);
105-
}
106-
107100
/*
108101
* we're supposed to send content-length but shutting down the
109102
* socket seems to be enough, saves some code
@@ -138,7 +131,6 @@ static int http_serve(int clientfd, const char *file_path, char *http_buf,
138131
if (n < 0) {
139132
perror("read");
140133
}
141-
http_drop(clientfd);
142134
return 0;
143135
}
144136

@@ -179,6 +171,7 @@ int main(int argc, char *argv[]) {
179171
} else if (pid == 0) {
180172
return http_serve(clientfd, argv[2], http_buf, sizeof(http_buf));
181173
}
174+
close(clientfd);
182175
}
183176
return 0;
184177
}

start.S

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ c(exit, 3) /* 60 */
1414
c(fork, 3) /* 57 */
1515
c(setsockopt, 4) /* 54 */
1616
c(listen, 1) /* 50 */
17-
c(bind, 1) /* 49 */
18-
c(shutdown, 5) /* 48 */
17+
c(bind, 6) /* 49 */
1918
c(accept, 2) /* 43 */
2019
c(socket, 38) /* 41 */
2120
c(close, 1) /* 03 */

0 commit comments

Comments
 (0)