If remote debugging is activated, lock a file to tell the IDE we are alive.

[INTERPRETER]
* NEW: If remote debugging is activated, lock a file to tell the IDE we are alive.
This commit is contained in:
gambas 2021-10-21 01:02:07 +02:00
parent 3771b2df30
commit 4681142810
3 changed files with 45 additions and 31 deletions

View file

@ -112,6 +112,7 @@ void DEBUG_init(void)
const char *dir; const char *dir;
const char *fifo_name; const char *fifo_name;
int pid; int pid;
int fd_lock;
if (!EXEC_debug) if (!EXEC_debug)
{ {
@ -128,6 +129,13 @@ void DEBUG_init(void)
if (!pid) if (!pid)
return; return;
sprintf(COMMON_buffer, DEBUG_FIFO_PATTERN, getuid(), pid, "lock");
fd_lock = open(COMMON_buffer, O_CREAT | O_WRONLY | O_CLOEXEC, 0666);
if (fd_lock < 1)
return;
STREAM_lock_all_fd(fd_lock); // On program end, that file will be automatically closed, and the lock released.
EXEC_debug = TRUE; EXEC_debug = TRUE;
EXEC_fifo = TRUE; EXEC_fifo = TRUE;

View file

@ -1872,11 +1872,38 @@ int STREAM_handle(STREAM *stream)
return (-1); return (-1);
} }
int STREAM_lock_all_fd(int fd)
{
int64_t pos;
pos = lseek(fd, 0, SEEK_CUR);
if (pos < 0)
return errno;
if (lseek(fd, 0, SEEK_SET) < 0)
return errno;
#ifdef F_TLOCK
if (lockf(fd, F_TLOCK, 0))
return errno;
#else
return ENOTSUP;
#endif
if (lseek(fd, pos, SEEK_SET) < 0)
return errno;
return 0;
}
bool STREAM_lock_all(STREAM *stream) bool STREAM_lock_all(STREAM *stream)
{ {
int64_t pos;
int fd; int fd;
int err;
if (STREAM_is_closed(stream)) if (STREAM_is_closed(stream))
THROW(E_CLOSED); THROW(E_CLOSED);
@ -1884,37 +1911,15 @@ bool STREAM_lock_all(STREAM *stream)
fd = STREAM_handle(stream); fd = STREAM_handle(stream);
if (fd < 0) if (fd < 0)
return TRUE; return TRUE;
err = STREAM_lock_all_fd(fd);
if (err == 0)
return FALSE;
if (err == EAGAIN)
return TRUE;
pos = lseek(fd, 0, SEEK_CUR); THROW_SYSTEM(err, NULL);
if (pos < 0)
goto __ERROR;
if (lseek(fd, 0, SEEK_SET) < 0)
goto __ERROR;
#ifdef F_TLOCK
if (lockf(fd, F_TLOCK, 0))
{
if (errno == EAGAIN)
return TRUE;
else
goto __ERROR;
}
#else
ERROR_warning("locking is not implemented");
#endif
if (lseek(fd, pos, SEEK_SET) < 0)
goto __ERROR;
return FALSE;
__ERROR:
THROW_SYSTEM(errno, NULL);
return TRUE; return TRUE;
} }

View file

@ -264,6 +264,7 @@ void STREAM_load(const char *path, char **buffer, int *len);
bool STREAM_map(const char *path, char **paddr, int *plen); bool STREAM_map(const char *path, char **paddr, int *plen);
void STREAM_unmap(char *addr, int len); void STREAM_unmap(char *addr, int len);
int STREAM_lock_all_fd(int fd);
bool STREAM_lock_all(STREAM *stream); bool STREAM_lock_all(STREAM *stream);
#define STREAM_is_closed(_stream) ((_stream)->type == NULL) #define STREAM_is_closed(_stream) ((_stream)->type == NULL)