HelenOS sources
This source file includes following definitions.
- rd_img_part_by_label
- rd_img_open
- rd_img_close
#include <errno.h>
#include <fibril.h>
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
#include <task.h>
#include <vfs/vfs.h>
#include <vol.h>
#include "rdimg.h"
#define FILE_BD "/srv/bd/file_bd"
#define RD_SVC "bd/iird"
#define RD_LABEL "HelenOS-rd"
static errno_t rd_img_part_by_label(vol_t *vol, const char *label,
service_id_t *rid)
{
vol_part_info_t vinfo;
service_id_t *part_ids = NULL;
size_t nparts;
size_t i;
errno_t rc;
rc = vol_get_parts(vol, &part_ids, &nparts);
if (rc != EOK) {
printf("Error getting list of volumes.\n");
goto out;
}
for (i = 0; i < nparts; i++) {
rc = vol_part_info(vol, part_ids[i], &vinfo);
if (rc != EOK) {
printf("Error getting volume information.\n");
rc = EIO;
goto out;
}
if (str_cmp(vinfo.label, label) == 0) {
*rid = part_ids[i];
rc = EOK;
goto out;
}
}
rc = ENOENT;
out:
free(part_ids);
return rc;
}
errno_t rd_img_open(const char *imgpath, char **rpath, rd_img_t **rimg)
{
rd_img_t *img = NULL;
char *rdpath = NULL;
errno_t rc;
task_id_t id;
task_wait_t wait;
task_exit_t texit;
vfs_stat_t stat;
int retval;
int cnt;
printf("rd_img_open: begin\n");
rdpath = str_dup("/vol/" RD_LABEL);
if (rdpath == NULL) {
rc = ENOMEM;
goto error;
}
img = calloc(1, sizeof(rd_img_t));
if (img == NULL) {
rc = ENOMEM;
goto error;
}
printf("rd_img_open: spawn file_bd\n");
rc = task_spawnl(&id, &wait, FILE_BD, FILE_BD, imgpath, RD_SVC, NULL);
if (rc != EOK) {
rc = EIO;
goto error;
}
printf("rd_img_open: wait for file_bd\n");
rc = task_wait(&wait, &texit, &retval);
if (rc != EOK || texit != TASK_EXIT_NORMAL) {
rc = EIO;
goto error;
}
printf("rd_img_open: wait for RAM disk to be mounted\n");
cnt = 10;
while (cnt > 0) {
rc = vfs_stat_path(rdpath, &stat);
if (rc == EOK)
break;
fibril_sleep(1);
--cnt;
}
if (cnt == 0) {
printf("rd_img_open: ran out of time\n");
rc = EIO;
goto error;
}
img->filebd_tid = id;
*rimg = img;
*rpath = rdpath;
printf("rd_img_open: success\n");
return EOK;
error:
if (rdpath != NULL)
free(rdpath);
if (img != NULL)
free(img);
return rc;
}
errno_t rd_img_close(rd_img_t *img)
{
errno_t rc;
service_id_t rd_svcid;
vol_t *vol = NULL;
printf("rd_img_close: begin\n");
rc = vol_create(&vol);
if (rc != EOK) {
printf("Error opening volume management service.\n");
rc = EIO;
goto error;
}
printf("rd_img_close: Find RAM disk volume.\n");
rc = rd_img_part_by_label(vol, RD_LABEL, &rd_svcid);
if (rc != EOK) {
printf("Error getting RAM disk service ID.\n");
rc = EIO;
goto error;
}
printf("rd_img_close: eject RAM disk volume\n");
rc = vol_part_eject(vol, rd_svcid);
if (rc != EOK) {
printf("Error ejecting RAM disk volume.\n");
rc = EIO;
goto error;
}
vol_destroy(vol);
rc = task_kill(img->filebd_tid);
if (rc != EOK) {
printf("Error killing file_bd.\n");
rc = EIO;
goto error;
}
free(img);
printf("rd_img_close: success\n");
return EOK;
error:
free(img);
if (vol != NULL)
vol_destroy(vol);
return rc;
}
HelenOS homepage, sources at GitHub