Re: Patch to add -l flag to cat(1)

2015-07-22 Thread Theo de Raadt
  So, what's the story with the -l option?  What change/fix in OpenBSD
  base requires it?
 
 Nothing requires it explicitly, more the question of if having the
 ability for cat to set exclusive locks on its stdout so that multiple
 calls to the same cat command will cause the the output to be arranged
 in sequence is useful.

Yes, and maybe the same option should be added to every command!




Re: Patch to add -l flag to cat(1)

2015-07-22 Thread Sevan Janiyan
Hi Philip,

On 23/07/2015 00:54, Philip Guenther wrote:
 This is the second time you've sent a patching adding a feature
 without saying *why* the feature should be added.  That's not very
 helpful.

Apologies.

 Your first patch for cat added a feature (-f option) to solve problem
 in NetBSD which had to be solved in a different way in OpenBSD.  As as
 result, we're not adding the -f option to OpenBSD.

The reason that patch was raised was not to fix issues in security(8) in
OpenBSD, I understood it as you came the realisation that you're
vulnerable to the same problem that the NetBSD folks were trying to fix
by adding -f to their cat(1).

 So, what's the story with the -l option?  What change/fix in OpenBSD
 base requires it?

Nothing requires it explicitly, more the question of if having the
ability for cat to set exclusive locks on its stdout so that multiple
calls to the same cat command will cause the the output to be arranged
in sequence is useful.

 (It's not portable, so any portable program should be doing this via a
 more powerful, portable tool, like perl, python, or C.)

Understood.


Sevan



Re: Patch to add -l flag to cat(1)

2015-07-22 Thread Philip Guenther
On Wed, Jul 22, 2015 at 4:23 PM, Sevan Janiyan ventur...@geeklan.co.uk wrote:
 Attached patch adds the -l flag to cat
 This option causes cat(1) to use fcntl(2) to set an exclusive advisory
 lock on stdout. which was used to guarantee orderly writing to file.
 Obtained from NetBSD cat.c r1.26

This is the second time you've sent a patching adding a feature
without saying *why* the feature should be added.  That's not very
helpful.

Your first patch for cat added a feature (-f option) to solve problem
in NetBSD which had to be solved in a different way in OpenBSD.  As as
result, we're not adding the -f option to OpenBSD.

So, what's the story with the -l option?  What change/fix in OpenBSD
base requires it?

(It's not portable, so any portable program should be doing this via a
more powerful, portable tool, like perl, python, or C.)


Philip Guenther



Patch to add -l flag to cat(1)

2015-07-22 Thread Sevan Janiyan
Hi,
Attached patch adds the -l flag to cat
This option causes cat(1) to use fcntl(2) to set an exclusive advisory
lock on stdout. which was used to guarantee orderly writing to file.
Obtained from NetBSD cat.c r1.26


Sevan Janiyan
From NetBSD
cat.c r1.32
cat.1 r1.26

Index: bin/cat/cat.1
===
RCS file: /cvs/src/bin/cat/cat.1,v
retrieving revision 1.34
diff -u -r1.34 cat.1
--- bin/cat/cat.1   15 Jan 2015 19:06:31 -  1.34
+++ bin/cat/cat.1   22 Jul 2015 23:17:57 -
@@ -41,7 +41,7 @@
 .Nd concatenate and print files
 .Sh SYNOPSIS
 .Nm cat
-.Op Fl benstuv
+.Op Fl belnstuv
 .Op Ar
 .Sh DESCRIPTION
 The
@@ -70,6 +70,15 @@
 option and also prints a dollar sign
 .Pq Ql \$
 at the end of each line.
+.It Fl l
+Set an exclusive advisory lock on the standard output file descriptor.
+This lock is set using
+.Xr fcntl 2
+with the
+.Dv F_SETLKW
+command. If the output file is already locked,
+.Nm
+will block until the lock is acquired.
 .It Fl n
 Number the output lines, starting at 1.
 .It Fl s
Index: bin/cat/cat.c
===
RCS file: /cvs/src/bin/cat/cat.c,v
retrieving revision 1.21
diff -u -r1.21 cat.c
--- bin/cat/cat.c   16 Jan 2015 06:39:28 -  1.21
+++ bin/cat/cat.c   22 Jul 2015 23:17:57 -
@@ -50,7 +50,7 @@
 
 extern char *__progname;
 
-int bflag, eflag, nflag, sflag, tflag, vflag;
+int bflag, eflag, lflag, nflag, sflag, tflag, vflag;
 int rval;
 char *filename;
 
@@ -63,10 +63,11 @@
 main(int argc, char *argv[])
 {
int ch;
+   struct flock stdout_lock;
 
setlocale(LC_ALL, );
 
-   while ((ch = getopt(argc, argv, benstuv)) != -1)
+   while ((ch = getopt(argc, argv, belnstuv)) != -1)
switch (ch) {
case 'b':
bflag = nflag = 1;  /* -b implies -n */
@@ -74,6 +75,9 @@
case 'e':
eflag = vflag = 1;  /* -e implies -v */
break;
+   case 'l':
+   lflag = 1;
+   break;
case 'n':
nflag = 1;
break;
@@ -91,11 +95,20 @@
break;
default:
(void)fprintf(stderr,
-   usage: %s [-benstuv] [file ...]\n, __progname);
+   usage: %s [-belnstuv] [file ...]\n, __progname);
exit(1);
/* NOTREACHED */
}
argv += optind;
+
+   if (lflag) {
+   stdout_lock.l_len = 0;
+   stdout_lock.l_start = 0;
+   stdout_lock.l_type = F_WRLCK;
+   stdout_lock.l_whence = SEEK_SET;
+   if (fcntl(STDOUT_FILENO, F_SETLKW, stdout_lock) == -1)
+   err(1, stdout);
+   }
 
if (bflag || eflag || nflag || sflag || tflag || vflag)
cook_args(argv);