Changed "remote_version" to "protocol_version".
[rsync/rsync.git] / clientserver.c
CommitLineData
a039749b 1/* -*- c-file-style: "linux"; -*-
4a7144ee 2 *
a254fd97
MP
3 * Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 2001-2002 by Martin Pool <mbp@samba.org>
4a7144ee 5 *
a254fd97
MP
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
4a7144ee 10 *
a254fd97
MP
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
4a7144ee 15 *
a254fd97
MP
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
3591c066 20
a254fd97
MP
21/**
22 * @file
23 *
24 * The socket based protocol for setting up a connection with
25 * rsyncd.
26 **/
3591c066
AT
27
28#include "rsync.h"
29
30extern int module_id;
31extern int read_only;
32extern int verbose;
33extern int rsync_port;
97cb8dc2 34char *auth_user;
b35d0d8e 35extern int sanitize_paths;
7c2a9e76 36extern int filesfrom_fd;
837cbad9
WD
37extern struct exclude_struct **server_exclude_list;
38extern char *exclude_path_prefix;
3591c066 39
be2961da 40/**
bc363ea9 41 * Run a client connected to an rsyncd. The alternative to this
be2961da
MP
42 * function for remote-shell connections is do_cmd().
43 *
fdf88d75
MP
44 * After negotiating which module to use and reading the server's
45 * motd, this hands over to client_run(). Telling the server the
46 * module will cause it to chroot/setuid/etc.
47 *
48 * Instead of doing a transfer, the client may at this stage instead
49 * get a listing of remote modules and exit.
be2961da
MP
50 *
51 * @return -1 for error in startup, or the result of client_run().
d0829892 52 * Either way, it eventually gets passed to exit_cleanup().
be2961da 53 **/
3591c066
AT
54int start_socket_client(char *host, char *path, int argc, char *argv[])
55{
68f40ebb 56 int fd, ret;
bcb7e502 57 char *p, *user=NULL;
06963d0f 58 extern char *bind_address;
13e29995 59 extern int default_af_hint;
4a7144ee 60
68f40ebb 61 /* this is redundant with code in start_inband_exchange(), but
4a7144ee
WD
62 * this short-circuits a problem before we open a socket, and
63 * the extra check won't hurt */
f98df1d9 64 if (*path == '/') {
ff81e809 65 rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n");
f98df1d9
AT
66 return -1;
67 }
68
bcb7e502
AT
69 p = strchr(host, '@');
70 if (p) {
71 user = host;
72 host = p+1;
73 *p = 0;
74 }
75
1bbd10fe 76 if (verbose >= 2) {
604f343c
MP
77 /* FIXME: If we're going to use a socket program for
78 * testing, then this message is wrong. We need to
79 * say something like "(except really using %s)" */
1264288c
MP
80 rprintf(FINFO, "opening tcp connection to %s port %d\n",
81 host, rsync_port);
1bbd10fe 82 }
837cbad9
WD
83 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
84 default_af_hint);
3591c066 85 if (fd == -1) {
65417579 86 exit_cleanup(RERR_SOCKETIO);
3591c066 87 }
68f40ebb
WD
88
89 ret = start_inband_exchange(user, path, fd, fd, argc);
90
91 return ret < 0? ret : client_run(fd, fd, -1, argc, argv);
92}
93
94int start_inband_exchange(char *user, char *path, int f_in, int f_out, int argc)
95{
96 int i;
97 char *sargs[MAX_ARGS];
98 int sargc = 0;
99 char line[MAXPATHLEN];
100 char *p;
101 extern int remote_version;
102 extern int kludge_around_eof;
103 extern int am_sender;
104 extern int daemon_over_rsh;
105 extern int list_only;
106
107 if (argc == 0 && !am_sender)
108 list_only = 1;
109
110 if (*path == '/') {
111 rprintf(FERROR, "ERROR: The remote path must start with a module name\n");
112 return -1;
113 }
114
115 if (!user) user = getenv("USER");
116 if (!user) user = getenv("LOGNAME");
117
4a7144ee
WD
118 /* set daemon_over_rsh to false since we need to build the
119 * true set of args passed through the rsh/ssh connection;
120 * this is a no-op for direct-socket-connection mode */
68f40ebb
WD
121 daemon_over_rsh = 0;
122 server_options(sargs, &sargc);
3591c066
AT
123
124 sargs[sargc++] = ".";
125
4a7144ee 126 if (path && *path)
3591c066
AT
127 sargs[sargc++] = path;
128
129 sargs[sargc] = NULL;
130
68f40ebb 131 io_printf(f_out, "@RSYNCD: %d\n", PROTOCOL_VERSION);
91eee594 132
68f40ebb 133 if (!read_line(f_in, line, sizeof(line)-1)) {
be2961da 134 rprintf(FERROR, "rsync: did not see server greeting\n");
3591c066
AT
135 return -1;
136 }
137
13c5fc0e 138 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
be2961da
MP
139 /* note that read_line strips of \n or \r */
140 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
141 line);
3591c066
AT
142 return -1;
143 }
144
2c91d3d3
AT
145 p = strchr(path,'/');
146 if (p) *p = 0;
68f40ebb 147 io_printf(f_out, "%s\n", path);
2c91d3d3
AT
148 if (p) *p = '/';
149
063393d6
MP
150 /* Old servers may just drop the connection here,
151 rather than sending a proper EXIT command. Yuck. */
9098bbf3 152 kludge_around_eof = list_only && (remote_version < 25);
7a55d06e 153
063393d6 154 while (1) {
68f40ebb 155 if (!read_line(f_in, line, sizeof(line)-1)) {
be2961da 156 rprintf(FERROR, "rsync: didn't get server startup line\n");
3591c066
AT
157 return -1;
158 }
31593dd6 159
31593dd6 160 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
68f40ebb 161 auth_client(f_out, user, line+18);
31593dd6
AT
162 continue;
163 }
31593dd6 164
3591c066 165 if (strcmp(line,"@RSYNCD: OK") == 0) break;
8f04bb36 166
cae95647
MP
167 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
168 /* This is sent by recent versions of the
169 * server to terminate the listing of modules.
170 * We don't want to go on and transfer
171 * anything; just exit. */
172 exit(0);
173 }
8f04bb36 174
136ac7ec 175 if (strncmp(line, "@ERROR", 6) == 0) {
c613d370 176 rprintf(FERROR,"%s\n", line);
136ac7ec
MP
177 /* This is always fatal; the server will now
178 * close the socket. */
179 return RERR_STARTCLIENT;
180 } else {
c613d370 181 rprintf(FINFO,"%s\n", line);
136ac7ec 182 }
3591c066 183 }
7a55d06e 184 kludge_around_eof = False;
3591c066 185
68f40ebb
WD
186 for (i = 0; i < sargc; i++) {
187 io_printf(f_out, "%s\n", sargs[i]);
3591c066 188 }
68f40ebb 189 io_printf(f_out, "\n");
3591c066 190
09b7f5db
AT
191 if (remote_version < 23) {
192 if (remote_version == 22 || (remote_version > 17 && !am_sender))
68f40ebb 193 io_start_multiplex_in(f_in);
09b7f5db 194 }
8d9dc9f9 195
68f40ebb 196 return 0;
3591c066
AT
197}
198
199
200
68f40ebb 201static int rsync_module(int f_in, int f_out, int i)
3591c066
AT
202{
203 int argc=0;
204 char *argv[MAX_ARGS];
205 char **argp;
e42c9458 206 char line[MAXPATHLEN];
2af27ad9 207 uid_t uid = (uid_t)-2; /* canonically "nobody" */
1a016bfd 208 gid_t gid = (gid_t)-2;
8ef4ffd6 209 char *p;
09021eab
DD
210 char *addr = client_addr(f_in);
211 char *host = client_name(f_in);
874895d5 212 char *name = lp_name(i);
8638dd48 213 int use_chroot = lp_use_chroot(i);
874895d5 214 int start_glob=0;
41979ff8 215 int ret;
7b372642
AT
216 char *request=NULL;
217 extern int am_sender;
68f40ebb
WD
218 extern int am_server;
219 extern int am_daemon;
8d9dc9f9 220 extern int remote_version;
943882a2 221 extern int am_root;
56c473b7
AT
222
223 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
224 rprintf(FERROR,"rsync denied on module %s from %s (%s)\n",
31ec50d7 225 name, host, addr);
68f40ebb 226 io_printf(f_out, "@ERROR: access denied to %s from %s (%s)\n",
31ec50d7 227 name, host, addr);
56c473b7
AT
228 return -1;
229 }
3591c066 230
68f40ebb
WD
231 if (am_daemon && am_server) {
232 rprintf(FINFO, "rsync allowed access on module %s from %s (%s)\n",
233 name, host, addr);
234 }
235
5e71c444 236 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
8d72ef6e
AT
237 if (errno) {
238 rprintf(FERROR,"failed to open lock file %s : %s\n",
5e71c444 239 lp_lock_file(i), strerror(errno));
68f40ebb 240 io_printf(f_out, "@ERROR: failed to open lock file %s : %s\n",
5e71c444 241 lp_lock_file(i), strerror(errno));
8d72ef6e
AT
242 } else {
243 rprintf(FERROR,"max connections (%d) reached\n",
5e71c444 244 lp_max_connections(i));
837cbad9
WD
245 io_printf(f_out, "@ERROR: max connections (%d) reached - try again later\n",
246 lp_max_connections(i));
8d72ef6e 247 }
0c515f17
AT
248 return -1;
249 }
250
4a7144ee 251
68f40ebb 252 auth_user = auth_server(f_in, f_out, i, addr, "@RSYNCD: AUTHREQD ");
d0d56395 253
97cb8dc2 254 if (!auth_user) {
d0d56395 255 rprintf(FERROR,"auth failed on module %s from %s (%s)\n",
68f40ebb
WD
256 name, host, addr);
257 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
4a7144ee 258 return -1;
d0d56395
AT
259 }
260
3591c066
AT
261 module_id = i;
262
716baed7 263 am_root = (getuid() == 0);
8ef4ffd6 264
716baed7
DD
265 if (am_root) {
266 p = lp_uid(i);
267 if (!name_to_uid(p, &uid)) {
32f76175 268 if (!isdigit(* (unsigned char *) p)) {
716baed7 269 rprintf(FERROR,"Invalid uid %s\n", p);
68f40ebb 270 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
716baed7 271 return -1;
4a7144ee 272 }
716baed7
DD
273 uid = atoi(p);
274 }
275
276 p = lp_gid(i);
277 if (!name_to_gid(p, &gid)) {
32f76175 278 if (!isdigit(* (unsigned char *) p)) {
716baed7 279 rprintf(FERROR,"Invalid gid %s\n", p);
68f40ebb 280 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
716baed7 281 return -1;
4a7144ee 282 }
716baed7
DD
283 gid = atoi(p);
284 }
8ef4ffd6 285 }
3b2b5345 286
4a7144ee
WD
287 /* TODO: If we're not root, but the configuration requests
288 * that we change to some uid other than the current one, then
289 * log a warning. */
290
291 /* TODO: Perhaps take a list of gids, and make them into the
292 * supplementary groups. */
8ef4ffd6 293
837cbad9
WD
294 exclude_path_prefix = use_chroot? "" : lp_path(i);
295
cd64343a 296 p = lp_include_from(i);
837cbad9 297 add_exclude_file(&server_exclude_list, p, MISSING_FATAL, ADD_INCLUDE);
cd64343a
DD
298
299 p = lp_include(i);
837cbad9 300 add_exclude_line(&server_exclude_list, p, ADD_INCLUDE);
cd64343a 301
8f3a2d54 302 p = lp_exclude_from(i);
837cbad9 303 add_exclude_file(&server_exclude_list, p, MISSING_FATAL, ADD_EXCLUDE);
8f3a2d54 304
5805327b 305 p = lp_exclude(i);
837cbad9
WD
306 add_exclude_line(&server_exclude_list, p, ADD_EXCLUDE);
307
308 exclude_path_prefix = NULL;
8f3a2d54 309
45a83540 310 log_init();
1a016bfd 311
8638dd48 312 if (use_chroot) {
b52c1d9d
MP
313 /*
314 * XXX: The 'use chroot' flag is a fairly reliable
315 * source of confusion, because it fails under two
316 * important circumstances: running as non-root,
317 * running on Win32 (or possibly others). On the
318 * other hand, if you are running as root, then it
319 * might be better to always use chroot.
320 *
321 * So, perhaps if we can't chroot we should just issue
322 * a warning, unless a "require chroot" flag is set,
323 * in which case we fail.
324 */
8638dd48 325 if (chroot(lp_path(i))) {
a039749b 326 rsyserr(FERROR, errno, "chroot %s failed", lp_path(i));
68f40ebb 327 io_printf(f_out, "@ERROR: chroot failed\n");
8638dd48
DD
328 return -1;
329 }
3591c066 330
c226b7c2 331 if (!push_dir("/", 0)) {
4a7144ee 332 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
68f40ebb 333 io_printf(f_out, "@ERROR: chdir failed\n");
8638dd48
DD
334 return -1;
335 }
3591c066 336
716baed7
DD
337 } else {
338 if (!push_dir(lp_path(i), 0)) {
a039749b 339 rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i));
68f40ebb 340 io_printf(f_out, "@ERROR: chdir failed\n");
716baed7
DD
341 return -1;
342 }
cb13abfe 343 sanitize_paths = 1;
716baed7
DD
344 }
345
346 if (am_root) {
715d1f45
MP
347 /* XXXX: You could argue that if the daemon is started
348 * by a non-root user and they explicitly specify a
349 * gid, then we should try to change to that gid --
350 * this could be possible if it's already in their
351 * supplementary groups. */
352
353 /* TODO: Perhaps we need to document that if rsyncd is
354 * started by somebody other than root it will inherit
355 * all their supplementary groups. */
356
716baed7 357 if (setgid(gid)) {
08a740ff 358 rsyserr(FERROR, errno, "setgid %d failed", (int) gid);
68f40ebb 359 io_printf(f_out, "@ERROR: setgid failed\n");
8638dd48
DD
360 return -1;
361 }
6969ebcf
WD
362#ifdef HAVE_SETGROUPS
363 /* Get rid of any supplementary groups this process
364 * might have inheristed. */
365 if (setgroups(1, &gid)) {
366 rsyserr(FERROR, errno, "setgroups failed");
367 io_printf(f_out, "@ERROR: setgroups failed\n");
368 return -1;
369 }
370#endif
3591c066 371
716baed7 372 if (setuid(uid)) {
08a740ff 373 rsyserr(FERROR, errno, "setuid %d failed", (int) uid);
68f40ebb 374 io_printf(f_out, "@ERROR: setuid failed\n");
8638dd48
DD
375 return -1;
376 }
377
716baed7 378 am_root = (getuid() == 0);
3591c066
AT
379 }
380
68f40ebb 381 io_printf(f_out, "@RSYNCD: OK\n");
3591c066
AT
382
383 argv[argc++] = "rsyncd";
384
385 while (1) {
68f40ebb 386 if (!read_line(f_in, line, sizeof(line)-1)) {
3591c066
AT
387 return -1;
388 }
389
390 if (!*line) break;
391
874895d5
AT
392 p = line;
393
874895d5 394 argv[argc] = strdup(p);
3591c066
AT
395 if (!argv[argc]) {
396 return -1;
397 }
398
874895d5 399 if (start_glob) {
1a016bfd 400 if (start_glob == 1) {
7b372642 401 request = strdup(p);
1a016bfd
AT
402 start_glob++;
403 }
cb13abfe 404 glob_expand(name, argv, &argc, MAX_ARGS);
874895d5
AT
405 } else {
406 argc++;
407 }
408
409 if (strcmp(line,".") == 0) {
410 start_glob = 1;
411 }
412
3591c066
AT
413 if (argc == MAX_ARGS) {
414 return -1;
415 }
416 }
417
cb13abfe 418 if (sanitize_paths) {
8638dd48
DD
419 /*
420 * Note that this is applied to all parameters, whether or not
421 * they are filenames, but no other legal parameters contain
422 * the forms that need to be sanitized so it doesn't hurt;
423 * it is not known at this point which parameters are files
424 * and which aren't.
425 */
426 for (i = 1; i < argc; i++) {
cb13abfe 427 sanitize_path(argv[i], NULL);
8638dd48
DD
428 }
429 }
430
4a7144ee 431 argp = argv;
15b7b73d 432 ret = parse_arguments(&argc, (const char ***) &argp, 0);
3591c066 433
7c2a9e76
WD
434 if (filesfrom_fd == 0)
435 filesfrom_fd = f_in;
436
7b372642 437 if (request) {
97cb8dc2 438 if (*auth_user) {
d0d56395
AT
439 rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n",
440 am_sender?"on":"to",
97cb8dc2 441 request, auth_user, host, addr);
d0d56395
AT
442 } else {
443 rprintf(FINFO,"rsync %s %s from %s (%s)\n",
444 am_sender?"on":"to",
445 request, host, addr);
446 }
7b372642
AT
447 free(request);
448 }
449
15b7b73d 450#ifndef DEBUG
3591c066
AT
451 /* don't allow the logs to be flooded too fast */
452 if (verbose > 1) verbose = 1;
0199b05f 453#endif
3591c066 454
09b7f5db
AT
455 if (remote_version < 23) {
456 if (remote_version == 22 || (remote_version > 17 && am_sender))
68f40ebb 457 io_start_multiplex_out(f_out);
554e0a8d 458 }
15b7b73d 459
4a7144ee
WD
460 /* For later protocol versions, we don't start multiplexing
461 * until we've configured nonblocking in start_server. That
462 * means we're in a sticky situation now: there's no way to
463 * convey errors to the client. */
464
465 /* FIXME: Hold off on reporting option processing errors until
466 * we've set up nonblocking and multiplexed IO and can get the
467 * message back to them. */
41979ff8 468 if (!ret) {
4a7144ee
WD
469 option_error();
470 exit_cleanup(RERR_UNSUPPORTED);
41979ff8
AT
471 }
472
81791cfc
AT
473 if (lp_timeout(i)) {
474 extern int io_timeout;
475 io_timeout = lp_timeout(i);
476 }
477
68f40ebb 478 start_server(f_in, f_out, argc, argp);
3591c066
AT
479
480 return 0;
481}
482
7a6421fa
AT
483/* send a list of available modules to the client. Don't list those
484 with "list = False". */
3591c066
AT
485static void send_listing(int fd)
486{
487 int n = lp_numservices();
488 int i;
063393d6
MP
489 extern int remote_version;
490
3591c066
AT
491 for (i=0;i<n;i++)
492 if (lp_list(i))
4a7144ee 493 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
8f04bb36 494
063393d6
MP
495 if (remote_version >= 25)
496 io_printf(fd,"@RSYNCD: EXIT\n");
3591c066
AT
497}
498
68f40ebb 499/* this is called when a connection is established to a client
3591c066
AT
500 and we want to start talking. The setup of the system is done from
501 here */
68f40ebb 502int start_daemon(int f_in, int f_out)
3591c066 503{
7a6421fa 504 char line[200];
3591c066 505 char *motd;
8ef4ffd6 506 int i = -1;
4cdf25e4 507 extern char *config_file;
13c5fc0e 508 extern int remote_version;
68f40ebb 509 extern int am_server;
4cdf25e4 510
f9e940ef 511 if (!lp_load(config_file, 0)) {
65417579 512 exit_cleanup(RERR_SYNTAX);
4cdf25e4 513 }
3591c066 514
30e8c8e1
DD
515 log_init();
516
68f40ebb
WD
517 if (!am_server) {
518 set_socket_options(f_in, "SO_KEEPALIVE");
519 set_socket_options(f_in, lp_socket_options());
520 set_nonblocking(f_in);
521 }
3591c066 522
68f40ebb 523 io_printf(f_out, "@RSYNCD: %d\n", PROTOCOL_VERSION);
3591c066
AT
524
525 motd = lp_motd_file();
27d3cdbc 526 if (motd && *motd) {
0090cbdb 527 FILE *f = fopen(motd,"r");
3591c066
AT
528 while (f && !feof(f)) {
529 int len = fread(line, 1, sizeof(line)-1, f);
530 if (len > 0) {
531 line[len] = 0;
68f40ebb 532 io_printf(f_out, "%s", line);
3591c066
AT
533 }
534 }
535 if (f) fclose(f);
68f40ebb 536 io_printf(f_out, "\n");
3591c066
AT
537 }
538
68f40ebb 539 if (!read_line(f_in, line, sizeof(line)-1)) {
91eee594
AT
540 return -1;
541 }
542
543 if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) {
68f40ebb 544 io_printf(f_out, "@ERROR: protocol startup error\n");
91eee594 545 return -1;
4a7144ee 546 }
91eee594 547
8ef4ffd6 548 while (i == -1) {
3591c066 549 line[0] = 0;
68f40ebb 550 if (!read_line(f_in, line, sizeof(line)-1)) {
3591c066
AT
551 return -1;
552 }
553
554 if (!*line || strcmp(line,"#list")==0) {
68f40ebb 555 send_listing(f_out);
3591c066 556 return -1;
4a7144ee 557 }
3591c066
AT
558
559 if (*line == '#') {
560 /* it's some sort of command that I don't understand */
68f40ebb 561 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
3591c066
AT
562 return -1;
563 }
564
565 i = lp_number(line);
566 if (i == -1) {
68f40ebb 567 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
3591c066
AT
568 return -1;
569 }
3591c066
AT
570 }
571
68f40ebb 572 return rsync_module(f_in, f_out, i);
3591c066
AT
573}
574
575
576int daemon_main(void)
577{
f9e940ef 578 extern char *config_file;
ad517ce5 579 extern int orig_umask;
8638dd48 580 char *pid_file;
13e29995 581 extern int no_detach;
1a016bfd 582
3591c066 583 if (is_a_socket(STDIN_FILENO)) {
41979ff8
AT
584 int i;
585
586 /* we are running via inetd - close off stdout and
4a7144ee
WD
587 * stderr so that library functions (and getopt) don't
588 * try to use them. Redirect them to /dev/null */
41979ff8 589 for (i=1;i<3;i++) {
4a7144ee 590 close(i);
41979ff8
AT
591 open("/dev/null", O_RDWR);
592 }
3eb38818 593
68f40ebb 594 return start_daemon(STDIN_FILENO, STDIN_FILENO);
3591c066
AT
595 }
596
13e29995 597 if (!no_detach)
4a7144ee 598 become_daemon();
3591c066 599
f9e940ef 600 if (!lp_load(config_file, 1)) {
65417579 601 exit_cleanup(RERR_SYNTAX);
f9e940ef
AT
602 }
603
45a83540 604 log_init();
f9e940ef 605
a358449a 606 rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n",
4a7144ee
WD
607 RSYNC_VERSION, rsync_port);
608 /* TODO: If listening on a particular address, then show that
609 * address too. In fact, why not just do inet_ntop on the
610 * local address??? */
e42c9458 611
8638dd48 612 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
ad517ce5
DD
613 char pidbuf[16];
614 int fd;
8638dd48
DD
615 int pid = (int) getpid();
616 cleanup_set_pid(pid);
ad517ce5
DD
617 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
618 0666 & ~orig_umask)) == -1) {
4a7144ee
WD
619 cleanup_set_pid(0);
620 rsyserr(FLOG, errno, "failed to create pid file %s", pid_file);
621 exit_cleanup(RERR_FILEIO);
8638dd48 622 }
8950ac03 623 snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid);
ad517ce5
DD
624 write(fd, pidbuf, strlen(pidbuf));
625 close(fd);
8638dd48
DD
626 }
627
8ef4ffd6
AT
628 start_accept_loop(rsync_port, start_daemon);
629 return -1;
3591c066 630}