You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.0 KiB
97 lines
2.0 KiB
10 years ago
|
/*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
9 years ago
|
*
|
||
7 years ago
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
9 years ago
|
*/
|
||
10 years ago
|
|
||
|
#include <errno.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <stdbool.h>
|
||
|
#include <stdint.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <sys/select.h>
|
||
10 years ago
|
#include <sys/socket.h>
|
||
9 years ago
|
#include <sys/stat.h>
|
||
|
#include <sys/types.h>
|
||
10 years ago
|
#include <unistd.h>
|
||
|
|
||
9 years ago
|
void fileNotClosed() {
|
||
|
int fd = open("hi.txt", O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||
|
if (fd != -1) {
|
||
|
char buffer[256];
|
||
|
// We can easily batch that by separating with space
|
||
|
write(fd, buffer, strlen(buffer));
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
void fileClosed() {
|
||
|
int fd = open("hi.txt", O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||
|
if (fd != -1) {
|
||
|
char buffer[256];
|
||
|
// We can easily batch that by separating with space
|
||
|
write(fd, buffer, strlen(buffer));
|
||
|
close(fd);
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
FILE* handler;
|
||
|
|
||
|
void fdopen_example() {
|
||
|
int fd = open("hi.txt", O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||
|
if (fd != -1) {
|
||
|
handler = fdopen(fd, "w");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void gzdopen_example() {
|
||
|
int fd = open("hi.txt", O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||
|
if (fd != -1) {
|
||
|
handler = gzdopen(fd, "w");
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void socketNotClosed() {
|
||
|
int fd = socket(AF_LOCAL, SOCK_RAW, 0);
|
||
|
if (fd != -1) {
|
||
|
char buffer[256];
|
||
|
// We can easily batch that by separating with space
|
||
|
write(fd, buffer, strlen(buffer));
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
int socketClosed() {
|
||
|
int socketFD = socket(AF_LOCAL, SOCK_RAW, 0);
|
||
|
if (socketFD == -1) {
|
||
|
return -1;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
int status;
|
||
10 years ago
|
|
||
9 years ago
|
status = fcntl(socketFD, F_SETFL, O_NONBLOCK);
|
||
|
if (status == -1) {
|
||
|
close(socketFD);
|
||
|
return -1;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
int reuseaddr = 1;
|
||
|
status = setsockopt(
|
||
|
socketFD, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
|
||
|
if (status == -1) {
|
||
|
close(socketFD);
|
||
|
return -1;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
int nosigpipe = 1;
|
||
|
status = setsockopt(
|
||
|
socketFD, SOL_SOCKET, SO_REUSEADDR, &nosigpipe, sizeof(nosigpipe));
|
||
|
if (status == -1) {
|
||
|
close(socketFD);
|
||
|
return -1;
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
return socketFD;
|
||
10 years ago
|
}
|