Getting “Too Many Files Open” Error in C Shared Library Despite ulimit Set to Unlimited

I’m working on a C program where I’ve created a shared library to intercept the connect system call and route the connection through a proxy server. The code works, but after running it for a while, I encounter a “Too Many Files Open” error, even though I’ve set ulimit -n to unlimited.

Here’s the relevant part of my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include "toralize.h"
Req *request(struct sockaddr_in *sock2) {
Req *req;
req = malloc(reqsize);
if (req == NULL) {
perror("malloc");
return NULL; // Handle the error appropriately in the calling function
}
req->vn = 4;
req->cd = 1;
req->dstip = sock2->sin_addr.s_addr;
req->dstport = sock2->sin_port;
strncpy(req->userid, USERNAME, 8);
return req;
}
int connect(int s2, const struct sockaddr *sock2, socklen_t addrlen) {
int s;
Req *req;
Res *res;
struct sockaddr_in sock;
char buf[ressize];
int success;
int (*p)(int, const struct sockaddr*, socklen_t);
p = dlsym(RTLD_NEXT, "connect");
if (p == NULL) {
perror("dlsym");
return -1;
}
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
perror("socket");
return -1;
}
sock.sin_family = AF_INET;
sock.sin_port = htons(PROXYPORT);
sock.sin_addr.s_addr = inet_addr(PROXY);
printf("Connecting to %s:%dn", inet_ntoa(sock.sin_addr), ntohs(sock.sin_port));
if (p(s, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
perror("connect");
close(s);
return -1;
}
printf("Connected to proxy.n");
req = request((struct sockaddr_in *)sock2);
if (req == NULL) {
close(s);
return -1;
}
if (write(s, req, reqsize) < 0) {
perror("write");
free(req);
close(s);
return -1;
}
memset(buf, 0, ressize);
if (read(s, buf, ressize) < 1) {
perror("read");
free(req);
close(s);
return -1;
}
res = (Res *)buf;
success = (res->cd == 90);
if (!success) {
fprintf(stderr, "Unable to traverse"
"the proxy, error code: %dn",
res->cd);
close(s);
free(req);
return -1;
}
printf("Connected through the proxy.n");
close(s2);
if (dup2(s, s2) < 0) {
perror("dup2");
free(req);
close(s);
return -1;
}
close(s);
free(req);
return 0;
}
</code>
<code>#include "toralize.h" Req *request(struct sockaddr_in *sock2) { Req *req; req = malloc(reqsize); if (req == NULL) { perror("malloc"); return NULL; // Handle the error appropriately in the calling function } req->vn = 4; req->cd = 1; req->dstip = sock2->sin_addr.s_addr; req->dstport = sock2->sin_port; strncpy(req->userid, USERNAME, 8); return req; } int connect(int s2, const struct sockaddr *sock2, socklen_t addrlen) { int s; Req *req; Res *res; struct sockaddr_in sock; char buf[ressize]; int success; int (*p)(int, const struct sockaddr*, socklen_t); p = dlsym(RTLD_NEXT, "connect"); if (p == NULL) { perror("dlsym"); return -1; } s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) { perror("socket"); return -1; } sock.sin_family = AF_INET; sock.sin_port = htons(PROXYPORT); sock.sin_addr.s_addr = inet_addr(PROXY); printf("Connecting to %s:%dn", inet_ntoa(sock.sin_addr), ntohs(sock.sin_port)); if (p(s, (struct sockaddr *)&sock, sizeof(sock)) < 0) { perror("connect"); close(s); return -1; } printf("Connected to proxy.n"); req = request((struct sockaddr_in *)sock2); if (req == NULL) { close(s); return -1; } if (write(s, req, reqsize) < 0) { perror("write"); free(req); close(s); return -1; } memset(buf, 0, ressize); if (read(s, buf, ressize) < 1) { perror("read"); free(req); close(s); return -1; } res = (Res *)buf; success = (res->cd == 90); if (!success) { fprintf(stderr, "Unable to traverse" "the proxy, error code: %dn", res->cd); close(s); free(req); return -1; } printf("Connected through the proxy.n"); close(s2); if (dup2(s, s2) < 0) { perror("dup2"); free(req); close(s); return -1; } close(s); free(req); return 0; } </code>
#include "toralize.h"

Req *request(struct sockaddr_in *sock2)  {
  Req *req;

  req = malloc(reqsize);
  if (req == NULL) {
    perror("malloc");
    return NULL; // Handle the error appropriately in the calling function
  }
  req->vn = 4;
  req->cd = 1;
  req->dstip = sock2->sin_addr.s_addr;
  req->dstport = sock2->sin_port;
  strncpy(req->userid, USERNAME, 8);
  
  return req;
}

