Skip to content

Commit 6741694

Browse files
committed
notify java that all childs has been killed when disconnecting.
free child list on disconnect.
1 parent bef3804 commit 6741694

10 files changed

Lines changed: 92 additions & 14 deletions

File tree

cSploitClient/child.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,29 @@ inline child_node *get_child_by_pending_seq(uint16_t seq) {
6969
return c;
7070
}
7171

72-
void free_child(child_node *c) {
72+
inline void free_child(child_node *c) {
7373
release_buffer(&(c->buffer));
7474
free(c);
7575
}
7676

77+
void free_all_childs() {
78+
child_node *c,*n;
79+
80+
pthread_mutex_lock(&(children.control.mutex));
81+
82+
c=(child_node *) children.list.head;
83+
84+
children.list.head = children.list.tail = NULL;
85+
86+
while(c) {
87+
n=(child_node *) c->next;
88+
free_child(c);
89+
c=n;
90+
}
91+
92+
pthread_mutex_unlock(&(children.control.mutex));
93+
}
94+
7795
/**
7896
* @brief send raw bytes to child
7997
* @param id the child id
@@ -147,4 +165,4 @@ jboolean send_to_child(JNIEnv *env, jclass clazz __attribute__((unused)), int id
147165
(*env)->ReleaseByteArrayElements(env, array, buff, JNI_ABORT);
148166

149167
return ret;
150-
}
168+
}

cSploitClient/child.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ child_node *create_child(uint16_t );
4949
inline child_node *get_child_by_id(uint16_t );
5050
inline child_node *get_child_by_pending_seq(uint16_t);
5151
void free_child(child_node *);
52+
void free_all_childs(void);
5253
extern jboolean send_to_child(JNIEnv *, jclass, int, jbyteArray);
5354

54-
#endif
55+
#endif

cSploitClient/command.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@
3434

3535
#include "command.h"
3636

37+
38+
void cmd_on_disconnect() {
39+
child_node *c;
40+
char found = 0;
41+
42+
pthread_mutex_lock(&(children.control.mutex));
43+
44+
for(c =(child_node *) children.list.head;c;c=(child_node *) c->next) {
45+
if(c->pending) {
46+
c->seq = 0;
47+
c->id = CTRL_ID;
48+
found = 1;
49+
}
50+
}
51+
52+
pthread_mutex_unlock(&(children.control.mutex));
53+
54+
if(found) {
55+
pthread_cond_broadcast(&(children.control.cond));
56+
}
57+
}
58+
3759
int on_cmd_started(message *m) {
3860
struct cmd_started_info *started_info;
3961
child_node *c;
@@ -160,7 +182,7 @@ int on_cmd_died(JNIEnv *env, message *m) {
160182

161183
pthread_cond_broadcast(&(children.control.cond));
162184

163-
event = create_child_died_event(env, &(died_info->signal));
185+
event = create_child_died_event(env, died_info->signal);
164186

165187
if(!event) {
166188
LOGE("%s: cannot create event", __func__);

cSploitClient/command.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ int on_command(JNIEnv *, message *);
2424
int start_blind_command(JNIEnv *, jclass, jstring);
2525
int start_command(JNIEnv *, jclass, jstring, jstring, jobjectArray);
2626
void kill_child(JNIEnv *, jclass, int, int);
27+
void cmd_on_disconnect(void);
2728
#endif

cSploitClient/connection.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "notifier.h"
3333
#include "handler.h"
3434
#include "auth.h"
35+
#include "child.h"
36+
#include "command.h"
3537

3638
#include "connection.h"
3739

@@ -43,8 +45,13 @@ void on_connect() {
4345
auth_on_connect();
4446
}
4547

46-
void on_disconnect() {
48+
void on_disconnect(JNIEnv *env) {
4749
auth_on_disconnect();
50+
cmd_on_disconnect();
51+
notifier_on_disconnect(env);
52+
53+
free_all_childs();
54+
unload_handlers();
4855
}
4956

5057
/**
@@ -138,7 +145,7 @@ jboolean is_unix_connected(JNIEnv *env _U_, jclass clazz _U_) {
138145
/**
139146
* @brief disconnect from UNIX socket
140147
*/
141-
void disconnect_unix(JNIEnv *env _U_, jclass clazz _U_) {
148+
void disconnect_unix(JNIEnv *env, jclass clazz _U_) {
142149

143150
if(!connected)
144151
return;
@@ -155,7 +162,5 @@ void disconnect_unix(JNIEnv *env _U_, jclass clazz _U_) {
155162

156163
connected = 0;
157164

158-
on_disconnect();
159-
160-
unload_handlers();
165+
on_disconnect(env);
161166
}

cSploitClient/connection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ extern pthread_mutex_t write_lock;
2727
jboolean connect_unix(JNIEnv *, jclass, jstring);
2828
jboolean is_unix_connected(JNIEnv *, jclass);
2929
void disconnect_unix(JNIEnv *, jclass);
30-
void on_disconnect(void);
30+
void on_disconnect(JNIEnv *);
3131

3232
#endif

cSploitClient/event.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,16 @@ jobject create_child_end_event(JNIEnv *env, void *arg) {
134134

135135
/**
136136
* @brief create an org.csploit.android.events.ChildDied
137-
* @param arg a poitner to the signal that caused the death
137+
* @param signal the signal number that caused the death
138138
* @returns the jobject on success, NULL on error.
139139
*/
140-
jobject create_child_died_event(JNIEnv *env, void *arg) {
140+
jobject create_child_died_event(JNIEnv *env, int signal) {
141141
jobject event;
142142

143143
event = (*env)->NewObject(env,
144144
cache.csploit.events.child_died.class,
145145
cache.csploit.events.child_died.ctor,
146-
*((unsigned int *) arg));
146+
signal);
147147

148148
if(event)
149149
return event;

cSploitClient/event.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
jobject create_newline_event(JNIEnv *, void *);
2727
jobject create_child_end_event(JNIEnv *, void *);
28-
jobject create_child_died_event(JNIEnv *, void *);
28+
jobject create_child_died_event(JNIEnv *, int);
2929
jobject create_stderrnewline_event(JNIEnv *, void *);
3030
jobject create_hop_event(JNIEnv *, void *);
3131
jobject create_port_event(JNIEnv *, void *);

cSploitClient/notifier.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,34 @@ int on_message(JNIEnv *env, message *m) {
339339

340340
}
341341

342+
/**
343+
* @brief notify java that all childs has been killed
344+
*/
345+
void notifier_on_disconnect(JNIEnv *env) {
346+
jobject event;
347+
child_node *c;
348+
349+
pthread_mutex_lock(&(children.control.mutex));
350+
for(c=(child_node *)children.list.head;c;c=(child_node *)c->next) {
351+
event = create_child_died_event(env, SIGKILL);
352+
353+
pthread_mutex_unlock(&(children.control.mutex));
354+
355+
if(!event) {
356+
LOGE("%s: cannot create event", __func__);
357+
} else if(send_event(env, c, event)) {
358+
LOGE("%s: cannot send event", __func__);
359+
}
360+
361+
if(event) {
362+
(*env)->DeleteLocalRef(env, event);
363+
}
364+
365+
pthread_mutex_lock(&(children.control.mutex));
366+
}
367+
pthread_mutex_unlock(&(children.control.mutex));
368+
}
369+
342370
void *notifier(void *arg) {
343371
msg_node *mn;
344372
message *m;

cSploitClient/notifier.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
#ifndef NOTIFIER_H
1919
#define NOTIFIER_H
2020

21+
#include <jni.h>
22+
2123
int start_notifier(void);
2224
void stop_notifier(void);
25+
void notifier_on_disconnect(JNIEnv *);
2326

2427
#endif

0 commit comments

Comments
 (0)