summaryrefslogtreecommitdiff
path: root/ipc.c (follow)
Commit message (Collapse)AuthorAge
* Switch back to variadic log macros.HEADpumasterAndre Noll2025-03-23
| | | | | | | | | | | | | This essentially reverts commit 66cdd5bc99a5 which aimed to make the dss log facility C89 conform. While this was a worthwhile goal in 2012, it has little value today, since in 2025 we can safely assume a C99 compliant compiler. The patch was created with git revert -Xours 66cdd5bc followed by manual tweaks to make it compile again.
* Compute ipc key only once.Andre Noll2024-05-23
| | | | | It's easier if dss.c passes the hash of the the config file path to the ipc functions than to pass a char pointer.
* Resolve config file path only once.Andre Noll2024-05-23
| | | | | | | | Currently we call get_config_file_name() before each call call to a public function of ipc.c because those functions compute the IPC key from the filename. This is unnecessary and caused several bugs. Clean up this mess by computing the filename only once and store it in a global variable.
* Use standard realpath(3).Andre Noll2024-05-23
| | | | | These days we may rely on the POSIX 2008 semantics of this function, so remove our open-coded version.
* ipc.c: Remove a dead store.Andre Noll2017-11-19
| | | | | scan-build correctly points out that the value stored to 'name' is never read.
* Replace license boilerplate with single line SPDX comments.Andre Noll2017-11-19
| | | | | | | | | | | | | | | | | | | | This gets rid of existing copyright templates in favor of just the one-liner SPDX (Software Package Data Exchange) notice. All files are licensed under the GPL-2.0, so the same tag is added to each file. No copyright is changed by this commit. Several files (mostly the very short ones) did not contain a license text so far. By default all files without license information are under the default license of this package, which is GPL version 2. This commit adds the missing SPDX line so that now all files except dss.css, index.html.in, INSTALL, NEWS and README have it. We also remove author and copyright year, since the author is the same everywhere, and the year hasn't been updated any more since at least six years. Accurate information is available from the git log. The COPYING file can also be removed because the license text at https://spdx.org/licenses/GPL-2.0.html is immutable.
* Revert "ipc.c: Use ftok() instead of SuperFastHash."Andre Noll2017-10-06
| | | | | | | | | | | | This reverts commit c92370affe722f38a85a41d1b5524e4a102b8f4d. This was not a good idea because ftok(3) hashes, among other information, the inode number of the file, and this number changes every time the configuration file is edited. The revert conflicted slightly to the commit which renamed get_key_or_die() to get_key() and changed the type of the return value to key_t, but the conflict was easy to resolve.
* ipc: Improve error diagnostics for kill.Andre Noll2017-07-26
| | | | | | | If dss is not running, the kill command prints "No such file or directory" because the call to semget(2) fails with ENOENT. This message is a bit misleading, so let's return -E_NOT_RUNNING in this case instead.
* ipc: Combine mutex_lock() and lock_dss().Andre Noll2017-07-13
| | | | | The former function is only called by the latter, and both are short, so let's combine them.
* ipc: Prefer key_t over int for System V IPC keys.Andre Noll2017-07-13
| | | | | | | | | | | | | | get_key() calls ftok(3), which returns a key_t value. key_t is also the type which semget(2), the only function which receives the key via mutex_get(), expects. It's stupid to convert the key_t from ftok(3) into an int, only to convert it back to key_t later. This patch changes ipc.c to use key_t everywhere. However, in mutex_get() we print a log message containing the value of the key, so the format string must be adjusted accordingly. Unfortunately, on Linux, key_t is the same as int while on FreeBSD and NetBSD it is defined as long. To avoid a warning from the compiler we use "%lx" in the format string and cast the value to long.
* ipc.c: Use ftok() instead of SuperFastHash.Andre Noll2017-06-25
| | | | | | | | | | | | ftok(3) uses the identity of the named file to generate a key_t type System V IPC key, which is easier than computing the key by hashing the (resolved) pathname of the config file. This change allows to get rid of the realpath() and the super_fast_hash() implementation. If ftok(3) fails, presumably because the underlying call to stat(2) fails, we now simply return a phony identifier, similar to what we did before in this case. This eliminates the only possible failure path in get_key_or_die(), so this function is renamed to get_key().
* ipc.c: Uninline get_key_or_die().Andre Noll2017-04-16
| | | | | | This function is rather big, so it's not clear whether it should be inlined or not. Without the inline attribute, that's up to the compiler to decide.
* ipc.c: Constify parameter of get_key_or_die().Andre Noll2017-04-16
| | | | | The function only reads from the location pointed at by the config_file variable.
* ipc: Fix error code returned by mutex_lock().Andre Noll2017-04-16
| | | | | In the error case do_semop() already returns the error code which corresponds to errno.
* ipc: Simplify mutex_try_lock().Andre Noll2016-08-21
| | | | | | | | | | There is no need to actually obtain the lock. A single semaphore operation will do just fine. With sem_op equal to zero and IPC_NOWAIT the semop() call returns immediately, and the return value tells whether the semaphore value was zero. Rename the (static) function to mutex_is_locked() to indicate that it performs only read-only operations on the semaphore set.
* ipc: Make pid pointer optional.Andre Noll2016-08-21
| | | | | | | | | | This changes get_dss_pid() to handle the case where the caller passed a NULL pid pointer. Conversely, if pid is not NULL, we now make sure to initialize the given address in all cases. The single caller currently never passes NULL, so this change is just defensive programming, protecting against future users. Be liberal in what you accept, be strict in what you return..
* Merge branch 't/rename_system_files'Andre Noll2012-09-16
|\ | | | | | | | | | | | | | | | | | | | | | | Has been cooking for several weeks. The conflicts daemon.c exec.c file.c ipc.c sig.c are trivial and can be resolved easily.
| * Rename source files which also exist as system headers.Andre Noll2012-08-15
| | | | | | | | | | | | | | | | | | As pointed out by Daniel Richard G. some of the dss header files are named the same as system header files. This patch renames these headers as well as their corresponding .c files. Specifically, error.h, fd.h, signal.h, string.h and time.h become err.h, file.h, sig.h, str.h and tv.h.
* | Make the dss log facility C89 conform.Daniel Richard G2012-08-15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Variadic macros were introduced in C99, so they are not supported on ANSI C compilers. Since currently all DSS_*_LOG macros are variadic, we need a replacement for these. Moreover, since not all compilers support __func__ or an equivalent, we need to check for this feature as well and provide a workaround if necessary. This patch introduces the new public function dss_log_set_params() which saves the given log level, filename, line number and the function name in global variables. The DSS_*_LOG macros are changed to receive a single argument only, which is the usual variadic list, enclosed in additional parentheses. The new DSS_*_LOG macros first set the log parameters by calling dss_log_set_params(), then call dss_log() with the variadic list as the argument. dss_log() is patched to print the function name only if __func__ is supported and fall back to file name and the line number otherwise. All DSS_*_LOG() calls are changed to the new syntax.
* | ipc.c: Eliminate per-field struct initializations.Daniel Richard G2012-08-15
| | | | | | | | | | This replaces the C99-only initializations in mutex_lock() and mutex_try_lock() by equivalent but portable code.
* | Have foo.c #include foo.h.Daniel Richard G2012-08-15
|/ | | | | This is both to ensure function signature consistency, and to eliminate GCC's "no previous declaration for _____" warnings.
* mutex_get(): Change parameter from key_t to int.Andre Noll2012-08-03
| | | | | | | | The two callers obtain the key from get_key_or_die(), which returns int. So pass an int anyway. This fixes a warning on NetBSD: ipc.c:263: warning: format '%x' expects type 'unsigned int', but argument 4 has type 'key_t'
* Add the --kill subcommand.Andre Noll2012-06-01
| | | | | | | | | | | | | | | It works as follows: Whenever a semaphore operation is performed, the PID of the process is stored in the sempid field of the semaphore. This PID can be obtained from a different process by calling semctl with the GETPID command. com_kill() first tries to acquire the lock by calling the new mutex_try_lock() function of ipc.c. In contrast to mutex_lock(), mutex_try_lock() only operates on the first semaphore in the semaphore set, leaving the sempid field of the second semaphore unchanged. If mutex_try_lock() succeeds, no running dss process is holding the lock and the kill command fails. Otherwise, some dss process is running whose PID can be obtained by calling semctl() on the second semaphore.
* Use semaphore locking to avoid starting dss multiple times.Andre Noll2012-06-01
It's trickier than one might expect but it is hopefully also much better than any pidfile-based approach. This patch adds ipc.c and ipc.h containing the public lock_dss() function which acquires a semaphore-based lock whose key is based on the hash of the resolved path name of the dss config file. This allows different instances of dss to coexist. All semaphore operations are called with both the SEM_UNDO and the IPC_NOWAIT flag. SEM_UNDO guarantees that no stale lock remains after dss was killed by SIGKIlL while IPC_NOWAIT makes the call to lock_dss() fail if another process is already holding the lock. The prune/create/run commands simply take the lock at startup and exit if it could not be acquired. The underlying semaphore set contains two semaphores. This is necessary to implement the --kill subcommand which is done in a subsequent patch.