Working with libdpkg in C - c

I looked into the "dpkg-query" source-code and tried to get the list of installed packages.
It compiles, but when i try to run i get the fault at modstatdb_open() function.
#define LIBDPKG_VOLATILE_API 1
#include <stdio.h>
#include <unistd.h>
#include <dpkg/macros.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg-array.h>
#include <dpkg/debug.h>
#include <dpkg/pkg-list.h>
void main()
{
struct pkg_array array;
modstatdb_open(msdbrw_readonly | msdbrw_available_readonly);
pkg_array_init_from_db(&array);
printf("%d\n",array.n_pkgs);
pkg_array_destroy(&array);
modstatdb_shutdown();
}
Segmentation fault (core dumped)
What is wrong there?

Working version.
It has to use dpkg_program_init(char *progname); before all.
#define LIBDPKG_VOLATILE_API 1
#include <stdio.h>
#include <unistd.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg-array.h>
void main()
{
struct pkgset *set;
struct pkginfo *inf;
struct pkg_array array;
dpkg_program_init("a.out");
modstatdb_open(msdbrw_available_readonly);
pkg_array_init_from_db(&array);
printf("Number of packages in local database: %d\n",array.n_pkgs);
inf = pkg_db_find_singleton("kate");
printf("status code of package: %d\n", inf->status);// 7 means installed
dpkg_program_done();
}

Related

C macro name must be an identifier

I've created a c project and this is the beginning of the main.c file:
#include <curl/curl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "include/httpdef.h"
//...some code
The httpdef.h beginning is this:
#ifndef httpdef
#define httpdef
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
//definitions
#endif
At the very first line of both files I get the error from the gcc compiler:
macro name must be an identifier
What could be the problem?
EDIT: I realized now that actually the compiler doesn't give any error, it's my vim plugin (YouCOmpleteMe) that generates this error. If I compile everything works and the error doesn't appear

How do I use user_regs_struct on ARM processor in ptrace-based debugger

I am following the code in Chapter 3 of "Learning Linux Binary Analysis". The code is, "A simple ptrace-based debugger".
I am trying to write this in my Raspberry Pi 3, which has an ARM processor. I know that the error means that I have not included the correct header file, but I cannot find what the correct header file is.
I get this error message:
field ‘pt_reg’ has incomplete type struct user_regs_struct pt_reg
After doing some research, I found asm/ptrace.h, which includes some macros for the registers struct specific to ARM. Am I on the right track? How in the world can I make this work with an ARM processor?
Here are the headers:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <elf.h>
#include <sys/types.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <sys/ptrace.h>
#include <sys/mman.h>
typedef struct handle {
Elf64_Ehdr *ehdr;
Elf64_Phdr *phdr;
Elf64_Shdr *shdr;
uint8_t *mem;
char *symname;
Elf64_Addr symaddr;
struct user_regs_struct pt_reg;
char *exec;
} handle_t;

Getter & Setter in C

i'm facing a problem in C where i'm trying to use some getter and setter to share a variable between multiple source file.
I declare here my variable (ok_button) with a getter and a setter:
variable.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include "../libhd_src/libhd.h"
int ok_button;
void set_ok_button(int value){
ok_button=value;
printf("Setting ok");
}
int get_ok_button(){
return ok_button;
}
Here, when i push a button, it sets the variable to 1. (Can't upload the full code of this source file, but i see in my logs that the function set_ok_button is correctly execuded when i press (i see the printf "Setting OK" everytime i press my button))
button.c
#include "../libhd_src/libhd.h"
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
void * button_back_center_short(void *arg){
set_ok_button(1);
return 0;
}
And here, i simply check the value of my variable with the getter function.
read.c
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <time.h>
#include "../../libhd_src/libhd.h"
int main(int argc, char **argv){
while(1){
printf("Value %d", get_ok_button());
usleep(500000);
}
}
The problem is that the value shown in read.c is always "0" even when i press my button and set the value to 1...
Does someone understand what's wrong ? Feel free to tell me if you see a better solution to do that :)
I think your problem may be that you have multiple set_ok_button and get_ok_button functions in different files. Make sure you only have them defined in one file, and in a header add 2 lines declaring (but not defining) the functions:
void set_ok_button(int value);
int get_ok_button();

Name server IP of linux machine using C language

I have to get name server IP of my system using C language.I am using Linux machine.
I have tried.
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/cdefs.h>
int main()
{
int res_init(void);
printf("_res.nscount %d\n",_res.nscount);
//printf("_res.nsaddr_list[0] %s\n",_res.nsaddr_list[0]);
return 0;
}
But I am getting _res.nscount as 0.Am I doing anything wrong?
You declared res_init() instead of calling it. Try:
Int main()
{
res_init();
/* ... */
However, nsaddr_list[0] isn't a string, so you won't be able to print it with printf("%s"). You'll have to use inet_ntoa() or similar to convert its sin_addr.s_addr value to a printable string.

Reading Garbage with shmget

I have created two processes. One of them creates a shared memory chunk and the other tries to read it. There are no compilation errors with this code but somehow Process 2 behaves weird.
Process 1:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#define nDEVICES 3
#define DEVICE_NAME_LIMIT 20
typedef struct d_list
{
char d_name[DEVICE_NAME_LIMIT];
int d_id;
}device_mapping;
int main()
{
key_t shared_memkey_D=ftok(".", 'D');
int shared_memid_D=shmget(shared_memkey_D, nDEVICES*sizeof(device_mapping), 0777|IPC_CREAT|IPC_EXCL);
device_mapping *DEVICES = (device_mapping*)shmat(shared_memid_D,0,0);
strcpy(DEVICES[0].d_name, "DISK");
strcpy(DEVICES[1].d_name, "PORT");
strcpy(DEVICES[2].d_name, "PRINTER");
DEVICES[0].d_id=1;
DEVICES[1].d_id=3;
DEVICES[2].d_id=2;
}
Process 2:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <signal.h>
#define nDEVICES 3
#define DEVICE_NAME_LIMIT 20
typedef struct d_list
{
char d_name[DEVICE_NAME_LIMIT];
int d_id;
}device_mapping;
int main()
{
key_t shared_memkey_D=ftok(".", 'D');
int shared_memid_D=shmget(shared_memkey_D, nDEVICES*sizeof(device_mapping), 0777);
device_mapping *DEVICES = (device_mapping*)shmat(shared_memid_D,0,0);
int i=0;
for (i=0; i<nDEVICES; i++)
printf("%s\t%d\n", DEVICES[i].d_name, DEVICES[i].d_id);
}
Process 2 is reading all garbage and I cannot figure out what is going wrong after several attempts. Please help.
EDIT:
I had copied the wrong program here. I have made the corrections. Its still giving me garbage.
Process 2 prints the following:
DISK 11038
?FG+ 3
#?FG+ 2
/* process 1 */
typedef struct d_list
{
char d_name[DEVICE_NAME_LIMIT];
int d_id;
}device_mapping;
and
/* process 2 */
typedef struct d_list
{
char * d_name;
int d_id;
}device_mapping;
are not the same.
Change device_mapping definition for process 2 to be the same as for process 1, using characters arrays but character pointers.
For process 1 d_name uses DEVICE_NAME_LIMIT bytes, for process 2 d_name uses 4 bytes (32bit) or 8 bytes (64bit).
I'd put the common structure definitions into a shared header file, which is included by both pocesses' sources.

Resources