-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuntu.cpp
129 lines (112 loc) · 3.49 KB
/
buntu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <syslog.h>
using namespace std;
#define DAEMON_NAME "vdaemon"
void process(){
ifstream fin("/home/gaurav/command.txt",ios::in);
if(fin!=NULL)
{
printf("\n File opened successfully through fopen()\n");
string command="0";
string str=" ";
FILE *fil;
fstream modify;
char *fn;
bool t=getline(fin,command);
char c=command[0];
while(t)
{
switch(c)
{
case '1':
{
getline(fin,str,'\n');
fn=new char[str.length()];
for(int i=0;i<str.length();i++)
fn[i]=str[i];
fil=fopen(fn,"w");
getline(fin,str,'\n');
break;
}
case '2':
{
getline(fin,str,'\n');
fn=new char[str.length()];
for(int i=0;i<str.length();i++)
fn[i]=str[i];
remove(fn);
getline(fin,str,'\n');
}
case '3':
{
getline(fin,str,'\n');
fn=new char[str.length()];
for(int i=0;i<str.length();i++)
fn[i]=str[i];
modify.open(fn,ios::out|ios::app);
getline(fin,str,'\n');
while(str.compare("@#$%^&*") != 0)
{
cout<<str<<endl;
modify<<str<<"\n";
getline(fin,str,'\n');
}
modify.close();
getline(fin,str,'\n');
break;
}
default: break;
}
t=getline(fin,command);
c=command[0];
printf("%c\n",c);
}
remove("/home/gaurav/command.txt");
}
else
{
printf("\nError\n");
}
}
int main(int argc, char *argv[]) {
//Set our Logging Mask and open the Log
setlogmask(LOG_UPTO(LOG_NOTICE));
openlog(DAEMON_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
syslog(LOG_INFO, "Entering Daemon");
pid_t pid, sid;
//Fork the Parent Process
pid = fork();
if (pid < 0) { exit(EXIT_FAILURE); }
//We got a good pid, Close the Parent Process
if (pid > 0) { exit(EXIT_SUCCESS); }
//Change File Mask
umask(0);
//Create a new Signature Id for our child
sid = setsid();
if (sid < 0) { exit(EXIT_FAILURE); }
//Change Directory
//If we cant find the directory we exit with failure.
if ((chdir("/")) < 0) { exit(EXIT_FAILURE); }
//Close Standard File Descriptors
close(STDIN_FILENO);
//close(STDOUT_FILENO);
close(STDERR_FILENO);
//----------------
//Main Process
//----------------
int i=4;
while(i--){
process(); //Run our Process
sleep(1); //Sleep for 1 seconds
}
//Close the log
closelog ();
}