summaryrefslogtreecommitdiffstats
path: root/src/libply/ply-utils.h
blob: 0af3f29745fd4ea0d6356f9fdd7cb78c41e68023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ply-utils.h - random useful functions and macros
 *
 * Copyright (C) 2007 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Written By: Ray Strode <rstrode@redhat.com>
 */
#ifndef PLY_UTILS_H
#define PLY_UTILS_H

#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>

#ifndef MIN
#define MIN(a,b) ((a) <= (b)? (a) : (b))
#endif

#ifndef MAX
#define MAX(a,b) ((a) >= (b)? (a) : (b))
#endif

#ifndef CLAMP
#define CLAMP(a,b,c) (MIN (MAX ((a), (b)), (c)))
#endif

typedef intptr_t ply_module_handle_t;
typedef void (* ply_module_function_t) (void);

typedef intptr_t ply_daemon_handle_t;

#ifndef PLY_HIDE_FUNCTION_DECLARATIONS
bool ply_open_unidirectional_pipe (int *sender_fd,
                                   int *receiver_fd);
int ply_connect_to_unix_socket (const char *path,
                                bool        is_abstract);
int ply_listen_to_unix_socket (const char *path,
                               bool        is_abstract);
bool ply_get_credentials_from_fd (int    fd,
                                  pid_t *pid,
                                  uid_t *uid,
                                  gid_t *gid);

bool ply_write (int         fd,
                const void *buffer,
                size_t      number_of_bytes); 
bool ply_read (int     fd,
               void   *buffer,
               size_t  number_of_bytes); 

bool ply_fd_has_data (int fd);
bool ply_fd_can_take_data (int fd);
bool ply_fd_may_block (int fd);
char **ply_copy_string_array (const char * const *array);
void ply_free_string_array (char **array);
void ply_close_all_fds (void);
double ply_get_timestamp (void);

void ply_save_errno (void);
void ply_restore_errno (void);

bool ply_directory_exists (const char *dir);
bool ply_file_exists (const char *file);
void ply_list_directory (const char *dir);

ply_module_handle_t *ply_open_module (const char *module_path);
ply_module_function_t ply_module_look_up_function (ply_module_handle_t *handle,
                                                   const char  *function_name);
void ply_close_module (ply_module_handle_t *handle);

bool ply_create_directory (const char *directory);

ply_daemon_handle_t *ply_create_daemon (void);
bool ply_detach_daemon (ply_daemon_handle_t *handle,
                        int                  exit_code);

#endif

#endif /* PLY_UTILS_H */
/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */