- /* canonic names for files */
- av += optind;
- for (i = 0 ; av[i] != NULL ; i++) {
- wpath = realpath(av[i], NULL);
- if (wpath == NULL) {
- syslog(LOG_ERR, "error while getting realpath of %dth widget: %s", i+1, av[i]);
- return 1;
- }
- av[i] = wpath;
+static int update_afids(uint32_t *afids)
+{
+ int rc;
+
+ memset(afids, 0, AFID_ACNT * sizeof(uint32_t));
+ rc = systemd_unit_list(0, get_afid_cb, afids);
+ if (rc >= 0)
+ rc = systemd_unit_list(1, get_afid_cb, afids);
+ if (rc < 0)
+ ERROR("troubles while updating afids");
+ return rc;
+}
+
+static int first_free_afid(uint32_t *afids)
+{
+ int afid;
+
+ afid = AFID_MIN;
+ while (afid <= AFID_MAX && !~afids[AFID_AIDX(afid)])
+ afid += 32;
+ while (afid <= AFID_MAX && AFID_TEST(afids, afid))
+ afid++;
+ if (afid > AFID_MAX) {
+ ERROR("Can't compute a valid afid");
+ errno = EADDRNOTAVAIL;
+ afid = -1;
+ }
+ return afid;
+}
+
+static int get_new_afid()
+{
+ int afid;
+
+ /* ensure existing afid bitmap */
+ if (afids_array == NULL) {
+ afids_array = malloc(AFID_ACNT * sizeof(uint32_t));
+ if (afids_array == NULL || update_afids(afids_array) < 0)
+ return -1;
+ }
+
+ /* allocates the afid */
+ afid = first_free_afid(afids_array);
+ if (afid < 0 && errno == EADDRNOTAVAIL) {
+ /* no more ids, try to rescan */
+ memset(afids_array, 0, AFID_ACNT * sizeof(uint32_t));
+ if (update_afids(afids_array) >= 0)
+ afid = first_free_afid(afids_array);