1

Hi i am having some trouble using sys_open.At this point i am starting to think that i am doing something wrong with the arguments of the functions. An example of how i use it:

fd = sys_open("file.txt" , O_WRONLY | O_CREAT, 0);
sys_write(fd, "test\n", strlen("test\n"));
sys_fsync(fd);
sys_fdatasync(fd);
sys_close(fd);

fd value is a non negative integer (0) . Every single one of the functions returns 0 so i guess they are executed corectly.Except sys_write. Every time that i go to find the file after i write on it i can never find it any idea maybe something on my code is wrong?

wxz
  • 2,254
  • 1
  • 10
  • 31
Mast Reyheart
  • 43
  • 1
  • 9
  • 1
    Meaning `sys_open` returns `fd = 0`? – wxz May 02 '21 at 17:39
  • yes it returns 0 if i remove O_CREAT it returns a negative but i have aother code that its creats a file using sys_creat – Mast Reyheart May 02 '21 at 17:49
  • 1
    From the [open syscall description](https://man7.org/linux/man-pages/man2/open.2.html): ```The mode argument specifies the file mode bits to be applied when a new file is created. If neither O_CREAT nor O_TMPFILE is specified in flags, then mode is ignored (and can thus be specified as 0, or simply omitted). The mode argument must be supplied if O_CREAT or O_TMPFILE is specified in flags; if it is not supplied, some arbitrary bytes from the stack will be applied as the file mode.``` – wxz May 02 '21 at 18:14
  • i am sorry but i lost you so i use O_CREAT or O_TMPFILE i must give the 3rd argument?did i understand it coreclty? – Mast Reyheart May 02 '21 at 18:19
  • 1
    Yes, if you do O_CREATE then you must supply a mode argument (argument 3) or else you'll get undefined behavior. Also typically file descriptor `fd` 0 is for stdin, so getting 0 from sys_open on a file is a bad sign. Read that link I posted and update your mode bit argument. If it answers your question, let me know and I can post it as an answer – wxz May 02 '21 at 18:22
  • yes i am trying to use the the mode_t i will let you know if that fix it thank you – Mast Reyheart May 02 '21 at 18:27
  • well after reading the link you sent me it was helpful so thank you but unfortunetly it didnt resolve my problem if you have any idea why it returns fd =0 or what should i look for please le me know thanks again :) – Mast Reyheart May 02 '21 at 19:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231861/discussion-between-wxz-and-mast-reyheart). – wxz May 02 '21 at 19:06
  • The string `"file.txt"` is not in user space – stark May 02 '21 at 20:32
  • what do you mean isnt in user space? – Mast Reyheart May 03 '21 at 09:55

1 Answers1

2

The sys_ family of functions for Linux are considered deprecated now. However, for the purposes of this question, sys_open is a user-level function that calls the open syscall, which then calls the kernel level do_sys_open(), the latter two defined in fs/open.c. So to open files from within the kernel you should stick to kernel level functions and not user-level functions.

Here are some answers about opening files at the kernel level, some of which are deprecated as well, so be aware.

wxz
  • 2,254
  • 1
  • 10
  • 31