public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* pread() from /dev/sda fails with: Illegal seek
@ 2020-01-11 14:10 David Balažic
  2020-01-11 20:24 ` Enrique Perez-Terron
  2020-01-12 16:59 ` Christian Franke
  0 siblings, 2 replies; 3+ messages in thread
From: David Balažic @ 2020-01-11 14:10 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 646 bytes --]

Hi!

I'm porting an app from Linux to Cygwin and stumbled on this problem:

pread on /dev/sda fails with Illegal seek.

The simplified code is:

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  unsigned int sector_size = 512;
  void *buf = calloc(sector_size,1);
  int fd = open("/dev/sda", O_DIRECT | O_RDONLY);
  if (-1 == pread(fd, buf, sector_size, 0)) {
    perror("pread failed");
  }
}

# g++ test.cc
# ./a  # run as adnministrator
pread failed: Illegal seek

Why is that?

cygcheck -s output attached.

Summary: cygwin 3.1.2-1  , Windows 8.1 , all 64 bit

Regards,
David

[-- Attachment #2: cygcheck-s --]
[-- Type: application/octet-stream, Size: 12161 bytes --]


Cygwin Configuration Diagnostics
Current System Time: Sat Jan 11 14:07:55 2020

Windows 8.1 Professional Ver 6.3 Build 9600 

Path:	C:\cygwin64\usr\local\bin
	C:\cygwin64\bin
	C:\Windows\system32
	C:\Windows
	C:\Windows\System32\Wbem
	C:\Windows\System32\WindowsPowerShell\v1.0
	C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common

Output from C:\cygwin64\bin\id.exe
UID: 197609(stein)             GID: 197121(None)
197121(None)                   197610(HomeUsers)
545(Users)                     4(INTERACTIVE)
66049(CONSOLE LOGON)           11(Authenticated Users)
15(This Organization)          113(Local account)
4095(CurrentSession)           66048(LOCAL)
262154(NTLM Authentication)    401408(Medium Mandatory Level)

SysDir: C:\Windows\system32
WinDir: C:\Windows

USER = 'stein'
PWD = '/home/stein/hdubench'
HOME = '/home/stein'

Use '-r' to scan registry

obcaseinsensitive set to 1

Cygwin installations found in the registry:
  System: Key: e022582115c10879 Path: C:\cygwin64
  System: Key: 9a234e4d83618c4a Path: C:\Users\stein\Documents\x\x\x64_win (ORPHANED)
  User:   Key: e022582115c10879 Path: C:\cygwin64
  User:   Key: 6435883e1e7fe6b2 Path: C:\Program Files (x86)\Exact Audio Copy (ORPHANED)
  User:   Key: d67f57a3330a6cd6 Path: C:\Users\stein\Documents\x\x\john-1.9.0-jumbo-1-win64 (ORPHANED)
  User:   Key: 9a234e4d83618c4a Path: C:\Users\stein\Documents\x\x\x64_win (ORPHANED)

c:  hd  NTFS    323339Mb  66% CP CS UN PA FC    QU  
d:  hd  NTFS    953865Mb  80% CP CS UN PA FC    QU  Data1
e:  hd  NTFS    629999Mb 100% CP CS UN PA FC    QU  za backup fill
h:  cd             N/A    N/A                       
i:  fd             N/A    N/A                       
j:  fd             N/A    N/A                       
k:  fd             N/A    N/A                       

C:\cygwin64      /          system  binary,auto
C:\cygwin64\bin  /usr/bin   system  binary,auto
C:\cygwin64\lib  /usr/lib   system  binary,auto
cygdrive prefix  /cygdrive  user    binary,posix=0,auto

Found: C:\cygwin64\bin\awk
 -> C:\cygwin64\bin\gawk.exe
Found: C:\cygwin64\bin\bash.exe
Found: C:\cygwin64\bin\cat.exe
Found: C:\Windows\system32\certutil.exe
Not Found: clinfo
Found: C:\Windows\system32\comp.exe
Found: C:\Windows\system32\convert.exe
Found: C:\cygwin64\bin\cp.exe
Found: C:\cygwin64\bin\cpp.exe
Not Found: crontab
Not Found: curl
Found: C:\cygwin64\bin\expand.exe
Found: C:\Windows\system32\expand.exe
Found: C:\cygwin64\bin\find.exe
Found: C:\Windows\system32\find.exe
Found: C:\Windows\system32\ftp.exe
Found: C:\cygwin64\bin\gcc.exe
Not Found: gdb
Found: C:\cygwin64\bin\grep.exe
Found: C:\cygwin64\bin\hostname.exe
Found: C:\Windows\system32\hostname.exe
Found: C:\cygwin64\bin\kill.exe
Found: C:\Windows\system32\klist.exe
Found: C:\cygwin64\bin\ld.exe
Found: C:\cygwin64\bin\ls.exe
Found: C:\cygwin64\bin\make.exe
Found: C:\cygwin64\bin\mv.exe
Found: C:\Windows\system32\nslookup.exe
Not Found: patch
Not Found: perl
Found: C:\Windows\system32\replace.exe
Found: C:\cygwin64\bin\rm.exe
Found: C:\cygwin64\bin\sed.exe
Found: C:\cygwin64\bin\sh.exe
Found: C:\Windows\system32\shutdown.exe
Found: C:\cygwin64\bin\sort.exe
Found: C:\Windows\system32\sort.exe
Not Found: ssh
Found: C:\cygwin64\bin\tar.exe
Found: C:\cygwin64\bin\test.exe
Found: C:\cygwin64\bin\timeout.exe
Found: C:\Windows\system32\timeout.exe
Found: C:\cygwin64\bin\vi.exe
Not Found: vim
Found: C:\cygwin64\bin\whoami.exe
Found: C:\Windows\system32\whoami.exe

   39k 2016/09/19 C:\cygwin64\bin\cygargp-0.dll
   29k 2018/12/09 C:\cygwin64\bin\cygatomic-1.dll
   19k 2018/12/20 C:\cygwin64\bin\cygattr-1.dll
  246k 2019/03/05 C:\cygwin64\bin\cygblkid-1.dll
   66k 2019/07/21 C:\cygwin64\bin\cygbz2-1.dll
   39k 2017/09/03 C:\cygwin64\bin\cygcrypt-0.dll
 2363k 2019/09/11 C:\cygwin64\bin\cygcrypto-1.0.0.dll
 2416k 2019/09/13 C:\cygwin64\bin\cygcrypto-1.1.dll
  347k 2019/03/05 C:\cygwin64\bin\cygfdisk-1.dll
   27k 2015/11/17 C:\cygwin64\bin\cygffi-6.dll
   56k 2019/07/28 C:\cygwin64\bin\cygformw-10.dll
  137k 2019/03/29 C:\cygwin64\bin\cyggc-1.dll
  119k 2018/01/16 C:\cygwin64\bin\cyggc-2.dll
   70k 2018/12/09 C:\cygwin64\bin\cyggcc_s-seh-1.dll
   46k 2019/07/10 C:\cygwin64\bin\cyggdbm-4.dll
  527k 2017/01/21 C:\cygwin64\bin\cyggmp-10.dll
 1472k 2019/07/29 C:\cygwin64\bin\cyggnutls-30.dll
  148k 2018/12/09 C:\cygwin64\bin\cyggomp-1.dll
  732k 2017/04/01 C:\cygwin64\bin\cygguile-17.dll
 1333k 2018/08/07 C:\cygwin64\bin\cygguile-2.0-22.dll
   33k 2017/02/13 C:\cygwin64\bin\cyghistory7.dll
  168k 2019/02/19 C:\cygwin64\bin\cyghogweed-4.dll
 1009k 2015/02/20 C:\cygwin64\bin\cygiconv-2.dll
  114k 2019/07/29 C:\cygwin64\bin\cygidn2-0.dll
   42k 2016/10/23 C:\cygwin64\bin\cygintl-8.dll
 1039k 2015/09/20 C:\cygwin64\bin\cygisl-13.dll
 1304k 2017/11/04 C:\cygwin64\bin\cygisl-15.dll
   35k 2019/07/28 C:\cygwin64\bin\cygltdl-7.dll
  144k 2019/07/21 C:\cygwin64\bin\cyglzma-5.dll
  129k 2017/10/05 C:\cygwin64\bin\cyglzo2-2.dll
  125k 2018/03/18 C:\cygwin64\bin\cygmagic-1.dll
  168k 2018/01/23 C:\cygwin64\bin\cygman-2-7-6-1.dll
   22k 2018/01/23 C:\cygwin64\bin\cygmandb-2-7-6-1.dll
   28k 2019/07/28 C:\cygwin64\bin\cygmenuw-10.dll
   92k 2018/03/17 C:\cygwin64\bin\cygmpc-3.dll
  346k 2017/11/04 C:\cygwin64\bin\cygmpfr-4.dll
  432k 2019/02/02 C:\cygwin64\bin\cygmpfr-6.dll
  301k 2019/07/28 C:\cygwin64\bin\cygncursesw-10.dll
  205k 2019/02/19 C:\cygwin64\bin\cygnettle-6.dll
 1013k 2019/02/20 C:\cygwin64\bin\cygp11-kit-0.dll
   14k 2019/07/28 C:\cygwin64\bin\cygpanelw-10.dll
  467k 2019/04/05 C:\cygwin64\bin\cygpcre-1.dll
   41k 2019/09/12 C:\cygwin64\bin\cygpipeline-1.dll
   41k 2016/07/13 C:\cygwin64\bin\cygpopt-0.dll
   65k 2019/07/21 C:\cygwin64\bin\cygpsl-5.dll
  315k 2018/12/09 C:\cygwin64\bin\cygquadmath-0.dll
  219k 2017/02/13 C:\cygwin64\bin\cygreadline7.dll
   11k 2015/07/17 C:\cygwin64\bin\cygsigsegv-2.dll
  150k 2019/03/05 C:\cygwin64\bin\cygsmartcols-1.dll
  408k 2019/09/11 C:\cygwin64\bin\cygssl-1.0.0.dll
  495k 2019/09/13 C:\cygwin64\bin\cygssl-1.1.dll
   12k 2017/11/13 C:\cygwin64\bin\cygssp-0.dll
 1400k 2018/12/09 C:\cygwin64\bin\cygstdc++-6.dll
   68k 2019/07/29 C:\cygwin64\bin\cygtasn1-6.dll
   57k 2019/07/28 C:\cygwin64\bin\cygticw-10.dll
 1612k 2018/08/16 C:\cygwin64\bin\cygunistring-2.dll
   27k 2019/03/05 C:\cygwin64\bin\cyguuid-1.dll
    8k 2016/06/13 C:\cygwin64\bin\cygvtv-0.dll
    8k 2016/06/13 C:\cygwin64\bin\cygvtv_stubs-0.dll
   83k 2017/03/03 C:\cygwin64\bin\cygz.dll
 3508k 2019/12/21 C:\cygwin64\bin\cygwin1.dll
    Cygwin DLL version info:
        DLL version: 3.1.2
        DLL epoch: 19
        DLL old termios: 5
        DLL malloc env: 28
        Cygwin conv: 181
        API major: 0
        API minor: 340
        Shared data: 5
        DLL identifier: cygwin1
        Mount registry: 3
        Cygwin registry name: Cygwin
        Installations name: Installations
        Cygdrive default prefix: 
        Build date: 
        Shared id: cygwin1S5


Can't find the cygrunsrv utility, skipping services check.


Cygwin Package Information
Package                  Version            Status
_autorebase              001007-1           OK
alternatives             1.3.30c-10         OK
base-cygwin              3.8-1              OK
base-files               4.3-2              OK
bash                     4.4.12-3           OK
binutils                 2.29-1             OK
bzip2                    1.0.8-1            OK
ca-certificates          2.32-1             OK
coreutils                8.26-2             OK
crypto-policies          20190218-1         OK
cygutils                 1.4.16-2           OK
cygwin                   3.1.2-1            OK
cygwin-devel             3.1.2-1            OK
dash                     0.5.9.1-1          OK
diffutils                3.5-2              OK
editrights               1.03-1             OK
file                     5.32-1             OK
findutils                4.6.0-1            OK
gawk                     5.0.1-1            OK
gcc-core                 7.4.0-1            OK
gcc-g++                  7.4.0-1            OK
getent                   2.18.90-4          OK
grep                     3.0-2              OK
groff                    1.22.4-1           OK
gzip                     1.8-1              OK
hostname                 3.13-1             OK
info                     6.7-1              OK
ipc-utils                1.0-2              OK
less                     551-1              OK
libargp                  20110921-3         OK
libatomic1               7.4.0-1            OK
libattr1                 2.4.48-2           OK
libblkid1                2.33.1-1           OK
libbz2_1                 1.0.8-1            OK
libcrypt0                2.1-1              OK
libfdisk1                2.33.1-1           OK
libffi6                  3.2.1-2            OK
libgc1                   8.0.4-1            OK
libgc2                   7.6.2-3            OK
libgcc1                  7.4.0-1            OK
libgdbm4                 1.13-1             OK
libgmp10                 6.1.2-1            OK
libgnutls30              3.6.9-1            OK
libgomp1                 7.4.0-1            OK
libguile17               1.8.8-3            OK
libguile2.0_22           2.0.14-3           OK
libhogweed4              3.4.1-1            OK
libiconv                 1.14-3             OK
libiconv2                1.14-3             OK
libidn2_0                2.2.0-1            OK
libintl8                 0.19.8.1-2         OK
libisl13                 0.14.1-1           OK
libisl15                 0.16.1-1           OK
libltdl7                 2.4.6-7            OK
liblzma5                 5.2.4-1            OK
liblzo2_2                2.10-1             OK
libmpc3                  1.1.0-1            OK
libmpfr4                 3.1.6-1p1          OK
libmpfr6                 4.0.2-1            OK
libncursesw10            6.1-1.20190727     OK
libnettle6               3.4.1-1            OK
libp11-kit0              0.23.15-1          OK
libpcre1                 8.43-1             OK
libpipeline1             1.5.1-1            OK
libpopt-common           1.16-2             OK
libpopt0                 1.16-2             OK
libpsl5                  0.21.0-1           OK
libquadmath0             7.4.0-1            OK
libreadline7             7.0.3-3            OK
libsigsegv2              2.10-2             OK
libsmartcols1            2.33.1-1           OK
libssl1.0                1.0.2t-1           OK
libssl1.1                1.1.1d-1           OK
libssp0                  6.4.0-4            OK
libstdc++6               7.4.0-1            OK
libtasn1_6               4.14-1             OK
libunistring2            0.9.10-1           OK
libuuid1                 2.33.1-1           OK
libvtv0                  5.4.0-1            OK
login                    1.13-1             OK
lrzip                    0.631-1            OK
lz4                      1.7.5-1            OK
make                     4.2.1-2            OK
man-db                   2.7.6.1-1          OK
mintty                   3.1.0-1            OK
ncurses                  6.1-1.20190727     OK
openssl                  1.1.1d-1           OK
p11-kit                  0.23.15-1          OK
p11-kit-trust            0.23.15-1          OK
publicsuffix-list-dafsa  20190717-1         OK
pv                       1.4.12-1           OK
rebase                   4.4.4-1            OK
run                      1.3.4-2            OK
sed                      4.4-1              OK
tar                      1.29-1             OK
terminfo                 6.1-1.20190727     OK
terminfo-extra           6.1-1.20190727     OK
tzcode                   2019c-1            OK
tzdata                   2019c-1            OK
util-linux               2.33.1-1           OK
vim-minimal              8.1.1772-1         OK
w32api-headers           7.0.0-1            OK
w32api-runtime           7.0.0-1            OK
wget                     1.19.1-2           OK
which                    2.20-2             OK
windows-default-manifest 6.4-1              OK
xz                       5.2.4-1            OK
zlib0                    1.2.11-1           OK
Use -h to see help about each section

[-- Attachment #3: Type: text/plain, Size: 219 bytes --]


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: pread() from /dev/sda fails with: Illegal seek
  2020-01-11 14:10 pread() from /dev/sda fails with: Illegal seek David Balažic
@ 2020-01-11 20:24 ` Enrique Perez-Terron
  2020-01-12 16:59 ` Christian Franke
  1 sibling, 0 replies; 3+ messages in thread
From: Enrique Perez-Terron @ 2020-01-11 20:24 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 1118 bytes --]

Den 2020-01-11 15:10, skrev David Balažic:
> pread on /dev/sda fails with Illegal seek.

Confirmed on windows 10, 64-bit.

My code:
$ cat t.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
         int fd = open("/dev/sda", O_RDONLY, 0);
         if (fd < 0) {
                 fprintf(stderr, "%s. open /dev/sda: %m\n", argv[0]);
                 exit(1);
         }
         char *buffer = malloc(512);
         if (!buffer) {
                 fprintf(stderr, "%s: Out of memory\n", argv[0]);
                 exit(1);
         }
         int result = pread(fd, buffer, 512, 0);
         if (result < 0) {
                 fprintf(stderr, "%s: pread: %m\n", argv[0]);
                 exit(1);
         }
         printf("All right!\n");
         return 0;
}

