bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 1/4] sha1sum: use AF_ALG when available


From: Tim Rühsen
Subject: Re: [PATCH 1/4] sha1sum: use AF_ALG when available
Date: Mon, 23 Apr 2018 17:07:36 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0

On 04/23/2018 01:17 PM, Matteo Croce wrote:
> +#include <config.h>
> +
> +#include "af_alg.h"
> +
> +/* from linux/include/linux/fs.h: (INT_MAX & PAGE_MASK) */
> +#define MAX_RW_COUNT 0x7FFFF000
> +#define BLOCKSIZE 32768
> +
> +int
> +afalg_stream (FILE * stream, void *resblock, const char *alg, int hashlen)
> +{
> +  struct sockaddr_alg salg = {
> +    .salg_family = AF_ALG,
> +    .salg_type = "hash",
> +  };
> +  int ret, cfd, ofd;
> +  static char buf[BLOCKSIZE];
> +  ssize_t size;
> +  struct stat st;
> +
> +  strcpy((char *)salg.salg_name, alg);

Better consider for size of salg.salg_name here.

> +  cfd = socket (AF_ALG, SOCK_SEQPACKET, 0);
> +  if (cfd < 0)
> +      return -EAFNOSUPPORT;

What about moving salg initialization here ?

> +  ret = bind (cfd, (struct sockaddr *) &salg, sizeof (salg));
> +  if (ret < 0)
> +    {
> +      ret = -EAFNOSUPPORT;
> +      goto out_cfd;
> +    }
> +
> +  ofd = accept (cfd, NULL, 0);
> +  if (ofd < 0)
> +    {
> +      ret = -EAFNOSUPPORT;
> +      goto out_ofd;
> +    }
> +
> +  /* if file is a regular file, attempt sendfile() to pipe the data */
> +  if (!fstat(fileno(stream), &st) && S_ISREG(st.st_mode) &&
> +      st.st_size <= MAX_RW_COUNT)

Is it possible to skip that MAX_RW_COUNT check on recent glibc ?
From 'man sendfile':
"
The original Linux sendfile() system call was not designed to handle
large file offsets.  Consequently, Linux 2.4 added
sendfile64(), with a wider type for the offset argument.  The glibc
sendfile()  wrapper  function  transparently  deals
with the kernel differences.
"

> +    {
> +      if (sendfile(ofd, fileno(stream), NULL, st.st_size) == -1)

From 'man sendfile':
"
Note  that  a  successful  call  to
sendfile()  may  write fewer bytes than requested; the caller should be
prepared to retry the call if there were unsent
bytes.  See also NOTES.
"

> +        ret = -EIO;
> +      else
> +        ret = 0;
> +    } else {
> +      /* sendfile() not possible, do a classic read-write loop */
> +      while ((size = fread (buf, 1, sizeof (buf), stream)))
> +        {
> +          if (send (ofd, buf, size, size == sizeof (buf) ? MSG_MORE : 0) == 
> -1)
> +            {
> +              ret = -EIO;
> +              goto out_ofd;
> +            }
> +        }
> +  }


Regards, Tim

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]