Mercurial > dovecot > core-2.2
annotate src/master/dict-process.c @ 5843:f655c4d4a419 HEAD
Moved child process handling to child-process.[ch]. The hash table now uses
pointers to structures instead of a casted process type. This allowed
removing another login-processes hash table.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sat, 30 Jun 2007 17:38:17 +0300 |
parents | 5b4c9b20eba0 |
children | 21e529b8a701 |
rev | line source |
---|---|
3855 | 1 /* Copyright (C) 2006 Timo Sirainen */ |
2 | |
3 #include "common.h" | |
4393
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
4 #include "array.h" |
3855 | 5 #include "ioloop.h" |
6 #include "network.h" | |
7 #include "fd-close-on-exec.h" | |
8 #include "env-util.h" | |
9 #include "log.h" | |
5843
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
10 #include "child-process.h" |
3855 | 11 #include "dict-process.h" |
12 | |
13 #include <syslog.h> | |
14 #include <unistd.h> | |
15 #include <sys/stat.h> | |
16 | |
3885
b624b687fe13
Create dict-server socket to base_dir.
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
17 #define DICT_SERVER_SOCKET_NAME "dict-server" |
3855 | 18 |
19 struct dict_process { | |
5843
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
20 struct child_process process; |
3885
b624b687fe13
Create dict-server socket to base_dir.
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
21 char *path; |
3855 | 22 int fd; |
4393
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
23 |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
24 struct log_io *log; |
3855 | 25 struct io *io; |
26 }; | |
27 | |
28 static struct dict_process *process; | |
29 | |
30 static void dict_process_unlisten(struct dict_process *process); | |
31 | |
32 static int dict_process_start(struct dict_process *process) | |
33 { | |
34 struct log_io *log; | |
4393
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
35 const char *executable, *const *dicts; |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
36 unsigned int i, count; |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
37 int log_fd; |
3855 | 38 pid_t pid; |
39 | |
40 log_fd = log_create_pipe(&log, 0); | |
41 if (log_fd < 0) | |
42 pid = -1; | |
43 else { | |
44 pid = fork(); | |
45 if (pid < 0) | |
46 i_error("fork() failed: %m"); | |
47 } | |
48 | |
49 if (pid < 0) { | |
50 (void)close(log_fd); | |
51 return -1; | |
52 } | |
53 | |
54 if (pid != 0) { | |
55 /* master */ | |
5843
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
56 child_process_add(pid, &process->process); |
3855 | 57 log_set_prefix(log, "dict: "); |
58 (void)close(log_fd); | |
59 | |
4393
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
60 process->log = log; |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
61 log_ref(process->log); |
3855 | 62 dict_process_unlisten(process); |
63 return 0; | |
64 } | |
65 log_set_prefix(log, "master-dict: "); | |
66 | |
67 /* set stdin and stdout to /dev/null, so anything written into it | |
68 gets ignored. */ | |
69 if (dup2(null_fd, 0) < 0) | |
70 i_fatal("dup2(stdin) failed: %m"); | |
71 if (dup2(null_fd, 1) < 0) | |
72 i_fatal("dup2(stdout) failed: %m"); | |
73 | |
74 /* stderr = log, 3 = listener */ | |
75 if (dup2(log_fd, 2) < 0) | |
76 i_fatal("dup2(stderr) failed: %m"); | |
77 if (dup2(process->fd, 3) < 0) | |
78 i_fatal("dup2(3) failed: %m"); | |
79 | |
80 for (i = 0; i <= 3; i++) | |
81 fd_close_on_exec(i, FALSE); | |
82 | |
83 child_process_init_env(); | |
84 env_put(t_strconcat("DICT_LISTEN_FROM_FD=", process->path, NULL)); | |
85 | |
4393
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
86 dicts = array_get(&settings_root->dicts, &count); |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
87 i_assert((count % 2) == 0); |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
88 for (i = 0; i < count; i += 2) |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
89 env_put(t_strdup_printf("DICT_%s=%s", dicts[i], dicts[i+1])); |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
90 |
3855 | 91 /* make sure we don't leak syslog fd, but do it last so that |
92 any errors above will be logged */ | |
93 closelog(); | |
94 | |
95 executable = PKG_LIBEXECDIR"/dict"; | |
96 client_process_exec(executable, ""); | |
97 i_fatal_status(FATAL_EXEC, "execv(%s) failed: %m", executable); | |
98 return -1; | |
99 } | |
100 | |
4907
5b4c9b20eba0
Replaced void *context from a lot of callbacks with the actual context
Timo Sirainen <tss@iki.fi>
parents:
4393
diff
changeset
|
101 static void dict_process_listen_input(struct dict_process *process) |
3855 | 102 { |
4393
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
103 i_assert(process->log == NULL); |
3855 | 104 dict_process_start(process); |
105 } | |
106 | |
107 static int dict_process_listen(struct dict_process *process) | |
108 { | |
109 mode_t old_umask; | |
110 int fd, i = 0; | |
111 | |
112 for (;;) { | |
113 old_umask = umask(0); | |
114 process->fd = net_listen_unix(process->path, 64); | |
115 umask(old_umask); | |
116 | |
117 if (process->fd != -1) | |
118 break; | |
119 | |
120 if (errno != EADDRINUSE || ++i == 2) { | |
121 i_error("net_listen_unix(%s) failed: %m", | |
122 process->path); | |
123 return -1; | |
124 } | |
125 | |
126 /* see if it really exists */ | |
127 fd = net_connect_unix(process->path); | |
128 if (fd != -1 || errno != ECONNREFUSED) { | |
129 if (fd != -1) (void)close(fd); | |
130 i_error("Socket already exists: %s", process->path); | |
131 return -1; | |
132 } | |
133 | |
134 /* delete and try again */ | |
135 if (unlink(process->path) < 0 && errno != ENOENT) { | |
136 i_error("unlink(%s) failed: %m", process->path); | |
137 return -1; | |
138 } | |
139 } | |
140 | |
141 fd_close_on_exec(process->fd, TRUE); | |
142 process->io = io_add(process->fd, IO_READ, | |
143 dict_process_listen_input, process); | |
144 | |
145 return process->fd != -1 ? 0 : -1; | |
146 } | |
147 | |
148 static void dict_process_unlisten(struct dict_process *process) | |
149 { | |
150 if (process->fd == -1) | |
151 return; | |
152 | |
3960
aeb424e64f24
Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents:
3892
diff
changeset
|
153 io_remove(&process->io); |
aeb424e64f24
Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents:
3892
diff
changeset
|
154 |
3855 | 155 if (close(process->fd) < 0) |
156 i_error("close(dict) failed: %m"); | |
157 process->fd = -1; | |
158 } | |
159 | |
5843
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
160 static void |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
161 dict_process_destroyed(struct child_process *process, |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
162 bool abnormal_exit __attr_unused__) |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
163 { |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
164 struct dict_process *p = (struct dict_process *)process; |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
165 |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
166 (void)dict_process_listen(p); |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
167 } |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
168 |
3855 | 169 void dict_process_init(void) |
170 { | |
171 process = i_new(struct dict_process, 1); | |
5843
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
172 process->process.type = PROCESS_TYPE_DICT; |
3855 | 173 process->fd = -1; |
3885
b624b687fe13
Create dict-server socket to base_dir.
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
174 process->path = i_strconcat(settings_root->defaults->base_dir, |
3975
9de8bce1077e
Added plugin {} section to dovecot.conf for passing extra environment
Timo Sirainen <tss@iki.fi>
parents:
3960
diff
changeset
|
175 "/"DICT_SERVER_SOCKET_NAME, NULL); |
9de8bce1077e
Added plugin {} section to dovecot.conf for passing extra environment
Timo Sirainen <tss@iki.fi>
parents:
3960
diff
changeset
|
176 (void)dict_process_listen(process); |
5843
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
177 |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
178 child_process_set_destroy_callback(PROCESS_TYPE_DICT, |
f655c4d4a419
Moved child process handling to child-process.[ch]. The hash table now uses
Timo Sirainen <tss@iki.fi>
parents:
4907
diff
changeset
|
179 dict_process_destroyed); |
3855 | 180 } |
181 | |
182 void dict_process_deinit(void) | |
183 { | |
184 dict_process_unlisten(process); | |
4393
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
185 if (process->log != NULL) |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
186 log_unref(process->log); |
3885
b624b687fe13
Create dict-server socket to base_dir.
Timo Sirainen <tss@iki.fi>
parents:
3879
diff
changeset
|
187 i_free(process->path); |
3855 | 188 i_free(process); |
189 } | |
190 | |
4393
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
191 void dict_process_kill(void) |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
192 { |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
193 if (process->log != NULL) { |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
194 log_unref(process->log); |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
195 process->log = NULL; |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
196 } |
9928ebb54719
Instead of passing URIs directly to dictionary server, it now accepts only
Timo Sirainen <tss@iki.fi>
parents:
3975
diff
changeset
|
197 } |