Running in a mintty as Administrator:
$ ./t
./t: pread: Illegal seek

$ uname -a
CYGWIN_NT-10.0 panter 3.1.2(0.340/5/3) 2019-12-21 15:25 x86_64 Cygwin

$ cc --version
cc (GCC) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.


Regards,
Enrique Perez-Terron

[-- Attachment #2: 0xD6B3CEE5.asc --]
[-- Type: application/pgp-keys, Size: 3107 bytes --]

[-- Attachment #3: Type: text/plain, Size: 219 bytes --]


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: pread() from /dev/sda fails with: Illegal seek
  2020-01-11 14:10 pread() from /dev/sda fails with: Illegal seek David Balažic
  2020-01-11 20:24 ` Enrique Perez-Terron
@ 2020-01-12 16:59 ` Christian Franke
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Franke @ 2020-01-12 16:59 UTC (permalink / raw)
  To: cygwin

David Balažic wrote:
> I'm porting an app from Linux to Cygwin and stumbled on this problem:
>
> pread on /dev/sda fails with Illegal seek.
>
> The simplified code is:
>
> #include <fcntl.h>
> #include <stdio.h>
> #include <unistd.h>
> #include <stdlib.h>
>
> int main(int argc, char *argv[])
> {
>    unsigned int sector_size = 512;
>    void *buf = calloc(sector_size,1);
>    int fd = open("/dev/sda", O_DIRECT | O_RDONLY);
>    if (-1 == pread(fd, buf, sector_size, 0)) {
>      perror("pread failed");
>    }
> }
>
> # g++ test.cc
> # ./a  # run as adnministrator
> pread failed: Illegal seek
>
> Why is that?


Cygwin implements pread() and pwrite() only for regular disk files. For 
all other file types (including raw devices) these functions fail with 
errno EPIPE ("Illegal seek").

According to POSIX, "pread() ... shall read from a given position in the 
file without changing the file offset", see [1]. This must also hold if 
the file descriptor is used in multiple threads or is duplicated or is 
inherited from/to other processes. This cannot be easily emulated with 
Windows API. See [2] for a related long comment in source code.

If the app you want to port uses the fd only locally in a single thread, 
then something like this function may work:

ssize_t emul_pread(int fd, void *buf, size_t n, off_t offset)
{
   off_t oldoffs = lseek(fd, 0, SEEK_CUR);
   if (oldoffs == (off_t)-1)
     return -1;
   if (lseek(fd, offset, SEEK_SET) == (off_t)-1)
     return -1;
   ssize_t nr = read(fd, buf, n);
   if (lseek(fd, oldoffs, SEEK_SET) == (off_t)-1)
     return -1;
   return nr;
}

Sorry, not tested :-)

Regards,
Christian

[1]
https://pubs.opengroup.org/onlinepubs/9699919799/functions/pread.html

[2]
https://cygwin.com/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/fhandler_disk_file.cc;h=32381a0b0dbff6b7983ec8c4cec2f67dc5821939#l1479


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-01-12 16:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-11 14:10 pread() from /dev/sda fails with: Illegal seek David Balažic
2020-01-11 20:24 ` Enrique Perez-Terron
2020-01-12 16:59 ` Christian Franke

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).