@@ -4,6 +4,9 @@ import * as CP from 'child_process'
44import { promisify } from 'util'
55import { supportedEngine } from './xdebugUtils'
66import { DOMParser } from '@xmldom/xmldom'
7+ import * as fs from 'fs'
8+ import { decode } from 'iconv-lite'
9+ import { ENCODING } from './dbgp'
710
811export class ControlSocket {
912 /**
@@ -58,7 +61,11 @@ export class ControlSocket {
5861 } )
5962 s . on ( 'data' , data => {
6063 s . destroy ( )
61- resolve ( data . toString ( ) )
64+ if ( data . length > 0 && data . at ( data . length - 1 ) == 0 ) {
65+ resolve ( decode ( data . subarray ( 0 , data . length - 1 ) , ENCODING ) )
66+ } else {
67+ resolve ( decode ( data , ENCODING ) )
68+ }
6269 } )
6370 s . on ( 'error' , error => {
6471 reject (
@@ -74,10 +81,9 @@ export class ControlSocket {
7481 }
7582
7683 async listControlSockets ( ) : Promise < XdebugRunningProcess [ ] > {
77- let retval :XdebugRunningProcess [ ]
84+ let retval : XdebugRunningProcess [ ]
7885 if ( process . platform === 'linux' ) {
79- // TODO
80- throw new Error ( 'Invalid platform for Xdebug control socket' )
86+ retval = await this . listControlSocketsLinux ( )
8187 } else if ( process . platform === 'win32' ) {
8288 retval = await this . listControlSocketsWin ( )
8389 } else {
@@ -97,6 +103,23 @@ export class ControlSocket {
97103 return retval2
98104 }
99105
106+ private async listControlSocketsLinux ( ) : Promise < XdebugRunningProcess [ ] > {
107+ const re = / @ ( x d e b u g - c t r l \. \d + ) $ /
108+
109+ const data = await fs . promises . readFile ( '/proc/net/unix' )
110+ const lines = data . toString ( ) . split ( '\n' )
111+ const sockets : XdebugRunningProcess [ ] = [ ]
112+
113+ for ( const line of lines ) {
114+ const matches = line . match ( re )
115+ if ( matches && matches . length > 0 ) {
116+ sockets . push ( { ctrlSocket : matches [ 1 ] } )
117+ }
118+ }
119+
120+ return sockets
121+ }
122+
100123 private async listControlSocketsWin ( ) : Promise < XdebugRunningProcess [ ] > {
101124 const exec = promisify ( CP . exec )
102125 try {
@@ -108,7 +131,6 @@ export class ControlSocket {
108131 . map < XdebugRunningProcess > ( v => < XdebugRunningProcess > { ctrlSocket : v } )
109132
110133 return retval
111-
112134 } catch ( err ) {
113135 if ( err instanceof Error && ( < ExecError > err ) . stderr == 'File Not Found\r\n' ) {
114136 return [ ]
@@ -124,7 +146,7 @@ interface ExecError extends Error {
124146
125147export interface XdebugRunningProcess {
126148 readonly ctrlSocket : string
127- ps : ControlPS
149+ ps ? : ControlPS
128150 // todo
129151}
130152
0 commit comments