system-calls

Inline Assembler for wrapper function doesn't work for some reason

懵懂的女人 提交于 2019-12-02 09:31:48
问题 I'm trying to write a wrapper function for read() system call , using asm volatile , but it won't work , since the res doesn't change its value . Here's the code : ssize_t my_read(int fd, void *buf, size_t count) { ssize_t res; __asm__ volatile( "int $0x80" /* make the request to the OS */ : "=a" (res), /* return result in eax ("a") */ "+b" (fd), /* pass arg1 in ebx ("b") */ "+c" (buf), /* pass arg2 in ecx ("c") */ "+d" (count) /* pass arg3 in edx ("d") */ : "a" (5) /* passing the system call

C/Unix Strange behaviour while using system calls and printf

早过忘川 提交于 2019-12-02 08:50:09
I'm a newbie, trying to really understand systems programming. In the following program, I'm reading a file called 'temp1' (containing 1 2 3 4) and printing its contents to stdout. However, I also wanted to check the value of file descriptor returned by open. If I include the '\n' in printf call on line 5, the output prints value filep first and then contents of file. But if I remove the newline, the contents of file get printed first and then the value of filep. Why would this happen ? int main(){ char buf[BUFSIZ]; int n, filep; // Open the file filep = open("temp1", 'r'); printf("%d\n",

Assembly and System Calls

回眸只為那壹抹淺笑 提交于 2019-12-02 08:08:31
问题 Im having a bit of trouble understanding the more complex system calls in assembly. I wrote a exec system call and it worked great .bss .text .globl _start _start: #exit(0) system call movl $1, %rax movl $0, %rbx int $0X80 Though I am a bit insure and have not been able to find info pertaining to how you put strings in a register. So as an example I wanted to do a exec system call and it as its first parameter needs a filename to run and I want to run "/bin/bash", but how do I get that in rbx

pass parameter using system command

别来无恙 提交于 2019-12-02 07:23:07
I have an executable program that runs in several pc's in a network. At first it gets the host name (pc-001.. pc-013 etc). Then i need to mount a network drive (server1) on even pc's and (server2) on odds one based on its host name. My problem is that i use system call to run dos command 'net use' for example system ("net use x: \\\\server1\\shares /user:username pass"); How can i pass a variable to username? username is the host name which i know its value and pass is the same for all computers. You could build the command passed to system e.g. like char cmdbuf[256]; snprintf(cmdbuf, sizeof

Catching SIGINT signal to terminate a custom shell

依然范特西╮ 提交于 2019-12-02 07:17:27
问题 Hope you can help me to resolve this problem. For school I have to transform Ctrl+C to a command which doesn't shut down the shell, but he reminds through printf() that I must type exit to close the shell. I don't even know where to start. Thank a lot. 回答1: Here's a trivial implementation of handling SIGINT using sigaction which will work on posix systems. Left out error checking for brevity. The linked manual should explain about sigaction . Basically the program loops through an infinite

memcpy and then printf, calling stat(), writing in buffer;

落花浮王杯 提交于 2019-12-02 06:06:18
问题 here are includes and my function: I'm trying to copy stbuf->st_mode in buffer with memcpy and when reading it back, value is not what I was trying to copy. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h> #include <string.h> void r1_getattr(char* pth, int cfd){ struct stat* stbuf = malloc(sizeof(stat)); int res = stat(pth, stbuf); printf("(%3o)\n", (stbuf->st_mode)&0777); char* wbuf = malloc(256); memcpy(wbuf, &(stbuf->st_mode), sizeof(mode_t)); printf("(%3o)

Why doesn't this attempt at using sys_write do anything?

心已入冬 提交于 2019-12-02 04:26:17
Here it is: .SECTION .data msg: .string "AAAA" .SECTION .text .globl _start _start: mov $1, %rax mov $1, %rdi mov msg, %rsi mov $4, %rdx syscall Not only does this code not segfault, it also outputs nothing. According to what I've read, a program should call sys_exit, or it would segfault, but this does not happen. mov msg, %rsi This instruction will interpret the data at "msg" as 64-bit value and load that value into the register rsi . The instruction does NOT load the address of "msg" into register rsi . This could be done by (note the $ ): mov $msg, %rsi According to what I've read, a

Assembly and System Calls

蓝咒 提交于 2019-12-02 04:18:01
Im having a bit of trouble understanding the more complex system calls in assembly. I wrote a exec system call and it worked great .bss .text .globl _start _start: #exit(0) system call movl $1, %rax movl $0, %rbx int $0X80 Though I am a bit insure and have not been able to find info pertaining to how you put strings in a register. So as an example I wanted to do a exec system call and it as its first parameter needs a filename to run and I want to run "/bin/bash", but how do I get that in rbx. How do I even know that I have to use rbx, in X86 I know I would use ebx, is it the same relationship

Make a program using only system-calls not windows dll's [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-02 04:14:28
This question already has an answer here: Windows system calls [duplicate] 1 answer I am trying to make a program work with system-calls not dll's ( kernel32.dll , ntdll.dll ). I know for example that the 0x2C (44) system call in windows 10 64-bit is the NtTerminateProcess buy that web page. Also when I disassemble the ntdll.dll i found that code: NtTerminateProcess: mov r10, rcx mov eax, 44 test byte [abs 7FFE0308h], 01h ;also what is in that memory address? jnz label syscall ret label: int 46 ;and why the 46 (the 2Eh windows NT interrupt) is here ret My question is how can for example

How not to open a file twice in linux?

吃可爱长大的小学妹 提交于 2019-12-02 04:12:18
问题 I have a linked list with an fd and a string I used to open this file in each entry. I want to open and add files to this list only if this file is not already opened, because I open and parse this files and do not want to do it twice. My idea was to compare the filename with every single name in this list, but my program do it multiple times and one file in Linux can have multiple names (soft/hard links). I think it should not be so complicated, because its easy for the OS to check, whether