int connect(int s2, const struct sockaddr *sock2, socklen_t addrlen)  {

  int s;
  Req *req;
  Res *res;
  struct sockaddr_in sock;
  char buf[ressize];
  int success;
  int (*p)(int, const struct sockaddr*, socklen_t);

  p = dlsym(RTLD_NEXT, "connect");
  if (p == NULL) {
    perror("dlsym");
    return -1;
  }

  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0)  {
    perror("socket");
    return -1;
  }

  sock.sin_family = AF_INET;
  sock.sin_port = htons(PROXYPORT);
  sock.sin_addr.s_addr = inet_addr(PROXY);

  printf("Connecting to %s:%dn", inet_ntoa(sock.sin_addr), ntohs(sock.sin_port));

  if (p(s, (struct sockaddr *)&sock, sizeof(sock)) < 0) {
    perror("connect");
    close(s);
    return -1;
  }

  printf("Connected to proxy.n");

  req = request((struct sockaddr_in *)sock2);
  if (req == NULL) {
    close(s);
    return -1;
  }

  if (write(s, req, reqsize) < 0) {
    perror("write");
    free(req);
    close(s);
    return -1;
  }

  memset(buf, 0, ressize);
  if (read(s, buf, ressize) < 1)  {
    perror("read");
    free(req);
    close(s);
    return -1;
  }

  res = (Res *)buf;
  success = (res->cd == 90);

  if (!success) {
    fprintf(stderr, "Unable to traverse"
            "the proxy, error code: %dn",
            res->cd);
    close(s);
    free(req);
    return -1;
  }

  printf("Connected through the proxy.n");
  
  close(s2);
  if (dup2(s, s2) < 0) {
    perror("dup2");
    free(req);
    close(s);
    return -1;
  }
  
  close(s);
  free(req);
  return 0;
}

Problem: Even though I’ve set ulimit -n to unlimited, I’m still getting a “Too Many Files Open” error after a while. I suspect this might be due to file descriptor leakage, but I’m not sure where the issue lies. I’m properly closing file descriptors in most places, but something seems to be missing.

toralize.h in case if there’s an issue with that file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/* toralize.h */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<dlfcn.h>
#define PROXY "127.0.0.1"
#define PROXYPORT 9050
#define reqsize sizeof(struct proxy_request)
#define ressize sizeof(struct proxy_response)
#define USERNAME "toraliz"
typedef unsigned char int8;
typedef unsigned short int int16;
typedef unsigned int int32;
struct proxy_request {
int8 vn;
int8 cd;
int16 dstport;
int32 dstip;
unsigned char userid[8];
};
typedef struct proxy_request Req;
struct proxy_response {
int8 vn;
int8 cd;
int16 _;
int32 __;
};
typedef struct proxy_response Res;
Req *request(struct sockaddr_in*);
int connect(int, const struct sockaddr*, socklen_t);
</code>
<code>/* toralize.h */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<unistd.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netinet/in.h> #include<dlfcn.h> #define PROXY "127.0.0.1" #define PROXYPORT 9050 #define reqsize sizeof(struct proxy_request) #define ressize sizeof(struct proxy_response) #define USERNAME "toraliz" typedef unsigned char int8; typedef unsigned short int int16; typedef unsigned int int32; struct proxy_request { int8 vn; int8 cd; int16 dstport; int32 dstip; unsigned char userid[8]; }; typedef struct proxy_request Req; struct proxy_response { int8 vn; int8 cd; int16 _; int32 __; }; typedef struct proxy_response Res; Req *request(struct sockaddr_in*); int connect(int, const struct sockaddr*, socklen_t); </code>
/* toralize.h */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<dlfcn.h>
#define PROXY "127.0.0.1"
#define PROXYPORT 9050
#define reqsize sizeof(struct proxy_request)
#define ressize sizeof(struct proxy_response)
#define USERNAME "toraliz" 
typedef unsigned char int8;
typedef unsigned short int int16;
typedef unsigned int int32;

struct proxy_request  {
  int8 vn;
  int8 cd;
  int16 dstport;
  int32 dstip;
  unsigned char userid[8];
};

typedef struct proxy_request Req;

struct proxy_response {
  int8 vn;
  int8 cd;
  int16 _;
  int32 __;
};
typedef struct proxy_response Res;

Req *request(struct sockaddr_in*);
int connect(int, const struct sockaddr*, socklen_t);

Lastly my build command:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>gcc toralize.c -o toralize.so -fPIC -shared -ldl -D_GNU_SOURCE
</code>
<code>gcc toralize.c -o toralize.so -fPIC -shared -ldl -D_GNU_SOURCE </code>
gcc toralize.c -o toralize.so -fPIC -shared -ldl -D_GNU_SOURCE

What I’ve Tried:

  • Verified that ulimit -n is set to unlimited.

  • Checked the code for places where file descriptors might not be properly closed.

Question:

  • What could be causing the “Too Many Files Open” error in this scenario?

  • How can I fix or debug this issue more effectively?

Any help or insights would be greatly appreciated!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật