why am i getting these syntax errors when trying to implement system call - operating-system

still working on this system call!!!
i have added a system call to a kernel, compiled and the OS is running off it.
now i am getting syntax error on the compilation of my test application.
testmycall.h
#include<linux/unistd.h>
#define __NR_mycall 244
_syscall1(long, mycall, int, i)
testmycall.c
#include<stdio.h>
#include "testmycall.h"
int main(int argc, char *argv[])
{
printf("%d\n", mycall(15));
}
here is the error
stef#ubuntu:~$ gcc -o testmycall testmycall.c
In file included from testmycall.c:3:
testmycall.h:7: error: expected ‘)’ before ‘mycall’
stef#ubuntu:~$ gcc -o testmycall testmycall.c
In file included from testmycall.c:3:
testmycall.h:7: error: expected declaration specifiers or ‘...’ before ‘mycall’
testmycall.h:7: error: expected declaration specifiers or ‘...’ before ‘i’
testmycall.c: In function ‘_syscall1’:
testmycall.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
testmycall.h:7: error: parameter name omitted
testmycall.h:7: error: parameter name omitted
testmycall.c:11: error: expected ‘{’ at end of input
stef#ubuntu
i have added in the syscall instead of _syscall1
now i get this error
stef#ubuntu:~$ gcc -o testmycall testmycall.c
testmycall.c: In function ‘syscall’:
testmycall.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
testmycall.c:11: error: expected ‘{’
this is the app, any ideas why???

I believe the _syscallN() macros were removed from the kernel headers around 2.6.18 or so.
The (not especially helpful) error messages from gcc are due to _syscall1 not being defined at all - you get the same errors if you write:
any_old_rubbish_here(long, mycall, int, i)
The syscall() function should work. man syscall for details.

The _syscall macros are obsolete and should not be used, instead use syscall, eg.
#define _GNU_SOURCE
#include <unistd.h>
...
printf("%d\n", syscall(__NR_mycall, 15));
Here's my test program:
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#define __NR_mycall 244
int main(int argc, char **argv)
{
printf("%d\n", syscall(__NR_mycall,15));
return 0;
}

Related

Difficulty adding Paillier algorithm to perceptual hash implementation

Received error:
libNCPH.h:39:15: fatal error: paillier.h: No such file or directory
39 | #include <paillier.h>
In libNCPH.h, it is also in this way:
extern "C" {
#include <gmp.h>
#include <paillier.h>
#include "tcp.h"}

Build/compile errors because of compilerOptions in legacy_code

I want to create some trivial S-function with legacy_code for my MexTest.c:
legacy_code('compile', specs, compilerOptions)
There are 2 macros (TEST_DEF1 and TEST_DEF2), which I want to put into compilerOptions. The 2 macros are used in the function TestFnc().
But somehow, the TestFnc() does not see the 2 macros defined in compilerOptions before...
MexTest.c:
#include "MexTest.h"
void TestFnc(double *p_u, double *p_y)
{
*p_y = *p_u + 1;
#ifndef TEST_DEF1
#error "undefined: TEST_DEF1"
#endif
#ifndef TEST_DEF2
#error "undefined: TEST_DEF2"
#endif
}
MexTest.h:
#ifndef MEXTEST_H_
#define MEXTEST_H_
void TestFnc(double *p_u, double *p_y);
#endif
legacy_code tool
specs = legacy_code('initialize');
specs.HeaderFiles = {'MexTest.h'};
specs.SourceFiles = {'MexTest.c'};
specs.SFunctionName = 'Test_SFunc_gen';
specs.OutputFcnSpec = 'void TestFnc(double u1[1], double y1[1])';
%% Compile C-MEX-s-function
legacy_code('sfcn_cmex_generate', specs);
legacy_code('generate_for_sim',specs);
legacy_code('compile', specs, {'-DTEST_DEF1', '-DTEST_DEF2'});
Mex Build Errors:
### Start Compiling Test_SFunc_gen
mex('-IC:\Users\xxxx\Documents\', '-c', '-outdir', 'C:\Users\xxxx\AppData\Local\Temp\tp05e00c12_2732_4dab_a0f8_f30bd05992f8', 'C:\Users\xxxx\Documents\MexTest.c')
Building with 'MinGW64 Compiler (C)'.
Error using mex
C:\Users\xxxx\Documents\MexTest.c: In function 'TestFnc':
C:\Users\xxxx\Documents\MexTest.c:8:10: error: #error "undefined: TEST_DEF1"
#error "undefined: TEST_DEF1"
C:\Users\xxxx\Documents\MexTest.c:12:10: error: #error "undefined: TEST_DEF2"
#error "undefined: TEST_DEF2"
How could I use the -D option correctly for my case?
(Matlab version 2021a)

db2 external function ends with SQL0444N, Reason code 6, SQLSTATE 42724

I'll develop new (external) functions for DB2. My first test:
db2crypt.h
#ifndef DB2CRYPT_H
#define DB2CRYPT_H
#include <string.h>
#include <stdlib.h>
char *encryptAes(const char *source, const char *key);
#endif /* DB2CRYPT_H */
db2crypt.cpp
#include "db2crypt.h"
#include <string>
char *encryptAes(const char *source, const char *key) {
std::string test("abc");
return (char *)test.c_str();
}
is compiled without an error.
g++ -fPIC -c db2crypt.cpp -std=c++14
g++ -shared -o db2crypt db2crypt.o -L$DB2PATH -ldb2
I also copied the new file into $DB2PATH/function and made a softlink in $DB2PATH/function/unfenced.
Then I created the function with
create function aes(VARCHAR(4096), VARCHAR(4096))
SPECIFIC encryptAes
RETURNS VARCHAR(4069)
NOT FENCED
DETERMINISTIC
NO SQL
NO EXTERNAL ACTION
LANGUAGE C
RETURNS NULL ON NULL
INPUT PARAMETER STYLE SQL
EXTERNAL NAME "db2crypt!encryptAes"
which was also ok.
But when I do select db2inst1.aes('a', 'b') from SYSIBM.SYSDUMMY1
I get the error
SQL0444N Die Routine "DB2INST1.AES" (spezifischer Name "ENCRYPTAES") ist
durch Code in Bibliothek oder Pfad ".../sqllib/function/db2crypt", Funktion
"encryptAes" implementiert, auf die kein Zugriff möglich ist. Ursachencode:
"6". SQLSTATE=42724
(sorry, I don't know how to change the error output into english)
What I made wrong?
Ok, I got the answer.
Thank you at #mao, you did help me. But I also needed some other help. If someone searches for an answer:
First you have to compile with some important parameters:
g++ -m64 -fPIC -c <yourfile>.cpp -std=c++14 -I/opt/ibm/db2/V11.1/include/ -D_REENTRANT
g++ -m64 -shared -o <yourfile> <yourfile>.o -L$DB2PATH -ldb2 -Wl,-rpath,$DB2PATH/$LIB -lpthread
Second: The function declaration, you also have to add parameters for null values AND the return value can't be a function return, it has to be a parameter. Also you have to use the types which are defined in sqludf.h:
void SQL_API_FN encryptAes(SQLUDF_CHAR *source,
SQLUDF_CHAR *key,
SQLUDF_CHAR out[4096],
SQLUDF_SMALLINT *sourcenull,
SQLUDF_SMALLINT *keynull,
SQLUDF_SMALLINT *outnull,
SQLUDF_TRAIL_ARGS) {
...
}
Also, when you do C++ instead of C, you have to tell the script that it has to handle the function as C:
#ifdef __cplusplus
extern "C"
#endif
void SQL_API_FN ...

error while compiling with matlab mex

I am try to compile using matlab mex, but i keep receiving this error:
Warning: You are using gcc version "5.4.0-6ubuntu1~16.04.1)". The version
currently supported with MEX is "4.2.3".
For a list of currently supported compilers see:
http://www.mathworks.com/support/compilers/current_release/
In file included from descriptor.h:4:0,
from calc_shot.cpp:11:
mesh.h: In function ‘double magnitude(const vec3d<T>&)’:
mesh.h:106:9: error: ‘sqrt’ is not a member of ‘std’
return std::sqrt((v1.x*v1.x) + (v1.y*v1.y) + (v1.z*v1.z));
^
In file included from calc_shot.cpp:11:0:
descriptor.h: At global scope:
descriptor.h:7:57: error: expected class-name before ‘{’ token
class invalid_mesh_descriptor : public std::logic_error {
^
descriptor.h: In constructor ‘invalid_mesh_descriptor::invalid_mesh_descriptor()’:
descriptor.h:9:57: error: expected class-name before ‘(’ token
explicit invalid_mesh_descriptor() : std::logic_error("Exception invalid_mesh_descriptor caught.") {}
^
descriptor.h:9:57: error: expected ‘{’ before ‘(’ token
descriptor.h: In constructor ‘invalid_mesh_descriptor::invalid_mesh_descriptor(const string&)’:
descriptor.h:10:70: error: expected class-name before ‘(’ token
invalid_mesh_descriptor(const std::string& msg) : std::logic_error("Exception invalid_mesh_descriptor caught: "+msg) {}
^
descriptor.h:10:70: error: expected ‘{’ before ‘(’ token
descriptor.h: In function ‘std::ostream& operator<<(std::ostream&, const vec_descriptor<T>&)’:
descriptor.h:123:45: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘const char [2]’)
for (size_t k=1; k < d.size(); ++k) s << " " << d(k);
^
In file included from /usr/include/c++/5/string:52:0,
from descriptor.h:5,
from calc_shot.cpp:11:
/usr/include/c++/5/bits/basic_string.h:5172:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
operator<<(basic_ostream<_CharT, _Traits>& __os,
^
/usr/include/c++/5/bits/basic_string.h:5172:5: note: template argument deduction/substitution failed:
In file included from calc_shot.cpp:11:0:
descriptor.h:123:48: note: mismatched types ‘const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>’ and ‘const char [2]’
for (size_t k=1; k < d.size(); ++k) s << " " << d(k);
^
mex: compile of ' "calc_shot.cpp"' failed.
i used to compile this code with LD_PRELOAD but at the moment i do not remember the argument, how can i solve?
solution:
mex -v calc_shot.cpp shot_descriptor.cpp -DUSE_FLANN -I"C:\Program Files\flann\include" -I/usr/include/eigen3
using this command the programs works like a charm

g++ problem with -l option and PostgreSQL

I've written simple program.
Here a code:
#include <iostream>
#include <stdio.h>
#include <D:\Program Files\PostgreSQL\8.4\include\libpq-fe.h>
#include <string>
using namespace std;
int main()
{
PGconn *conn;
PGresult *res;
int rec_count;
int row;
int col;
cout << "ble ble: " << 8 << endl;
conn = PQconnectdb("dbname=db_pm host=localhost user=postgres password=postgres");
if (PQstatus(conn) == CONNECTION_BAD) {
puts("We were unable to connect to the database");
exit(0);
}
}
I'm trying to connect with PostgreSQL.
I compile this code with following command:
gcc -I/"d:\Program Files\PostgreSQL\" -L/"d:\Program Files\PostgreSQL\8.4\lib\" -lpq -o firstcpp.o firstcpp.cpp
This command is from following site:
http://www.mkyong.com/database/how-to-building-postgresql-libpq-programs/
And when I compile it I get following error:
/cygnus/cygwin-b20/H-i586-cygwin32/i586-cygwin32/bin/ld: cannot open -lpq: No such file or directory
collect2: ld returned 1 exit status
Does anyone help me?
Difek
You can try using forward slashes instead of backward slashes. And I have no idea about the first forward slash. Isn't it meant to be inside the quotes ? Eg -I"/d:/Program Files/PostgreSQL/"
Anyway, if you are using the gcc from cygwin, you could also try
-I"/cygdrive/d/Program Files/PostgreSQL"
And I'd do the same with that include (libpq-fe) - though apparently that is working, the error is in the linker.