Skip to content

Commit

Permalink
custom log names for ipc errors
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed May 10, 2024
1 parent 673edbf commit 5a7b7bc
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 27 deletions.
18 changes: 11 additions & 7 deletions src/ipc/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#pragma once

#ifndef IPC_LOG_NAME
#define IPC_LOG_NAME "ipc"
#endif

#include "ipc_proc.h"
#include "ipc_ring.h"
#include "ipc_sem.h"
Expand Down Expand Up @@ -115,15 +119,15 @@ ipc_server_t* ipc_server_start(const char* args[], const char* const name, const
ipc_server_t* const server = (ipc_server_t*)calloc(1, sizeof(ipc_server_t));
if (server == NULL)
{
fprintf(stderr, "[ipc] ipc_process_start failed: out of memory\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_process_start failed: out of memory\n");
return NULL;
}

const uint32_t shared_data_size = sizeof(ipc_shared_data_t) + (sizeof(ipc_ring_t) + rbsize) * 2;

if (! ipc_shm_server_create(&server->shm, name, shared_data_size, false))
{
fprintf(stderr, "[ipc] ipc_process_start failed: could not create shared memory segment\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_process_start failed: could not create shared memory segment\n");
free(server);
return NULL;
}
Expand All @@ -139,15 +143,15 @@ ipc_server_t* ipc_server_start(const char* args[], const char* const name, const

if (! ipc_sem_create(&shared_data->sem_server))
{
fprintf(stderr, "[ipc] ipc_sem_create failed\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_sem_create failed\n");
ipc_shm_server_destroy(&server->shm);
free(server);
return NULL;
}

if (! ipc_sem_create(&shared_data->sem_client))
{
fprintf(stderr, "[ipc] ipc_sem_create failed\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_sem_create failed\n");
ipc_sem_destroy(&shared_data->sem_server);
ipc_shm_server_destroy(&server->shm);
free(server);
Expand All @@ -170,7 +174,7 @@ ipc_server_t* ipc_server_start(const char* args[], const char* const name, const
return server;
}

fprintf(stderr, "[ipc] client side failed to start\n");
fprintf(stderr, "[" IPC_LOG_NAME "] client side failed to start\n");
ipc_server_stop(server);
return NULL;
}
Expand Down Expand Up @@ -240,15 +244,15 @@ ipc_client_t* ipc_client_attach(const char* const name, const uint32_t rbsize)

if (client == NULL)
{
fprintf(stderr, "[ipc] ipc_client_attach failed: out of memory\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_client_attach failed: out of memory\n");
return NULL;
}

const uint32_t shared_data_size = sizeof(ipc_shared_data_t) + (sizeof(ipc_ring_t) + rbsize) * 2;

if (! ipc_shm_client_attach(&client->shm, name, shared_data_size, false))
{
fprintf(stderr, "[ipc] ipc_client_attach failed: could not attach shared memory segment\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_client_attach failed: could not attach shared memory segment\n");
free(client);
return NULL;
}
Expand Down
20 changes: 12 additions & 8 deletions src/ipc/ipc_proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#pragma once

#ifndef IPC_LOG_NAME
#define IPC_LOG_NAME "ipc"
#endif

#ifdef __cplusplus
#define IPC_STRUCT_INIT {}
#include <cstdint>
Expand Down Expand Up @@ -48,7 +52,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[])
#else
if (access(args[0], X_OK) != 0)
{
fprintf(stderr, "[ipc] cannot exec: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] cannot exec: %s\n", strerror(errno));
return NULL;
}
#endif
Expand All @@ -57,7 +61,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[])
ipc_proc_t* volatile const proc = (ipc_proc_t*)calloc(1, sizeof(ipc_proc_t));
if (proc == NULL)
{
fprintf(stderr, "[ipc] ipc_proc_start failed: out of memory\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_proc_start failed: out of memory\n");
return NULL;
}

Expand All @@ -74,7 +78,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[])
wchar_t* const cmd = (wchar_t*)malloc(sizeof(wchar_t) * cmdlen);
if (cmd == NULL)
{
fprintf(stderr, "[ipc] ipc_proc_start failed: out of memory\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_proc_start failed: out of memory\n");
free(proc);
return NULL;
}
Expand All @@ -93,7 +97,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[])
const DWORD wrtn = MultiByteToWideChar(CP_UTF8, 0, args[i], -1, cmdptr, cmdlen - (cmdptr - cmd) / sizeof(wchar_t));
if (wrtn <= 0)
{
fprintf(stderr, "[ipc] ipc_proc_start failed: %s\n", StrError(GetLastError()));
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_proc_start failed: %s\n", StrError(GetLastError()));
free(cmd);
free(proc);
return NULL;
Expand All @@ -107,7 +111,7 @@ ipc_proc_t* ipc_proc_start(const char* const args[])

*cmdptr = 0;

fprintf(stderr, "[ipc] ipc_proc_start trying to launch '%ls'\n", cmd);
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_proc_start trying to launch '%ls'\n", cmd);

STARTUPINFOW si = IPC_STRUCT_INIT;
si.cb = sizeof(si);
Expand All @@ -129,13 +133,13 @@ ipc_proc_t* ipc_proc_start(const char* const args[])
// child process
case 0:
execvp(args[0], (char* const*)args);
fprintf(stderr, "[ipc] exec failed: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] exec failed: %s\n", strerror(errno));
_exit(1);
return NULL;

// error
case -1:
fprintf(stderr, "[ipc] vfork failed: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] vfork failed: %s\n", strerror(errno));
free(proc);
return NULL;
}
Expand Down Expand Up @@ -228,7 +232,7 @@ void ipc_proc_stop(ipc_proc_t* const proc)
break;
}

fprintf(stderr, "[ipc] waitpid failed: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] waitpid failed: %s\n", strerror(errno));
return;
}
#endif
Expand Down
8 changes: 6 additions & 2 deletions src/ipc/ipc_ring.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#pragma once

#ifndef IPC_LOG_NAME
#define IPC_LOG_NAME "ipc"
#endif

#ifdef __cplusplus
#include <cassert>
#include <cstdint>
Expand Down Expand Up @@ -69,7 +73,7 @@ bool ipc_ring_read(ipc_ring_t* ring, void* dst, uint32_t size)
if ((ring->flags & ipc_ring_flag_error_reading) == 0)
{
ring->flags |= ipc_ring_flag_error_reading;
fprintf(stderr, "[ipc] ipc_ring_read failed: not enough space\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_ring_read failed: not enough space\n");
}
return false;
}
Expand Down Expand Up @@ -122,7 +126,7 @@ bool ipc_ring_write(ipc_ring_t* ring, const void* src, uint32_t size)
if ((ring->flags & ipc_ring_flag_error_writing) == 0)
{
ring->flags |= ipc_ring_flag_error_writing;
fprintf(stderr, "[ipc] ipc_ring_write failed: not enough space\n");
fprintf(stderr, "[" IPC_LOG_NAME "] ipc_ring_write failed: not enough space\n");
}
ring->flags |= ipc_ring_flag_invalidate_commit;
return false;
Expand Down
6 changes: 5 additions & 1 deletion src/ipc/ipc_sem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#pragma once

#ifndef IPC_LOG_NAME
#define IPC_LOG_NAME "ipc"
#endif

#ifdef __cplusplus
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -68,7 +72,7 @@ bool ipc_sem_create(ipc_sem_t* const sem)
#else
if (sem_init(sem, 1, 0) == 0)
return true;
fprintf(stderr, "[ipc] sem_init failed: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] sem_init failed: %s\n", strerror(errno));
return false;
#endif
}
Expand Down
22 changes: 13 additions & 9 deletions src/ipc/ipc_shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#pragma once

#ifndef IPC_LOG_NAME
#define IPC_LOG_NAME "ipc"
#endif

