Commit b03b372a authored by Éric Thiébaut's avatar Éric Thiébaut
Browse files

Simplify naming of configuration parameters

parent 4f343a97
......@@ -339,7 +339,7 @@ int main(
// Publish shmid of shared camera.
shmid = tao_get_shared_camera_shmid(shcam);
if (tao_config_write_long(group, "shmid", shmid) != TAO_OK) {
if (tao_config_write_long(group, shmid) != TAO_OK) {
fprintf(stderr, "%s: Failed to publish my shmid.\n", progname);
return EXIT_FAILURE;
}
......@@ -425,7 +425,7 @@ static void on_return(
{
// Write invalid shmid.
if (group != NULL) {
tao_config_write_long(group, "shmid", -1L);
tao_config_write_long(group, -1L);
}
// Free all resources (more or less in reverse order compared to creation).
......
......@@ -113,68 +113,43 @@ tao_status tao_file_close(
tao_status tao_config_path(
char* path,
long size,
const char* group,
const char* param)
const char* name)
{
// Check names.
bool flag = false;
long group_length = 0;
if (group != NULL) {
while (group[group_length] != '\0') {
flag |= (group[group_length] == '/');
++group_length;
}
}
long param_length = 0;
if (param != NULL) {
while (param[param_length] != '\0') {
flag |= (param[param_length] == '/');
++param_length;
}
}
if (flag || group_length < 1 || param_length < 1) {
tao_push_error(__func__, TAO_BAD_NAME);
return TAO_ERROR;
}
// Concatenate path.
const char* configdir = TAO_CONFIG_DIR;
long name_length = TAO_STRLEN(name);
long root_length = strlen(configdir);
if (root_length + group_length + param_length + 3 > size) {
if (root_length + name_length + 2 > size) {
tao_push_error(__func__, TAO_BAD_SIZE);
return TAO_ERROR;
}
memcpy(path, configdir, root_length);
path[root_length] = '/';
memcpy(path + root_length + 1, group, group_length);
path[root_length + group_length + 1] = '/';
memcpy(path + root_length + group_length + 2, param, param_length);
path[root_length + group_length + param_length + 2] = '\0';
memcpy(path + root_length + 1, name, name_length);
path[root_length + name_length + 1] = '\0';
return TAO_OK;
}
FILE* tao_config_open(
const char* group,
const char* param,
const char* name,
const char* mode)
{
char path[PATH_MAX];
if (tao_config_path(path, sizeof(path), group, param) != TAO_OK) {
if (tao_config_path(path, sizeof(path), name) != TAO_OK) {
return NULL;
}
return tao_file_open(path, mode);
}
tao_status tao_config_read_long(
const char* group,
const char* param,
const char* name,
long* ptr)
{
if (ptr == NULL) {
tao_push_error(__func__, TAO_BAD_ADDRESS);
return TAO_ERROR;
}
FILE* file = tao_config_open(group, param, "r");
FILE* file = tao_config_open(name, "r");
if (file == NULL) {
return TAO_ERROR;
}
......@@ -191,11 +166,10 @@ tao_status tao_config_read_long(
}
tao_status tao_config_write_long(
const char* group,
const char* param,
const char* name,
long value)
{
FILE* file = tao_config_open(group, param, "w");
FILE* file = tao_config_open(name, "w");
if (file == NULL) {
return TAO_ERROR;
}
......@@ -211,12 +185,11 @@ tao_status tao_config_write_long(
}
tao_shmid tao_config_read_shmid(
const char* group,
const char* param)
const char* name)
{
tao_shmid shmid = TAO_BAD_SHMID;
long value;
if (tao_config_read_long(group, param, &value) != TAO_OK) {
if (tao_config_read_long(name, &value) != TAO_OK) {
tao_discard_errors(NULL);
} else if (value >= 0 && (tao_shmid)value == value) {
shmid = value;
......@@ -225,8 +198,7 @@ tao_shmid tao_config_read_shmid(
}
int tao_config_read(
const char* group,
const char* param,
const char* name,
const char* format,
...)
{
......@@ -235,7 +207,7 @@ int tao_config_read(
tao_push_error(__func__, TAO_BAD_ARGUMENT);
return TAO_ERROR;
}
FILE* file = tao_config_open(group, param, "r");
FILE* file = tao_config_open(name, "r");
if (file == NULL) {
return TAO_ERROR;
}
......@@ -253,8 +225,7 @@ int tao_config_read(
}
tao_status tao_config_write(
const char* group,
const char* param,
const char* name,
const char* format,
...)
{
......@@ -263,7 +234,7 @@ tao_status tao_config_write(
tao_push_error(__func__, TAO_BAD_ARGUMENT);
return TAO_ERROR;
}
FILE* file = tao_config_open(group, param, "w");
FILE* file = tao_config_open(name, "w");
if (file == NULL) {
return TAO_ERROR;
}
......
......@@ -24,8 +24,8 @@ TAO_BEGIN_DECLS
*
* TAO implement a simple mechanism to globally store configuration parameters.
* Each parameter is written (in a human readable form) in a file. The file
* name has the form `"/tmp/tao/$group/$param"` where `$group` is the name of
* the group of parameters and `$param` is the parameter name.
* name has the form `"/tmp/tao/$name"` where `$name` is the name of the
* parameter, possibly with `"/"` to separated parameters in groups.
*
* @{
*/
......@@ -49,14 +49,14 @@ TAO_BEGIN_DECLS
*
* @param path Path to the file.
*
* @param mode Access mode for the file. One of: `"r"`, `"r+"`, `"w"`, `"w+"`,
* `"a"`, or `"a+"`.
* @param mode Access mode for the file. One of: `"r"`, `"r+"`, `"w"`,
* `"w+"`, `"a"`, or `"a+"`.
*
* @return A file pointer or `NULL` in case of error.
*/
extern FILE* tao_file_open(
const char* path,
const char *mode);
const char* mode);
/**
* Close a file.
......@@ -74,15 +74,11 @@ extern tao_status tao_file_close(
/**
* Open a configuration file.
*
* This function opens the file storing a configuration parameter in a given
* group of parameters. The caller is responsible of calling `fclose` or
* tao_file_close() to eventually close the file.
* This function opens the file storing a configuration parameter. The caller
* is responsible of calling `fclose` or tao_file_close() to eventually close
* the file.
*
* @param group Group of configuration parameters. Must not contain special
* characters such as `'/'`.
*
* @param param Name of configuration parameter. Must not contain special
* characters such as `'/'`.
* @param name Name of configuration parameter.
*
* @param mode Access mode for the file. One of: `"r"`, `"r+"`, `"w"`, `"w+"`,
* `"a"`, or `"a+"`.
......@@ -90,52 +86,40 @@ extern tao_status tao_file_close(
* @return A file pointer or `NULL` in case of error.
*/
extern FILE* tao_config_open(
const char* group,
const char* param,
const char* name,
const char* mode);
/**
* Concatenate configuration path.
*
* This function builds the path of the file storing a configuration parameter
* in a given group of parameters.
* This function builds the path of the file storing a configuration parameter.
*
* @param path Destination buffer.
*
* @param size Size (in bytes) of destination buffer.
*
* @param group Group of configuration parameters. Must not contain special
* characters such as `'/'`.
*
* @param param Name of configuration parameter. Must not contain special
* characters such as `'/'`.
* @param name Name of configuration parameter.
*
* @return @ref TAO_OK on success; @ref TAO_ERROR in case of failure.
*/
extern tao_status tao_config_path(
char* path,
long size,
const char* group,
const char* param);
const char* name);
/**
* Read integer configuration parameter.
*
* This function reads the value of an integer configuration parameter.
*
* @param group Group of configuration parameters. Must not contain special
* characters such as `'/'`.
*
* @param param Name of configuration parameter. Must not contain special
* characters such as `'/'`.
* @param name Name of configuration parameter.
*
* @param ptr Address to store parameter value.
*
* @return @ref TAO_OK on success; @ref TAO_ERROR in case of failure.
*/
extern tao_status tao_config_read_long(
const char* group,
const char* param,
const char* name,
long* ptr);
/**
......@@ -143,19 +127,14 @@ extern tao_status tao_config_read_long(
*
* This function writes the value of an integer configuration parameter.
*
* @param group Group of configuration parameters. Must not contain special
* characters such as `'/'`.
*
* @param param Name of configuration parameter. Must not contain special
* characters such as `'/'`.
* @param name Name of configuration parameter.
*
* @param value Parameter value.
*
* @return @ref TAO_OK on success; @ref TAO_ERROR in case of failure.
*/
extern tao_status tao_config_write_long(
const char* group,
const char* param,
const char* name,
long value);
/**
......@@ -166,28 +145,19 @@ extern tao_status tao_config_write_long(
* identifier form some running server. In case of failure, any errors are
* discarded and @ref TAO_BAD_SHMID is returned.
*
* @param group Group of configuration parameters. Must not contain special
* characters such as `'/'`.
*
* @param param Name of configuration parameter. Must not contain special
* characters such as `'/'`.
* @param param Name of configuration parameter.
*
* @return A shared memory identifier of @ref TAO_BAD_SHMID in case of failure.
*/
extern tao_shmid tao_config_read_shmid(
const char* group,
const char* param);
const char* name);
/**
* Read configuration parameter.
*
* This function reads the value of a configuration parameter.
*
* @param group Group of configuration parameters. Must not contain special
* characters such as `'/'`.
*
* @param param Name of configuration parameter. Must not contain special
* characters such as `'/'`.
* @param name Name of configuration parameter.
*
* @param format Format string as in `scanf` function.
*
......@@ -196,10 +166,9 @@ extern tao_shmid tao_config_read_shmid(
* @return The number of scanned values, -1 in case of error.
*/
extern int tao_config_read(
const char* group,
const char* param,
const char* name,
const char* format,
...) TAO_FORMAT_SCANF(3, 4);
...) TAO_FORMAT_SCANF(2, 3);
/**
* Write configuration parameter.
......@@ -208,11 +177,7 @@ extern int tao_config_read(
* `'\n'` is automatically appended if the last character of the format string
* is not a newline.
*
* @param group Group of configuration parameters. Must not contain special
* characters such as `'/'`.
*
* @param param Name of configuration parameter. Must not contain special
* characters such as `'/'`.
* @param name Name of configuration parameter.
*
* @param format Format string as in `printf` function.
*
......@@ -221,10 +186,9 @@ extern int tao_config_read(
* @return @ref TAO_OK on success; @ref TAO_ERROR in case of failure.
*/
extern tao_status tao_config_write(
const char* group,
const char* param,
const char* name,
const char* format,
...) TAO_FORMAT_PRINTF(3, 4);
...) TAO_FORMAT_PRINTF(2, 3);
// * @}
......
......@@ -211,7 +211,7 @@ static char const* shape[] = {
};
static long const nrows = sizeof(shape)/sizeof(shape[0]);
static char const* ident = "FakeDM0";
static char const* ident = NULL;
static char const* progname = NULL;
static bool debug = false;
static tao_remote_mirror* dm = NULL;
......@@ -248,8 +248,8 @@ static void cleanup(void)
}
if (ident != NULL) {
// Write invalid shmid.
tao_config_write_long(ident, "server_shmid", TAO_BAD_SHMID);
tao_config_write_long(ident, "telemetry_shmid", TAO_BAD_SHMID);
// FIXME: This is done by tao_remote_object_destroy.
tao_config_write_long(ident, TAO_BAD_SHMID);
}
}
......@@ -291,84 +291,102 @@ int main(
}
// Parse arguments.
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0 ||
strcmp(argv[i], "--help") == 0) {
printf("usage: %s [OPTIONS ...]\n", progname);
printf(" -ident NAME Name of server [\"%s\"].\n",
ident);
printf(" -orient BITS Orientation of layout [%u].\n",
int iarg = 1;
const char* usage = "usage: %s [OPTIONS ...] [--] NAME\n";
while (iarg < argc) {
if (argv[iarg][0] != '-') {
break;
}
if (argv[iarg][1] == '-' && argv[iarg][2] == '\0') {
++iarg;
break;
}
if (strcmp(argv[iarg], "-h") == 0 || strcmp(argv[iarg], "-help") == 0 ||
strcmp(argv[iarg], "--help") == 0) {
printf(usage, progname);
printf(" NAME Name of server.\n");
printf(" -orient BITS Orientation of layout [%u].\n",
orient);
printf(" -nbufs NUMBER Number of frame buffers [%ld].\n",
printf(" -nbufs NUMBER Number of frame buffers [%ld].\n",
nbufs);
printf(" -perms BITS Bitwise mask of permissions [0%o].\n",
printf(" -perms BITS Bitwise mask of permissions [0%o].\n",
perms);
printf(" -debug Debug mode [%s].\n",
printf(" -debug Debug mode [%s].\n",
(debug ? "true" : "false"));
printf(" -h, -help, --help Print this help.\n");
printf(" -h, -help, --help Print this help.\n");
return EXIT_SUCCESS;
}
if (strcmp(argv[i], "-ident") == 0) {
if (i + 1 >= argc) {
if (strcmp(argv[iarg], "-ident") == 0) {
if (iarg + 1 >= argc) {
fprintf(stderr, "%s: missing argument for option %s\n",
progname, argv[i]);
progname, argv[iarg]);
return EXIT_FAILURE;
}
ident = argv[i+1];
++i;
ident = argv[iarg+1];
++iarg;
continue;
}
if (strcmp(argv[i], "-orient") == 0) {
if (i + 1 >= argc) {
if (strcmp(argv[iarg], "-orient") == 0) {
if (iarg + 1 >= argc) {
fprintf(stderr, "%s: missing argument for option %s\n",
progname, argv[i]);
progname, argv[iarg]);
return EXIT_FAILURE;
}
if (sscanf(argv[i+1], "%i %c", (int*)&orient, &dummy) != 1) {
if (sscanf(argv[iarg+1], "%iarg %c", (int*)&orient, &dummy) != 1) {
fprintf(stderr, "%s: invalid value \"%s\" for option %s\n",
progname, argv[i+1], argv[i]);
progname, argv[iarg+1], argv[iarg]);
return EXIT_FAILURE;
}
orient &= 5;
++i;
++iarg;
continue;
}
if (strcmp(argv[i], "-nbufs") == 0) {
if (i + 1 >= argc) {
if (strcmp(argv[iarg], "-nbufs") == 0) {
if (iarg + 1 >= argc) {
fprintf(stderr, "%s: missing argument for option %s\n",
progname, argv[i]);
progname, argv[iarg]);
return EXIT_FAILURE;
}
if (sscanf(argv[i+1], "%ld %c", &nbufs, &dummy) != 1 || nbufs < 2) {
if (sscanf(argv[iarg+1], "%ld %c", &nbufs, &dummy) != 1 || nbufs < 2) {
fprintf(stderr, "%s: invalid value \"%s\" for option %s\n",
progname, argv[i+1], argv[i]);
progname, argv[iarg+1], argv[iarg]);
return EXIT_FAILURE;
}
++i;
++iarg;
continue;
}
if (strcmp(argv[i], "-perms") == 0) {
if (i + 1 >= argc) {
if (strcmp(argv[iarg], "-perms") == 0) {
if (iarg + 1 >= argc) {
fprintf(stderr, "%s: missing argument for option %s\n",
progname, argv[i]);
progname, argv[iarg]);
return EXIT_FAILURE;
}
if (sscanf(argv[i+1], "%i %c", (int*)&perms, &dummy) != 1) {
if (sscanf(argv[iarg+1], "%iarg %c", (int*)&perms, &dummy) != 1) {
fprintf(stderr, "%s: invalid value \"%s\" for option %s\n",
progname, argv[i+1], argv[i]);
progname, argv[iarg+1], argv[iarg]);
return EXIT_FAILURE;
}
perms &= 0777;
++i;
++iarg;
continue;
}
if (strcmp(argv[i], "-debug") == 0) {
if (strcmp(argv[iarg], "-debug") == 0) {
debug = true;
continue;
}
fprintf(stderr, "%s: unknown option %s\n", progname, argv[i]);
fprintf(stderr, "%s: unknown option %s\n", progname, argv[iarg]);
return EXIT_FAILURE;
}
if (iarg != argc - 1) {
if (iarg == argc) {
fprintf(stderr, "%s: missing server name\n", progname);
} else {
fprintf(stderr, "%s: too many arguments\n", progname);
}
fprintf(stderr, usage, progname);
return EXIT_FAILURE;
}
ident = argv[iarg];
#define NEW_ARRAY(T, nelem) ((T*)malloc((nelem)*sizeof(T)))
......@@ -455,10 +473,7 @@ int main(
fprintf(stderr, "%s: mirror telemetry available at shmid=%d\n",
progname, (int)tao_get_shared_mirror_data_shmid(dat));
}
tao_config_write_long(ident, "server_shmid",
tao_get_remote_mirror_shmid(dm));
tao_config_write_long(ident, "telemetry_shmid",
tao_get_shared_mirror_data_shmid(dat));
tao_config_write_long(ident, tao_get_remote_mirror_shmid(dm));
// Run loop (on entry of the loop we own the lock on the remote mirror
// instance).
......
......@@ -53,6 +53,11 @@ ENCODE(long double, ld);
#undef ENCODE
/**
* @def tao_forced_store(ptr, val)
*
* Force storing value `val` at constant pointer address `ptr`.
*/
#define tao_forced_store(ptr, val) \
_Generic( \
ptr, \
......
......@@ -356,7 +356,7 @@ int main(
reflect_settings();
// Publish shmid of shared camera.
if (tao_config_write_long(group, "shmid", tao_get_shmid(shcam) )!= TAO_OK) {
if (tao_config_write_long(group, tao_get_shmid(shcam) )!= TAO_OK) {
fprintf(stderr, "%s: Failed to publish my shmid\n", progname);
goto error;
}
......@@ -429,7 +429,7 @@ int main(
error:
if (group != NULL) {
tao_config_write_long(group, "shmid", -1L);
tao_config_write_long(group, -1L);
}
tao_report_errors();
return EXIT_FAILURE;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment