{"action":"created","issue":{"url":"https://api.github.com/repos/analogdevicesinc/libiio/issues/23","labels_url":"https://api.github.com/repos/analogdevicesinc/libiio/issues/23/labels{/name}","comments_url":"https://api.github.com/repos/analogdevicesinc/libiio/issues/23/comments","events_url":"https://api.github.com/repos/analogdevicesinc/libiio/issues/23/events","html_url":"https://github.com/analogdevicesinc/libiio/issues/23","id":122901038,"number":23,"title":"Function read_all (local.c) return -5 when data is available","user":{"login":"jcsistemas2001","id":5248986,"avatar_url":"https://avatars.githubusercontent.com/u/5248986?v=3","gravatar_id":"","url":"https://api.github.com/users/jcsistemas2001","html_url":"https://github.com/jcsistemas2001","followers_url":"https://api.github.com/users/jcsistemas2001/followers","following_url":"https://api.github.com/users/jcsistemas2001/following{/other_user}","gists_url":"https://api.github.com/users/jcsistemas2001/gists{/gist_id}","starred_url":"https://api.github.com/users/jcsistemas2001/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jcsistemas2001/subscriptions","organizations_url":"https://api.github.com/users/jcsistemas2001/orgs","repos_url":"https://api.github.com/users/jcsistemas2001/repos","events_url":"https://api.github.com/users/jcsistemas2001/events{/privacy}","received_events_url":"https://api.github.com/users/jcsistemas2001/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-12-18T08:01:57Z","updated_at":"2015-12-18T13:00:54Z","closed_at":null,"body":"In a acquisition system with 36 bytes by sample, a buffer is created [iio_device_create_buffer(dev, 1000, false)], and trying to fetch 2 samples, the function iio_buffer_refill return -5. \r\n\r\nAfter debugging, the function read_all (at local.c) return -5 in the last condition because the function read the samples partially (not all bytes defined in \"len\").\r\n\r\n    static ssize_t read_all(void *dst, size_t len, int fd)\r\n    {\r\n\tuintptr_t ptr = (uintptr_t) dst;\r\n\tssize_t readsize;\r\n\tint ret;\r\n\twhile (len > 0) {\r\n\t\tdo {\r\n\t\t\tret = read(fd, (void *) ptr, len);\r\n\t\t} while (ret == -1 && errno == EINTR);\r\n\r\n\t\tif (ret == -1) {\r\n\t\t\tret = -errno;\r\n\t\t\tbreak;\r\n\t\t} else if (ret == 0) {\r\n\t\t\tret = -EIO;\r\n\t\t\tbreak;\r\n\t\t}\r\n\r\n\t\tptr += ret;\r\n\t\tlen -= ret;\r\n\t}\r\n\treadsize = (ssize_t)(ptr - (uintptr_t) dst);\r\n\tif ((ret > 0 || ret == -EAGAIN) && (readsize > 0)) \r\n\t\treturn readsize;\r\n\telse \r\n\t\treturn ret;\r\n    }\r\n\r\nIf the samples count (in my case 2) are not in the driver buffer, the library discard any sample acquired and return -5.  \r\n\r\nI suggest to two changes:\r\n\r\n- Not change \"ret\" to \"-EIO\" when read return 0\r\n- Compare \"ret\" with \">=\" in last condition\r\n\r\nBelow the suggestion in the code:\r\n\r\n    static ssize_t read_all(void *dst, size_t len, int fd)\r\n    {\r\n\tuintptr_t ptr = (uintptr_t) dst;\r\n\tssize_t readsize;\r\n\tint ret;\r\n\twhile (len > 0) {\r\n\t\tdo {\r\n\t\t\tret = read(fd, (void *) ptr, len);\r\n\t\t} while (ret == -1 && errno == EINTR);\r\n\t\tif (ret == -1) {\r\n\t\t\tret = -errno;\r\n\t\t\tbreak;\r\n\t\t} else if (ret == 0) {\r\n\t\t\t// >>>> ret = -EIO;  <<<<\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\tptr += ret;\r\n\t\tlen -= ret;\r\n\t}\r\n\treadsize = (ssize_t)(ptr - (uintptr_t) dst);\r\n\t// >>>> if ((ret > 0 || ret == -EAGAIN) && (readsize > 0)) <<<<\r\n\tif ((ret >= 0 || ret == -EAGAIN) && (readsize > 0)) \r\n\t\treturn readsize;\r\n\telse\r\n\t\treturn ret;\r\n    }"},"comment":{"url":"https://api.github.com/repos/analogdevicesinc/libiio/issues/comments/165772187","html_url":"https://github.com/analogdevicesinc/libiio/issues/23#issuecomment-165772187","issue_url":"https://api.github.com/repos/analogdevicesinc/libiio/issues/23","id":165772187,"user":{"login":"jcsistemas2001","id":5248986,"avatar_url":"https://avatars.githubusercontent.com/u/5248986?v=3","gravatar_id":"","url":"https://api.github.com/users/jcsistemas2001","html_url":"https://github.com/jcsistemas2001","followers_url":"https://api.github.com/users/jcsistemas2001/followers","following_url":"https://api.github.com/users/jcsistemas2001/following{/other_user}","gists_url":"https://api.github.com/users/jcsistemas2001/gists{/gist_id}","starred_url":"https://api.github.com/users/jcsistemas2001/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jcsistemas2001/subscriptions","organizations_url":"https://api.github.com/users/jcsistemas2001/orgs","repos_url":"https://api.github.com/users/jcsistemas2001/repos","events_url":"https://api.github.com/users/jcsistemas2001/events{/privacy}","received_events_url":"https://api.github.com/users/jcsistemas2001/received_events","type":"User","site_admin":false},"created_at":"2015-12-18T13:00:53Z","updated_at":"2015-12-18T13:00:53Z","body":"Thanks for your reply. I underand your statement about to follow the standard. However, it's not clear to me, that the function must return 0 when the pipe is closed, I think must return -1 with ernno (http://pubs.opengroup.org/onlinepubs/009695399/functions/read.html).\r\nIn any case, may I know your suggestion about the value that must be returned by the driver (kfifo) to inform the application that there is not data available?\r\nIt's odd because nobody has noted data loss (based on iio_readdev example).\r\n"}}