where is "syscall" function is declared ?

Hello,

Here is an example from chapter 5 of the book “Explore BeagleBone” by Derek Molloy:

`
#include<gnu/libc-version.h>
#include<sys/syscall.h>
#include<sys/types.h>
#include
#include<signal.h>
using namespace std;

int main(){
//gnu_get_libc_version() returns a string that identifies the
//glibc version available on the system.
cout << "The GNU libc version is " << gnu_get_libc_version() << endl;

// process id tid is thread identifier
// look inside sys/syscall.h for System Call Numbers
pid_t tid; //pid_t is of type integer
tid = syscall(SYS_gettid); // make a system call to get the process id
cout << "The Current PID is: " << tid << endl;
//can also get by calling getpid() function from signal.h
cout << "The Current PID is: " << getpid() << endl;

// Can get current UserID by using:
int uid = syscall(SYS_getuid);
cout << "It is being run by user with ID: " << uid << endl;
// or getting the value from syscalls.kernelgrok.com
uid = syscall(0xc7);
cout << "It is being run by user with ID: " << uid << endl;

return 0;
}

`

It isn’t compiled:

debian@beaglebone:~/exploringbb/chp05/syscall$ g++ syscall.cpp -o syscall
syscall.cpp: In function ‘int main()’:
syscall.cpp:22:10: error: ‘syscall’ was not declared in this scope
tid = syscall(SYS_gettid); // make a system call to get the process id
^~~~~~~
syscall.cpp:22:10: note: suggested alternative: ‘swscanf’
tid = syscall(SYS_gettid); // make a system call to get the process id
^~~~~~~
swscanf
syscall.cpp:25:38: error: ‘getpid’ was not declared in this scope
cout << "The Current PID is: " << getpid() << endl;
^~~~~~
syscall.cpp:25:38: note: suggested alternative: ‘getpt’
cout << "The Current PID is: " << getpid() << endl;
^~~~~~
getpt
debian@beaglebone:~/exploringbb/chp05/syscall$

Indeed, I searched for syscall declaration in the toolchain … and didn’t find it:

debian@beaglebone:~/exploringbb/chp05/syscall$ sudo grep -rn /usr/include/arm-linux-gnueabihf/ -e “syscall(”
[sudo] password for debian:
debian@beaglebone:~/exploringbb/chp05/syscall$

Any comments ?

Thanks.

Hello,

Here is an example from chapter 5 of the book "Explore BeagleBone" by Derek
Molloy:

  First or second edition? I don't find your (following) example in the
second edition -- though there /is/ a program using syscall

debian@beaglebone:~/exploringbb/chp05/syscall$ g++ syscall.cpp -o syscall
syscall.cpp: In function ‘int main()’:
syscall.cpp:22:10: error: ‘syscall’ was not declared in this scope

  Note that your source file appears to be named syscall, along with the
output executable. That may be confusing the compiler some. Especially
since the header file that appears to provide the function is syscall.h
(which could imply there is a syscall.c (or .cpp) somewhere in the system.

  The example in the second edition is a file named glibcTest.cpp in a
DIRECTORY named syscall.

Indeed, I searched for syscall declaration in the toolchain ... and didn't
find it:

debian@beaglebone:~/exploringbb/chp05/syscall$ sudo grep -rn
/usr/include/arm-linux-gnueabihf/ -e "syscall("

  The odds are good that there is a space between syscall and the (

debian@beaglebone:~$ sudo find / -iname "syscall.h"
/usr/include/syscall.h
/usr/include/arm-linux-gnueabihf/sys/syscall.h
/usr/include/arm-linux-gnueabihf/bits/syscall.h
debian@beaglebone:~$

Though it turns out to be a definition in /usr/include/unistd.h which is
invoked by /sys/syscall.h (itself invoked by /syscall.h)

#ifdef __USE_MISC
/* Invoke `system call' number SYSNO, passing it the remaining arguments.
   This is completely system-dependent, and not often useful.

   In Unix, `syscall' sets `errno' for all errors and most calls return -1
   for errors; in many systems you cannot pass arguments or get return
   values for all system calls (`pipe', `fork', and `getppid' typically
   among them).

   In Mach, all system calls take normal arguments and always return an
   error code (zero for success). */
extern long int syscall (long int __sysno, ...) __THROW;

#endif /* Use misc. */

(NOTE the space I predicted)

debian@beaglebone:~$ sudo grep -rn /usr/include/arm-linux-gnueabihf/ -e
"syscall ("
/usr/include/arm-linux-gnueabihf/asm/unistd-common.h:4:#define
__NR_restart_syscall (__NR_SYSCALL_BASE + 0)
/usr/include/arm-linux-gnueabihf/asm/unistd-oabi.h:14:#define __NR_syscall
(__NR_SYSCALL_BASE + 113)
debian@beaglebone:~$ sudo grep -rn /usr/include/ -e "syscall ("
/usr/include/arm-linux-gnueabihf/asm/unistd-common.h:4:#define
__NR_restart_syscall (__NR_SYSCALL_BASE + 0)
/usr/include/arm-linux-gnueabihf/asm/unistd-oabi.h:14:#define __NR_syscall
(__NR_SYSCALL_BASE + 113)
/usr/include/unistd.h:1056:extern long int syscall (long int __sysno, ...)
__THROW;
debian@beaglebone:~$

As a follow-up, using YOUR source, I added one line (and changed the
file name)

Hello,

Here is an example from chapter 5 of the book “Explore BeagleBone” by Derek
Molloy:

First or second edition? I don’t find your (following) example in the
second edition – though there /is/ a program using syscall

2nd Ed. I cloned the source from GitHub - derekmolloy/exploringBB: Source code for the book Exploring BeagleBone, by Derek Molloy (see www.exploringbeaglebone.com)
This link is proposed in the 2nd edidtion of the book (p. xxxiv)

debian@beaglebone:~/exploringbb/chp05/syscall$ g++ syscall.cpp -o syscall
syscall.cpp: In function ‘int main()’:
syscall.cpp:22:10: error: ‘syscall’ was not declared in this scope

Note that your source file appears to be named syscall, along with the
output executable. That may be confusing the compiler some. Especially
since the header file that appears to provide the function is syscall.h
(which could imply there is a syscall.c (or .cpp) somewhere in the system.

The example in the second edition is a file named glibcTest.cpp in a
DIRECTORY named syscall.

Here is content of syscall subfolder:
debian@beaglebone:~/exploringbb/chp05/syscall$ ls -l *.cpp
-rw-r–r-- 1 debian debian 931 Sep 2 13:26 callchmod.cpp
-rw-r–r-- 1 debian debian 1234 Sep 2 13:26 glibcTest.cpp
-rw-r–r-- 1 debian debian 1345 Sep 2 13:26 syscall.cpp
debian@beaglebone:~/exploringbb/chp05/syscall$

So, there are both: glibcTest.cpp and syscall.cpp
Also there is build file:

debian@beaglebone:~/exploringbb/chp05/syscall$ more build
#!/bin/bash
g++ syscall.cpp -o syscall
g++ callchmod.cpp -o callchmod
debian@beaglebone:~/exploringbb/chp05/syscall$

Obviously Derek Molloy tried his code and undoubtedly he succeeded

Indeed, I searched for syscall declaration in the toolchain … and didn’t
find it:

debian@beaglebone:~/exploringbb/chp05/syscall$ sudo grep -rn
/usr/include/arm-linux-gnueabihf/ -e “syscall(”

The odds are good that there is a space between syscall and the (

debian@beaglebone:~$ sudo find / -iname “syscall.h”
/usr/include/syscall.h
/usr/include/arm-linux-gnueabihf/sys/syscall.h
/usr/include/arm-linux-gnueabihf/bits/syscall.h
debian@beaglebone:~$

Though it turns out to be a definition in /usr/include/unistd.h which is
invoked by /sys/syscall.h (itself invoked by /syscall.h)

#ifdef __USE_MISC
/* Invoke `system call’ number SYSNO, passing it the remaining arguments.
This is completely system-dependent, and not often useful.

In Unix, syscall' sets errno’ for all errors and most calls return -1
for errors; in many systems you cannot pass arguments or get return
values for all system calls (pipe', fork’, and `getppid’ typically
among them).

In Mach, all system calls take normal arguments and always return an
error code (zero for success). */
extern long int syscall (long int __sysno, …) __THROW;

#endif /* Use misc. */

(NOTE the space I predicted)

Ok

debian@beaglebone:~$ sudo grep -rn /usr/include/arm-linux-gnueabihf/ -e
“syscall (”
/usr/include/arm-linux-gnueabihf/asm/unistd-common.h:4:#define
__NR_restart_syscall (__NR_SYSCALL_BASE + 0)
/usr/include/arm-linux-gnueabihf/asm/unistd-oabi.h:14:#define __NR_syscall
(__NR_SYSCALL_BASE + 113)
debian@beaglebone:~$ sudo grep -rn /usr/include/ -e “syscall (”
/usr/include/arm-linux-gnueabihf/asm/unistd-common.h:4:#define
__NR_restart_syscall (__NR_SYSCALL_BASE + 0)
/usr/include/arm-linux-gnueabihf/asm/unistd-oabi.h:14:#define __NR_syscall
(__NR_SYSCALL_BASE + 113)
/usr/include/unistd.h:1056:extern long int syscall (long int __sysno, …)
__THROW;
debian@beaglebone:~$


Dennis L Bieber

Anyway, what seems to work at Derek Molloy is not working with my toolchain.

Thanks.

Works ! Thanks.
Concerning 2nd ed, as I mentioned, I used link form 2nd edition.

What worked for you? I cloned the code from the link you provided and the syscall code has build errors.

What I did to clear this out was:

  1. In “syscall.cpp”, I added ‘unistd.h’ to the includes:
    Ex:
    #include <unistd.h>

  2. I did the same in callchmod.cpp,

  3. Run ./build

Apparently there is an issue with unistd.h not being added in certain areas with gcc.

Cheers,

Jon

Yes, I proceeded in the same way … as Dennis Bieber suggested: so adding of #include <unistd.h> resolves the issue.

Interesting... the program shown on
http://exploringbeaglebone.com/chapter5/#Using_a_System_Call_to_use_the_chmod_command
also does not have the include -- but as I mentioned, the program in the
actual book does have the needed include.

Note that the web example version of the library is a bit older, and the OS
may have distributed things differently.

Yeah, I missed where this was shown in the email.

As far as the link for Drerk’s web page is concerned, I suspect it is a reference to the first ed of his book.

From what I gathered, syscall is not POSIX compliant and could cause compatibility issues so it’s use is discouraged. This I would suspect is why there are suggestions from the error when running the example to use ‘swscanf’ and ‘getpt’ instead.

However, as you found, ‘syscall’ is defined in ‘unistd.h’ so having this included is required which does not seem that new. Then again, the code on Derek’s site is at least 6 years old and may not be kept up to date which could explain the use of syscall there.

Cheers,

Jon

I've found a few references to 4.7.x gcc and C89/C99 that this
changed, but haven't found the git commit/explanation..

Regards,

The only real references I have seen is in the following gnulib manual :
https://www.gnu.org/software/gnulib/manual/gnulib.pdf

And the subsequence link:
https://www.gnu.org/software/libc/manual/html_node/System-Calls.html

Cheers,

Jon