Program to let any user run a command I have a script which can only be run by one specific user on a system. I need to let all users on the system have access to run that script and see the output. But there must never be more than one instance of the script running simultaneously.

I came up with this program to be run as the specific user who can run the script. This program will be left running listening on a TCP port. Any user who want to run the script then connects to the TCP port and the script will run.

#include &lt;arpa/inet.h&gt;
#include &lt;errno.h&gt;
#include &lt;error.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/wait.h&gt;
#include &lt;unistd.h&gt;

int setup_socket()
{
  const int one = 1;
  struct sockaddr_in6 bind_addr = {
    .sin6_addr = IN6ADDR_LOOPBACK_INIT,
    .sin6_family = AF_INET6,
    .sin6_port = htons(65454),
    .sin6_flowinfo = 0,
    .sin6_scope_id = 0,
  };
  int fd = socket(PF_INET6, SOCK_STREAM, 0);

  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &amp;one, sizeof(one));

  if (bind(fd, (struct sockaddr*)&amp;bind_addr, sizeof(bind_addr)))
    error(EXIT_FAILURE, errno, "bind() failed");

  if (listen(fd, 5))
    error(EXIT_FAILURE, errno, "listen() failed");

  return fd;
}

int main(int argc, char ** argv)
{
  int fd;

  if (argc &lt; 2) {
    fprintf(stderr, "Usage: %s &lt;command&gt;\n", argv[0]);
    return EXIT_FAILURE;
  }

  fd = setup_socket();

  while (1) {
    int child_fd = accept(fd, NULL, NULL);
    pid_t child = fork();

    switch (child) {
    case -1:
      error(0, errno, "fork() failed");
      close(child_fd);
      break;
    case 0:
      if ((dup2(child_fd, 1) != 1) ||
          (dup2(child_fd, 2) != 2))
        error(EXIT_FAILURE, errno, "dup2() failed");
      execvp(argv[1], argv + 1);
      error(EXIT_FAILURE, errno, "excvp(%s) failed", argv[1]);
    default:
      close(child_fd);
      waitpid(child, NULL, 0);
    }
  }
}


Do you see any flaws in this program? I am mostly concerned with security problems, but also interested in hearing of any other problems you may see in the program.
This looks a bit unclear:


const int one = 1;



It's primarily used as the option_value argument of setsockopt().  Consider renaming this to something more clear so that it doesn't leave readers confused.
In addition to your other error-checking, you can also check the return value of socket() and print its error in case it fails.
A little nit-picky, but you can just initialize fd after the command line check instead of declaring it first and then assigning to it:

int fd = setup_socket();