server_start.c
author Lluís Batlle i Rossell <viric@viric.name>
Mon, 01 Apr 2024 11:06:42 +0200
changeset 379 1f8972dab2c1
parent 373 453b2b0a833c
permissions -rw-r--r--
Added signature for changeset 13554e106f17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     1
/*
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     2
    Task Spooler - a task queue system for the unix user
267
11631dd11ff8 Updating copyright years in the source.
viric@mandarina
parents: 251
diff changeset
     3
    Copyright (C) 2007-2009  LluĂ­s Batlle i Rossell
49
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     4
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     5
    Please find the license in the provided COPYING file.
09bb8a5583e9 Added the license.
lbatlle@npdl268.bpo.hp.com
parents: 44
diff changeset
     6
*/
44
4dcf05746ece Better include files.
viric@llimona
parents: 28
diff changeset
     7
#include <unistd.h>
4dcf05746ece Better include files.
viric@llimona
parents: 28
diff changeset
     8
#include <string.h>
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
     9
#include <sys/types.h>
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    10
#include <sys/socket.h>
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    11
#include <sys/un.h>
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    12
#include <errno.h>
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    13
#include <stdlib.h>
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 3
diff changeset
    14
#include <stdio.h>
146
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents: 133
diff changeset
    15
#include <sys/time.h>
302
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    16
#include <sys/stat.h>
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
    17
#include <signal.h>
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    18
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
    19
#include "main.h"
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    20
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    21
extern int server_socket;
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    22
251
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
    23
static char *socket_path;
302
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    24
static int should_check_owner = 0;
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    25
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
    26
static int fork_server(int ls);
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
    27
251
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
    28
void create_socket_path(char **path)
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    29
{
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    30
    char *tmpdir;
119
361b08d33762 $USER is no more used for crating the socket path. Now the UID is used.
viric@llimona
parents: 92
diff changeset
    31
    char userid[20];
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    32
    int size;
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    33
64
2a17c9c7b1d5 Added a limit to the number of finished jobs.
viric@llimona
parents: 49
diff changeset
    34
    /* As a priority, TS_SOCKET mandates over the path creation */
251
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
    35
    *path = getenv("TS_SOCKET");
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
    36
    if (*path != 0)
64
2a17c9c7b1d5 Added a limit to the number of finished jobs.
viric@llimona
parents: 49
diff changeset
    37
    {
2a17c9c7b1d5 Added a limit to the number of finished jobs.
viric@llimona
parents: 49
diff changeset
    38
        /* We need this in our memory, for forks and future 'free'. */
251
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
    39
        size = strlen(*path) + 1;
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
    40
        *path = (char *) malloc(size);
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
    41
        strcpy(*path, getenv("TS_SOCKET"));
302
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    42
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    43
        /* We don't want to check ownership of the socket here,
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    44
         * as the user may have thought of some shared queue */
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    45
        should_check_owner = 0;
64
2a17c9c7b1d5 Added a limit to the number of finished jobs.
viric@llimona
parents: 49
diff changeset
    46
        return;
2a17c9c7b1d5 Added a limit to the number of finished jobs.
viric@llimona
parents: 49
diff changeset
    47
    }
2a17c9c7b1d5 Added a limit to the number of finished jobs.
viric@llimona
parents: 49
diff changeset
    48
2a17c9c7b1d5 Added a limit to the number of finished jobs.
viric@llimona
parents: 49
diff changeset
    49
    /* ... if the $TS_SOCKET doesn't exist ... */
2a17c9c7b1d5 Added a limit to the number of finished jobs.
viric@llimona
parents: 49
diff changeset
    50
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    51
    /* Create the path */
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    52
    tmpdir = getenv("TMPDIR");
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    53
    if (tmpdir == NULL)
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    54
        tmpdir = "/tmp";
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    55
119
361b08d33762 $USER is no more used for crating the socket path. Now the UID is used.
viric@llimona
parents: 92
diff changeset
    56
    sprintf(userid, "%u", (unsigned int) getuid());
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    57
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    58
    /* Calculate the size */
119
361b08d33762 $USER is no more used for crating the socket path. Now the UID is used.
viric@llimona
parents: 92
diff changeset
    59
    size = strlen(tmpdir) + strlen("/socket-ts.") + strlen(userid) + 1;
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    60
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    61
    /* Freed after preparing the socket address */
251
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
    62
    *path = (char *) malloc(size);
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    63
251
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
    64
    sprintf(*path, "%s/socket-ts.%s", tmpdir, userid);
302
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    65
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    66
    should_check_owner = 1;
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    67
}
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
    68
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    69
int try_connect(int s)
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    70
{
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    71
    struct sockaddr_un addr;
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    72
    int res;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    73
359
3cbf0e10ef48 Quick fix for Bug#975321
Alexander Inyukhin <shurick@sectorb.msk.ru>
parents: 302
diff changeset
    74
    memset(&addr, 0, sizeof(addr));
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    75
    addr.sun_family = AF_UNIX;
359
3cbf0e10ef48 Quick fix for Bug#975321
Alexander Inyukhin <shurick@sectorb.msk.ru>
parents: 302
diff changeset
    76
    strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
3cbf0e10ef48 Quick fix for Bug#975321
Alexander Inyukhin <shurick@sectorb.msk.ru>
parents: 302
diff changeset
    77
    if (strcmp(socket_path, addr.sun_path))
3cbf0e10ef48 Quick fix for Bug#975321
Alexander Inyukhin <shurick@sectorb.msk.ru>
parents: 302
diff changeset
    78
        error("Cannot create the socket '%s'. Probably, the name is too long.", socket_path);
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    79
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    80
    res = connect(s, (struct sockaddr *) &addr, sizeof(addr));
302
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    81
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
    82
    return res;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    83
}
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
    84
302
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    85
static void
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    86
try_check_ownership()
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    87
{
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    88
    int res;
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    89
    struct stat socketstat;
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    90
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    91
    if (!should_check_owner)
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    92
        return;
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    93
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    94
    res = stat(socket_path, &socketstat);
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    95
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    96
    if (res != 0)
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    97
        error("Cannot state the socket %s.", socket_path);
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    98
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
    99
    if (socketstat.st_uid != getuid())
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
   100
        error("The uid %i does not own the socket %s.", getuid(), socket_path);
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
   101
}
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
   102
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   103
void wait_server_up(int fd)
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   104
{
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   105
    char a;
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   106
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   107
    read(fd, &a, 1);
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   108
    close(fd);
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   109
}
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   110
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   111
/* Returns the fd where to wait for the parent notification */
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   112
static int fork_server(int ls)
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   113
{
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   114
    int pid;
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   115
    int p[2];
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   116
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   117
    /* !!! stdin/stdout */
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   118
    pipe(p);
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   119
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   120
    pid = fork();
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   121
    switch (pid)
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   122
    {
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   123
        case 0: /* Child */
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   124
            close(p[0]);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   125
            close(server_socket);
66
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 64
diff changeset
   126
            /* Close all std handles for the server */
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 64
diff changeset
   127
            close(0);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 64
diff changeset
   128
            close(1);
f70a27afd92e Adding "swap jobs" with -U. Fixed a big problem with fds 0, 1, 2.
viric@mandarina
parents: 64
diff changeset
   129
            close(2);
133
4306e96f5906 Added setsid when going background.
viric@llimona
parents: 129
diff changeset
   130
            setsid();
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   131
            server_main(p[1], socket_path, ls);
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   132
            exit(0);
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   133
            break;
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   134
        case -1: /* Error */
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   135
            return -1;
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   136
        default: /* Parent */
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   137
            close(ls);
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   138
            close(p[1]);
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   139
    }
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   140
    /* Return the read fd */
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   141
    return p[0];
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   142
}
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   143
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   144
void notify_parent(int fd)
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   145
{
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   146
    char a = 'a';
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   147
    write(fd, &a, 1);
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   148
    close(fd);
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   149
}
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   150
368
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   151
static int is_path_unixsocket(const char *path)
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   152
{
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   153
    int res;
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   154
    struct stat socketstat;
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   155
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   156
    res = stat(path, &socketstat);
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   157
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   158
    if (res == 0 && S_ISSOCK(socketstat.st_mode))
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   159
        return 1;
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   160
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   161
    return 0;
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   162
}
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   163
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   164
static int open_server_socket()
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   165
{
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   166
    int res;
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   167
    int ls;
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   168
    struct sockaddr_un addr;
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   169
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   170
    ls = socket(AF_UNIX, SOCK_STREAM, 0);
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   171
    if(ls == -1)
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   172
        error("cannot create the listen socket in the server");
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   173
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   174
    addr.sun_family = AF_UNIX;
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   175
    strcpy(addr.sun_path, socket_path);
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   176
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   177
    res = bind(ls, (struct sockaddr *) &addr, sizeof(addr));
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   178
    if (res == -1)
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   179
    {
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   180
        warning("Error binding.");
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   181
        close(ls);
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   182
        return -1;
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   183
    }
368
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   184
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   185
    res = listen(ls, 0);
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   186
    if (res == -1)
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   187
    {
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   188
        /* If bind succeeded, can this fail? */
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   189
        warning("Error listening.");
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   190
        close(ls);
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   191
        return -1;
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   192
    }
368
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   193
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   194
    return ls;
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   195
}
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   196
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   197
int ensure_server_up()
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   198
{
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   199
    int res;
25
e9e4babe6262 Now the server is quicly started.
viric@llimona
parents: 9
diff changeset
   200
    int notify_fd;
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   201
    int server_bind_fd = -1;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   202
129
312083fb8899 Change PF_UNIX to AF_UNIX. According to socket(2), this is better.
viric@llimona
parents: 119
diff changeset
   203
    server_socket = socket(AF_UNIX, SOCK_STREAM, 0);
92
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 66
diff changeset
   204
    if (server_socket == -1)
05004c52ecff Better error reports on internal handled errors.
viric@llimona
parents: 66
diff changeset
   205
        error("getting the server socket");
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   206
251
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
   207
    create_socket_path(&socket_path);
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
   208
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   209
    res = try_connect(server_socket);
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   210
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   211
    /* Good connection */
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   212
    if (res == 0)
302
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
   213
    {
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
   214
        try_check_ownership();
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   215
        return 1;
302
c60e0db23bd7 Adding a check for ownership in the socket.
viric <viriketo@gmail.com>
parents: 267
diff changeset
   216
    }
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   217
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   218
    /* If error other than "No one listens on the other end"... */
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   219
    if (!(errno == ENOENT || errno == ECONNREFUSED))
373
453b2b0a833c Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 369
diff changeset
   220
        error("c: cannot connect to the server at '%s': %s",
453b2b0a833c Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 369
diff changeset
   221
                socket_path, strerror(errno));
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   222
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   223
    if (errno == ECONNREFUSED)
368
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   224
    {
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   225
        if (!is_path_unixsocket(socket_path))
373
453b2b0a833c Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 369
diff changeset
   226
            error("The socket path exists and it is not a unix socket: %s",
453b2b0a833c Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 369
diff changeset
   227
                    socket_path);
453b2b0a833c Fix error message about bad ts_socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 369
diff changeset
   228
251
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
   229
        unlink(socket_path);
368
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   230
    }
3
2fb8a6bdd024 More code.
viric@llimona
parents: 2
diff changeset
   231
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   232
    /* Try to start a server. It may fail if multiple clients try to do this at oncei. */
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   233
    server_bind_fd = open_server_socket();
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   234
    if (server_bind_fd != -1)
368
8dc7e38d782c Do not unlink if the socket_path is not a unix socket
Lluís Batlle i Rossell <viric@viric.name>
parents: 359
diff changeset
   235
    {
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   236
        /* If it worked, wait for server to start. Otherwise, repeat connect */
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   237
        notify_fd = fork_server(server_bind_fd);
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   238
        wait_server_up(notify_fd);
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   239
    }
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   240
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   241
    /* Connect after either server start or reattempt because of clash at start */
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   242
    res = try_connect(server_socket);
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   243
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   244
    /* The second time didn't work. Abort. */
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   245
    if (res == -1)
9
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 3
diff changeset
   246
    {
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 3
diff changeset
   247
        fprintf(stderr, "The server didn't come up.\n");
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 3
diff changeset
   248
        exit(-1);
9acd8ae3190c First usable version!
lbatlle@npdl268.bpo.hp.com
parents: 3
diff changeset
   249
    }
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   250
369
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   251
    try_check_ownership();
040912c941e4 Try to fix the server start race.
Lluís Batlle i Rossell <viric@viric.name>
parents: 368
diff changeset
   252
251
5e0802df5788 Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents: 231
diff changeset
   253
    free(socket_path);
28
107abb4ec98a Unified socket path, depending on the username and $TMPDIR.
viric@llimona
parents: 25
diff changeset
   254
2
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   255
    /* Good connection on the second time */
602bd67df3aa Changed tabs to 4 spaces. Rule.
viric@llimona
parents: 1
diff changeset
   256
    return 1;
1
74928e2b27f4 Honta versio de ia strukturo.
viric@llimona
parents:
diff changeset
   257
}