File: hash.c

package info (click to toggle)
microdc2 0.15.6-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 4,160 kB
  • ctags: 3,648
  • sloc: ansic: 31,186; sh: 4,024; makefile: 141; sed: 16
file content (164 lines) | stat: -rw-r--r-- 4,476 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* lookup.c - Performing name lookup with getaddrinfo
 *
 * Copyright (C) 2004, 2005 Oskar Liljeblad
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>

#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>

#include "gettext.h"            /* Gnulib/GNU gettext */
#define _(s) gettext(s)
#define N_(s) gettext_noop(s)
#include "xalloc.h"		/* Gnulib */
#include "xvasprintf.h"		/* Gnulib */
#include "common/msgq.h"
#include "common/byteq.h"
#include "common/ptrv.h"
#include "microdc.h"

#include "tth/tth.h"

MsgQ *hash_request_mq = NULL;
MsgQ *hash_result_mq = NULL;
pid_t hash_child;

static void
__attribute__((noreturn))
hash_main(int request_fd[2], int result_fd[2])
{
    MsgQ *request_mq;
    MsgQ *result_mq;
    struct sigaction sigact;

    close(request_fd[1]);
    close(result_fd[0]);
    request_mq = msgq_new(request_fd[0]);
    result_mq = msgq_new(result_fd[1]);

    /* Inability to register these signals is not a fatal error. */
    sigact.sa_flags = SA_RESTART;
    sigact.sa_handler = SIG_IGN;
#ifdef HAVE_STRUCT_SIGACTION_SA_RESTORER
    sigact.sa_restorer = NULL;
#endif
    sigaction(SIGINT, &sigact, NULL);
    sigaction(SIGTERM, &sigact, NULL);
    sigaction(SIGUSR1, &sigact, NULL);
    sigaction(SIGCHLD, &sigact, NULL);
    sigaction(SIGPIPE, &sigact, NULL);

    while (msgq_read_complete_msg(request_mq) > 0) {
        char *filename, *hash;
        struct stat st;

        msgq_get(request_mq, MSGQ_STR, &filename, MSGQ_END);

        /*
        fprintf(stderr, "HASH: begin processing %s\n", filename);
        fflush(stderr);
        */

        if (stat(filename, &st) < 0) {
            hash = xasprintf("FAILED");
        } else {
            char* tthl = NULL;
            size_t tthl_size;
            hash = tth(filename, &tthl, &tthl_size);
            if (tthl != NULL)
                free(tthl);
        }

        /*
        fprintf(stderr, "HASH: %s: %s\n", filename, hash);
        fflush(stderr);
        */

        msgq_put(result_mq, MSGQ_STR, hash, MSGQ_END);
        free(hash);
        if (msgq_write_all(result_mq) < 0) {
            /*
            fprintf(stderr, "HASH: msgq_write_all error: %d, %s\n", errno, errstr);
            fflush(stderr);
            */
            break;
        }
    }

    /* msgq_read_complete_msg may have failed if it returned < 0.
     * But we can't print any errors from this process (it would
     * interfere with the readline-managed display, so just exit
     * gracefully.
     */

    msgq_free(request_mq);
    msgq_free(result_mq);
    close(request_fd[0]);
    close(result_fd[1]);
    exit(EXIT_SUCCESS);
}

bool
hash_init(void)
{
    int request_fd[2];
    int result_fd[2];

    if (pipe(request_fd) != 0 || pipe(result_fd) != 0) {
        warn(_("Cannot create pipe pair - %s\n"), errstr);
        return false;
    }
    if (!fd_set_nonblock_flag(request_fd[1], true)
            || !fd_set_nonblock_flag(result_fd[0], true)) {
        warn(_("Cannot set non-blocking flag - %s\n"), errstr);
        return false;
    }

    hash_child = fork();
    if (hash_child < 0) {
        warn(_("Cannot create process - %s\n"), errstr);
        return false;
    }
    if (hash_child == 0) {
        setpriority(PRIO_PROCESS, 0, 16);
        hash_main(request_fd, result_fd);
    }

    close(request_fd[0]);
    close(result_fd[1]);
    hash_request_mq = msgq_new(request_fd[1]);
    hash_result_mq = msgq_new(result_fd[0]);
    return true;
}

void
hash_finish(void)
{
    if (hash_request_mq != NULL) {
        close(hash_request_mq->fd);
        msgq_free(hash_request_mq);
    }
    if (hash_result_mq != NULL) {
        close(hash_result_mq->fd);
        msgq_free(hash_result_mq);
    }
}