Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:58:12+00:00 2026-05-28T01:58:12+00:00

I’ve written a server that accepts a socket connection on a secondary port for

  • 0

I’ve written a server that accepts a socket connection on a secondary port for the purposes of streaming debugging information that normally goes to stderr. This second port –an error serving port– is only intended to have one connection at a time, which, is convenient, because it allows to me redirect stderr using a dup2(2) call. (See Can I redirect a parent process's stderr to a socket file descriptor on a forked process?).

The following code is nearly satisfactory in every regard. When a client logs into the port, the stderr stream is directed to the socket. When another client logs in, the stream is redirected again, and the first client stops receiving: entirely satisfactory.

Where it falls short in the design is when the client closes the connection, the server crashes because it is trying to write() to a socket that is closed.

I’ve got a rudimentary signal handler for the normal child processes, but I’m not sure how to handle the specific signal from the parent process when the error socket closes.

How can I trap the signal (in the parent) that the connection on the ERR_PORT_NUM has closed and have the signal handler reopen (or dup) stderr back to /dev/null for the next awaiting error client?

Also, what should I do with an original error client connection when a second connects? Currently the first client is left dangling. Even a non-graceful shut-down of the first connection is acceptable.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <errno.h>
#include <pwd.h>
#include <signal.h>
#include <netinet/in.h>
#include <sys/mman.h>

#define PORT_NUM 12345
#define ERR_PORT_NUM 54321

static void child_handler(int signum)
{
    switch (signum) {
        case SIGALRM:
            exit(EXIT_FAILURE);
            break;
        case SIGUSR1:
            exit(EXIT_SUCCESS);
            break;
        case SIGCHLD:
            exit(EXIT_FAILURE);
            break;
    }
}

static void daemonize(void)
{
    /* Trap signals that we expect to recieve */
    signal(SIGUSR1, child_handler);
    signal(SIGALRM, child_handler);

    signal(SIGCHLD, SIG_IGN);   /* A child process dies */
    signal(SIGTSTP, SIG_IGN);   /* Various TTY signals */
    signal(SIGTTOU, SIG_IGN);
    signal(SIGTTIN, SIG_IGN);
    signal(SIGHUP, SIG_IGN);    /* Ignore hangup signal */
    signal(SIGTERM, SIG_DFL);   /* Die on SIGTERM */

    freopen("/dev/null", "r", stdin);
    freopen("/dev/null", "w", stdout);
    freopen("/dev/null", "w", stderr);
}

static void server_work(void)
{
    int sockfd, err_sockfd;
    socklen_t clilen;
    struct sockaddr_in serv_addr, cli_addr, err_serv_addr, err_cli_addr;
    struct timeval tv = { 0 };
    int new_stderr;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    err_sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0 || err_sockfd < 0)
        return;

    memset((char *) &serv_addr, '\0', sizeof(serv_addr));
    memset((char *) &err_serv_addr, '\0', sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(PORT_NUM);

    err_serv_addr.sin_family = AF_INET;
    err_serv_addr.sin_addr.s_addr = INADDR_ANY;
    err_serv_addr.sin_port = htons(ERR_PORT_NUM);

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
        < 0)
        return;
    if (bind
        (err_sockfd, (struct sockaddr *) &err_serv_addr,
         sizeof(err_serv_addr)) < 0)
        return;

    listen(sockfd, 5);
    listen(err_sockfd, 5);

    clilen = sizeof(cli_addr);

    while (1) {
        int maxfd;
        fd_set read_sockets_set;

        FD_ZERO(&read_sockets_set);
        FD_SET(sockfd, &read_sockets_set);
        FD_SET(err_sockfd, &read_sockets_set);

        maxfd = (err_sockfd > sockfd) ? err_sockfd : sockfd;

        if (select(maxfd + 1, &read_sockets_set, NULL, NULL, NULL) < 0) {
            break;
        }
        if (FD_ISSET(sockfd, &read_sockets_set)) {
            /* Typical process fork(2) and such ... not gremaine to the question. */
        }
        if (FD_ISSET(err_sockfd, &read_sockets_set)) {
            new_stderr =
                accept(err_sockfd, (struct sockaddr *) &err_cli_addr,
                       &clilen);
            dup2(new_stderr, STDERR_FILENO);
        }
    }
    close(sockfd);
    close(err_sockfd);
    return;
}

int main(int argc, char *argv[])
{
    daemonize();                /* greatly abbreviated for question */

    server_work();
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:58:12+00:00Added an answer on May 28, 2026 at 1:58 am

    You could simply ignore SIGPIPE. It’s a useless, annoying signal.

    signal(SIGPIPE, SIG_IGN);
    

    If you ignore it then your program will instead receive an EPIPE error code from the failed write() call. This lets you handle the I/O error at a sensible place in your code rather than in some global signal handler.

    EPIPE

    fd is connected to a pipe or socket whose reading end is closed. When this happens the writing process will also receive a SIGPIPE signal. (Thus, the write return value is seen only if the program catches, blocks or ignores this signal.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.