Skip to content

Commit dc287aa

Browse files
committed
fix for being workable on the systems with PID > 65536
1 parent cafc338 commit dc287aa

5 files changed

Lines changed: 437 additions & 5 deletions

File tree

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ DEPDIRS := ${DEP} $(addprefix ${DEP}/,${PROJECTS})
2727
ALLDIRS := ${DEPDIRS} ${OBJDIRS}
2828

2929

30-
.PHONY: all clean
30+
.PHONY: all clean test
3131

3232
EXELIST := ${EXE}/mtproto-proxy
33+
TESTLIST := ${EXE}/test_pid
3334

3435

3536
OBJECTS = \
@@ -96,6 +97,13 @@ ${EXE}/mtproto-proxy: ${OBJ}/mtproto/mtproto-proxy.o ${OBJ}/mtproto/mtproto-conf
9697
${LIB}/libkdb.a: ${LIB_OBJS}
9798
rm -f $@ && ar rcs $@ $^
9899

100+
${EXE}/test_pid: tests/test_pid.c ${OBJ}/common/pid.o | ${EXE}
101+
${CC} ${CFLAGS} ${CINCLUDE} -o $@ $^
102+
103+
test: ${TESTLIST}
104+
@for t in ${TESTLIST}; do echo "--- Running $$t ---"; $$t || exit 1; done
105+
@echo "All tests passed."
106+
99107
clean:
100108
rm -rf ${OBJ} ${DEP} ${EXE} || true
101109

common/pid.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ npid_t PID;
3939
void init_common_PID (void) {
4040
if (!PID.pid) {
4141
int p = getpid ();
42-
assert (!(p & 0xffff0000));
43-
PID.pid = p;
42+
unsigned short folded = (unsigned short)((p & 0xffff) ^ (p >> 16));
43+
PID.pid = folded ? folded : 1;
4444
}
4545
if (!PID.utime) {
4646
PID.utime = time (0);

common/pid.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
struct process_id {
3030
unsigned ip;
3131
short port;
32-
unsigned short pid;
32+
unsigned short pid; /* XOR-folded from OS pid to fit wire protocol (16 bits) */
3333
int utime;
3434
};
3535

3636
struct process_id_ext {
3737
unsigned ip;
3838
short port;
39-
unsigned short pid;
39+
unsigned short pid; /* XOR-folded from OS pid to fit wire protocol (16 bits) */
4040
int utime;
4141
int actor_id;
4242
};

mtproto/mtproto-proxy.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,6 +2304,11 @@ void mtfront_pre_init (void) {
23042304
int pid = fork ();
23052305
assert (pid >= 0);
23062306
if (!pid) {
2307+
npid_t old_PID = PID;
2308+
PID.pid = 0;
2309+
PID.utime = 0;
2310+
init_common_PID ();
2311+
assert (PID.pid != old_PID.pid || PID.utime != old_PID.utime);
23072312
worker_id = i;
23082313
workers = 0;
23092314
slave_mode = 1;

0 commit comments

Comments
 (0)