View | Details | Raw Unified | Return to bug 253
Collapse All | Expand All

(-)openssh-3.2.2p1/ssh.1 (+12 lines)
Lines 51-56 Link Here
51
.Op Fl afgknqstvxACNPTX1246
51
.Op Fl afgknqstvxACNPTX1246
52
.Op Fl b Ar bind_address
52
.Op Fl b Ar bind_address
53
.Op Fl c Ar cipher_spec
53
.Op Fl c Ar cipher_spec
54
.Op Fl d Ar pidfile
54
.Op Fl e Ar escape_char
55
.Op Fl e Ar escape_char
55
.Op Fl i Ar identity_file
56
.Op Fl i Ar identity_file
56
.Op Fl l Ar login_name
57
.Op Fl l Ar login_name
Lines 421-426 Link Here
421
See
422
See
422
.Cm Ciphers
423
.Cm Ciphers
423
for more information.
424
for more information.
425
.It Fl d Ar pidfile
426
Requests
427
.Nm
428
to write a file containing the process ID before going into the background.
429
This implies
430
.Fl n 
431
and 
432
.Fl f .
433
This is useful to create tunnels to use in scripts, since they can 
434
then be killed with
435
.Ic kill `cat pidfile` .
424
.It Fl e Ar ch|^ch|none
436
.It Fl e Ar ch|^ch|none
425
Sets the escape character for sessions with a pty (default:
437
Sets the escape character for sessions with a pty (default:
426
.Ql ~ ) .
438
.Ql ~ ) .
(-)openssh-3.2.2p1/ssh.c (-5 / +57 lines)
Lines 113-118 Link Here
113
int fork_after_authentication_flag = 0;
113
int fork_after_authentication_flag = 0;
114
114
115
/*
115
/*
116
 * Flag indicating that ssh should write its pid out to a file. Useful for
117
 * fork()'d clients.
118
 */
119
120
int write_pid_flag = 0;
121
char *pidfile = NULL;
122
  
123
/*
116
 * General data structure for command line options and options configurable
124
 * General data structure for command line options and options configurable
117
 * in configuration files.  See readconf.h.
125
 * in configuration files.  See readconf.h.
118
 */
126
 */
Lines 180-185 Link Here
180
	fprintf(stderr, "  -P          Don't allocate a privileged port.\n");
188
	fprintf(stderr, "  -P          Don't allocate a privileged port.\n");
181
	fprintf(stderr, "  -q          Quiet; don't display any warning messages.\n");
189
	fprintf(stderr, "  -q          Quiet; don't display any warning messages.\n");
182
	fprintf(stderr, "  -f          Fork into background after authentication.\n");
190
	fprintf(stderr, "  -f          Fork into background after authentication.\n");
191
   	fprintf(stderr, "  -d file     Write daemon pid to file (implies -f).\n");
183
	fprintf(stderr, "  -e char     Set escape character; ``none'' = disable (default: ~).\n");
192
	fprintf(stderr, "  -e char     Set escape character; ``none'' = disable (default: ~).\n");
184
193
185
	fprintf(stderr, "  -c cipher   Select encryption algorithm\n");
194
	fprintf(stderr, "  -c cipher   Select encryption algorithm\n");
Lines 244-249 Link Here
244
static int ssh_session(void);
253
static int ssh_session(void);
245
static int ssh_session2(void);
254
static int ssh_session2(void);
246
static void load_public_identity_files(void);
255
static void load_public_identity_files(void);
256
static void write_pid(pid_t pid);
257
static pid_t go_daemon();
247
258
248
/*
259
/*
249
 * Main program for the ssh client.
260
 * Main program for the ssh client.
Lines 315-321 Link Here
315
326
316
again:
327
again:
317
	while ((opt = getopt(ac, av,
328
	while ((opt = getopt(ac, av,
318
	    "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVX")) != -1) {
329
	    "1246ab:c:d:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVX")) != -1) {
319
		switch (opt) {
330
		switch (opt) {
320
		case '1':
331
		case '1':
321
			options.protocol = SSH_PROTO_1;
332
			options.protocol = SSH_PROTO_1;
Lines 336-341 Link Here
336
			fork_after_authentication_flag = 1;
347
			fork_after_authentication_flag = 1;
337
			stdin_null_flag = 1;
348
			stdin_null_flag = 1;
338
			break;
349
			break;
350
		case 'd':
351
			write_pid_flag = 1;
352
			fork_after_authentication_flag = 1;
353
			stdin_null_flag = 1;
354
		        pidfile = optarg;
355
			break;
339
		case 'x':
356
		case 'x':
340
			options.forward_x11 = 0;
357
			options.forward_x11 = 0;
341
			break;
358
			break;
Lines 1001-1008 Link Here
1001
1018
1002
	/* If requested, let ssh continue in the background. */
1019
	/* If requested, let ssh continue in the background. */
1003
	if (fork_after_authentication_flag)
1020
	if (fork_after_authentication_flag)
1004
		if (daemon(1, 1) < 0)
1021
          go_daemon();
1005
			fatal("daemon() failed: %.200s", strerror(errno));
1006
1022
1007
	/*
1023
	/*
1008
	 * If a command was specified on the command line, execute the
1024
	 * If a command was specified on the command line, execute the
Lines 1200-1207 Link Here
1200
1216
1201
	/* If requested, let ssh continue in the background. */
1217
	/* If requested, let ssh continue in the background. */
1202
	if (fork_after_authentication_flag)
1218
	if (fork_after_authentication_flag)
1203
		if (daemon(1, 1) < 0)
1219
          go_daemon();
1204
			fatal("daemon() failed: %.200s", strerror(errno));
1205
1220
1206
	return client_loop(tty_flag, tty_flag ?
1221
	return client_loop(tty_flag, tty_flag ?
1207
	    options.escape_char : SSH_ESCAPECHAR_NONE, id);
1222
	    options.escape_char : SSH_ESCAPECHAR_NONE, id);
Lines 1246-1249 Link Here
1246
		options.identity_files[i] = filename;
1261
		options.identity_files[i] = filename;
1247
		options.identity_keys[i] = public;
1262
		options.identity_keys[i] = public;
1248
	}
1263
	}
1264
}
1265
1266
/* We use this to go into daemon mode, rather than daemon(), because
1267
 * we want the parent to write out the pid file before exiting, to
1268
 * prevent any races for the pid file in scripts, etc. */
1269
1270
static pid_t
1271
go_daemon() {
1272
  pid_t child_pid;
1273
1274
  child_pid = fork();
1275
1276
  if (child_pid == -1) {
1277
    fatal("Couldn't fork child: %.200s", strerror(errno));
1278
  } else if (child_pid == 0) {
1279
    setsid();
1280
  } else {
1281
    if (write_pid_flag) 
1282
      write_pid(child_pid);
1283
    exit(0);
1284
  }
1285
1286
  return child_pid;
1287
}
1288
1289
/* Simple function to write a pid to a file. */
1290
1291
static void
1292
write_pid(pid_t pid) {
1293
  FILE* f;
1294
  f = fopen(pidfile, "wb"); /* no particular reason for "b", but sshd.c does it. */
1295
  if (!f) {
1296
    fatal("Can't write pid file %.100s: %.200s", pidfile, strerror(errno));
1297
  } else {
1298
    fprintf(f, "%u\n", (u_int) pid);
1299
    fclose(f);
1300
  }
1249
}
1301
}

Return to bug 253