#ifdef __cplusplus
#define IPC_STRUCT_INIT {}
#include <cstddef>
Expand Down Expand Up @@ -101,14 +105,14 @@ bool ipc_shm_server_create(ipc_shm_server_t* const shm, const char* const name,
shm->handle = CreateFileMappingA(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE|SEC_COMMIT, 0, (DWORD)size, shmname);
if (shm->handle == NULL)
{
fprintf(stderr, "[ipc] CreateFileMapping failed: %s\n", StrError(GetLastError()));
fprintf(stderr, "[" IPC_LOG_NAME "] CreateFileMapping failed: %s\n", StrError(GetLastError()));
return false;
}

shm->ptr = (uint8_t*)MapViewOfFile(shm->handle, FILE_MAP_ALL_ACCESS, 0, 0, size);
if (shm->ptr == NULL)
{
fprintf(stderr, "[ipc] MapViewOfFile failed: %s\n", StrError(GetLastError()));
fprintf(stderr, "[" IPC_LOG_NAME "] MapViewOfFile failed: %s\n", StrError(GetLastError()));
CloseHandle(shm->handle);
return false;
}
Expand All @@ -119,13 +123,13 @@ bool ipc_shm_server_create(ipc_shm_server_t* const shm, const char* const name,
shm->fd = shm_open(shmname, O_CREAT|O_EXCL|O_RDWR, 0666);
if (shm->fd < 0)
{
fprintf(stderr, "[ipc] shm_open failed: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] shm_open failed: %s\n", strerror(errno));
return false;
}

if (ftruncate(shm->fd, (off_t)size) != 0)
{
fprintf(stderr, "[ipc] ftruncate failed: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] ftruncate failed: %s\n", strerror(errno));
close(shm->fd);
shm_unlink(shmname);
return false;
Expand All @@ -147,7 +151,7 @@ bool ipc_shm_server_create(ipc_shm_server_t* const shm, const char* const name,

if (shm->ptr == NULL || shm->ptr == MAP_FAILED)
{
fprintf(stderr, "[ipc] mmap failed: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] mmap failed: %s\n", strerror(errno));
close(shm->fd);
shm_unlink(shmname);
return false;
Expand Down Expand Up @@ -188,14 +192,14 @@ bool ipc_shm_client_attach(ipc_shm_client_t* const shm, const char* const name,
shm->handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, shmname);
if (shm->handle == NULL)
{
fprintf(stderr, "[ipc] OpenFileMapping failed: %s\n", StrError(GetLastError()));
fprintf(stderr, "[" IPC_LOG_NAME "] OpenFileMapping failed: %s\n", StrError(GetLastError()));
return false;
}

shm->ptr = (uint8_t*)MapViewOfFile(shm->handle, FILE_MAP_ALL_ACCESS, 0, 0, size);
if (shm->ptr == NULL)
{
fprintf(stderr, "[ipc] MapViewOfFile failed: %s\n", StrError(GetLastError()));
fprintf(stderr, "[" IPC_LOG_NAME "] MapViewOfFile failed: %s\n", StrError(GetLastError()));
CloseHandle(shm->handle);
return false;
}
Expand All @@ -206,7 +210,7 @@ bool ipc_shm_client_attach(ipc_shm_client_t* const shm, const char* const name,
shm->fd = shm_open(shmname, O_RDWR, 0);
if (shm->fd < 0)
{
fprintf(stderr, "[ipc] shm_open failed: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] shm_open failed: %s\n", strerror(errno));
return false;
}

Expand All @@ -226,7 +230,7 @@ bool ipc_shm_client_attach(ipc_shm_client_t* const shm, const char* const name,

if (shm->ptr == NULL || shm->ptr == MAP_FAILED)
{
fprintf(stderr, "[ipc] mmap failed: %s\n", strerror(errno));
fprintf(stderr, "[" IPC_LOG_NAME "] mmap failed: %s\n", strerror(errno));
close(shm->fd);
return false;
}
Expand Down
1 change: 1 addition & 0 deletions src/ui-client.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2024 Filipe Coelho <[email protected]>
// SPDX-License-Identifier: ISC

#define IPC_LOG_NAME "ipc-client"
#include "ui-base.h"

#include <dlfcn.h>
Expand Down
1 change: 1 addition & 0 deletions src/ui-server.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2024 Filipe Coelho <[email protected]>
// SPDX-License-Identifier: ISC

#define IPC_LOG_NAME "ipc-server"
#include "ui-base.h"

#include <lv2/urid/urid.h>
Expand Down

0 comments on commit 5a7b7bc

Please sign in to comment.