where is "syscall" function is declared ?

363 views
Skip to first unread message

Pavel Yermolenko

unread,
Sep 8, 2020, 7:12:05 AM9/8/20
to BeagleBoard
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<iostream>
#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.

Dennis Lee Bieber

unread,
Sep 8, 2020, 10:24:26 AM9/8/20
to Beagleboard
On Tue, 8 Sep 2020 04:12:05 -0700 (PDT), in
gmane.comp.hardware.beagleboard.user Pavel Yermolenko
<py.ohayo-FOWRGO...@public.gmane.org> wrote:

>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:~$


--
Dennis L Bieber

Dennis Lee Bieber

unread,
Sep 8, 2020, 10:35:43 AM9/8/20
to Beagleboard
On Tue, 08 Sep 2020 10:24:06 -0400, in gmane.comp.hardware.beagleboard.user
Dennis Lee Bieber <dennis.l.bieber-Re5...@public.gmane.org>
wrote:


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

-=-=-=-
debian@beaglebone:~/exploring$ cat test.cpp
#include<gnu/libc-version.h>
#include<sys/syscall.h>
#include<sys/types.h>
#include<iostream>
#include<signal.h>

#include <unistd.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;
}
debian@beaglebone:~/exploring$
-=-=-=-
debian@beaglebone:~/exploring$ g++ test.cpp -o test
debian@beaglebone:~/exploring$ ./test
The GNU libc version is 2.28
The Current PID is: 2396
The Current PID is: 2396
It is being run by user with ID: 1000
It is being run by user with ID: 1000
debian@beaglebone:~/exploring$


The example program in the second edition has that line.


--
Dennis L Bieber

Pavel Yermolenko

unread,
Sep 8, 2020, 11:57:45 AM9/8/20
to BeagleBoard


On Tuesday, September 8, 2020 at 4:24:26 PM UTC+2, Dennis Bieber wrote:
On Tue, 8 Sep 2020 04:12:05 -0700 (PDT), in
gmane.comp.hardware.beagleboard.user Pavel Yermolenko
<py.ohayo-FOWRGOYEGZlyDzI6CaY1VQ@public.gmane.org> wrote:

>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.com/derekmolloy/exploringbb.git
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.

Pavel Yermolenko

unread,
Sep 8, 2020, 12:05:10 PM9/8/20
to BeagleBoard


On Tuesday, September 8, 2020 at 4:35:43 PM UTC+2, Dennis Bieber wrote:
On Tue, 08 Sep 2020 10:24:06 -0400, in gmane.comp.hardware.beagleboard.user

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

jonnymo

unread,
Sep 8, 2020, 1:46:03 PM9/8/20
to Beagle Board
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 
 


On Tue, Sep 8, 2020 at 9:05 AM Pavel Yermolenko <py.o...@sunrise.ch> wrote:


On Tuesday, September 8, 2020 at 4:35:43 PM UTC+2, Dennis Bieber wrote:
On Tue, 08 Sep 2020 10:24:06 -0400, in gmane.comp.hardware.beagleboard.user

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/d28ca569-05a8-4d40-845b-385f89105ea2o%40googlegroups.com.

Pavel Yermolenko

unread,
Sep 9, 2020, 3:58:05 AM9/9/20
to BeagleBoard
Yes, I proceeded in the same way ... as Dennis Bieber suggested: so adding of #include <unistd.h> resolves the issue.


On Tuesday, September 8, 2020 at 7:46:03 PM UTC+2, jonnymo wrote:
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 
 


On Tue, Sep 8, 2020 at 9:05 AM Pavel Yermolenko <py....@sunrise.ch> wrote:


On Tuesday, September 8, 2020 at 4:35:43 PM UTC+2, Dennis Bieber wrote:
On Tue, 08 Sep 2020 10:24:06 -0400, in gmane.comp.hardware.beagleboard.user
To unsubscribe from this group and stop receiving emails from it, send an email to beagl...@googlegroups.com.

Dennis Lee Bieber

unread,
Sep 9, 2020, 11:30:18 AM9/9/20
to Beagleboard
On Tue, 8 Sep 2020 08:57:45 -0700 (PDT), in
gmane.comp.hardware.beagleboard.user Pavel Yermolenko
<py.ohayo-FOWRGO...@public.gmane.org> wrote:
>>
>
>2nd Ed. I cloned the source from github.com/derekmolloy/exploringbb.git
>This link is proposed in the 2nd edidtion of the book (p. xxxiv)
>

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.


--
Dennis L Bieber

jonnymo

unread,
Sep 9, 2020, 1:28:31 PM9/9/20
to Beagle Board
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

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/g3thlf15sthqjajpiia5q64opdadr915vp%404ax.com.

Robert Nelson

unread,
Sep 9, 2020, 1:31:56 PM9/9/20
to Beagle Board
On Wed, Sep 9, 2020 at 12:28 PM jonnymo <jonn...@gmail.com> wrote:
>
> 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.

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,


--
Robert Nelson
https://rcn-ee.com/

jonnymo

unread,
Sep 9, 2020, 1:40:42 PM9/9/20
to Beagle Board
The only real references I have seen is in the following gnulib manual :

And the subsequence link:

Cheers,

Jon


--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages